Skip to content
Snippets Groups Projects
Commit 6bbe63b2 authored by Jonah Husson's avatar Jonah Husson
Browse files

Comment fixes, bool -> enum for connectiontype

parent 498028a0
No related branches found
No related tags found
3 merge requests!39Merge release into master,!32Project/channels,!31Abstract connections from hosts, add capability to use webgrpc hosts
...@@ -2,6 +2,7 @@ package connect ...@@ -2,6 +2,7 @@ package connect
import ( import (
"github.com/ktr0731/grpc-web-go-client/grpcweb" "github.com/ktr0731/grpc-web-go-client/grpcweb"
jww "github.com/spf13/jwalterweatherman"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
...@@ -9,6 +10,27 @@ const ( ...@@ -9,6 +10,27 @@ const (
tlsError = "TLS cannot be disabled in production, only for testing suites!" tlsError = "TLS cannot be disabled in production, only for testing suites!"
) )
// ConnectionType is intended to act as an enum for different methods of host connection
type ConnectionType uint8
// Enumerate the extant connection methods
const (
Grpc ConnectionType = iota
Web
)
// Stringify connection constants
func (ct ConnectionType) String() string {
switch ct {
case Grpc:
return "grpc"
case Web:
return "web"
default:
return "unknown"
}
}
// Connection is an interface designed to sit between hosts and connections // Connection is an interface designed to sit between hosts and connections
// to allow use of grpcweb clients. // to allow use of grpcweb clients.
type Connection interface { type Connection interface {
...@@ -36,15 +58,15 @@ type clientConnHelpers interface { ...@@ -36,15 +58,15 @@ type clientConnHelpers interface {
disconnect() disconnect()
} }
// newConnection initializes a webConn and returns it wrapped as a Connection // newConnection initializes a webConn or grpcConn and returns it wrapped as a Connection
func newConnection(isWeb bool, host *Host) Connection { func newConnection(t ConnectionType, host *Host) Connection {
if isWeb { switch t {
return &webConn{ case Web:
h: host, return &webConn{h: host}
} case Grpc:
} else { return &grpcConn{h: host}
return &grpcConn{ default:
h: host, jww.ERROR.Printf("Cannot make connection of type %s", t)
} return nil
} }
} }
...@@ -39,8 +39,9 @@ func TestWebConnection(t *testing.T) { ...@@ -39,8 +39,9 @@ func TestWebConnection(t *testing.T) {
} }
hostParams := GetDefaultHostParams() hostParams := GetDefaultHostParams()
TestingOnlyDisableTLS = true TestingOnlyDisableTLS = true
hostParams.ConnectionType = Web
h, err := newHost(hostId, addr, nil, hostParams, true) h, err := newHost(hostId, addr, nil, hostParams)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -12,11 +12,10 @@ import ( ...@@ -12,11 +12,10 @@ import (
"time" "time"
) )
// webConn implements the Connection interface // grpcConn implements the Connection interface
type grpcConn struct { type grpcConn struct {
h *Host h *Host
webConn *grpcweb.ClientConn connection *grpc.ClientConn
grpcConn *grpc.ClientConn
} }
// GetWebConn returns the grpcweb ClientConn object // GetWebConn returns the grpcweb ClientConn object
...@@ -27,7 +26,7 @@ func (gc *grpcConn) GetWebConn() *grpcweb.ClientConn { ...@@ -27,7 +26,7 @@ func (gc *grpcConn) GetWebConn() *grpcweb.ClientConn {
// GetGrpcConn returns the grpc ClientConn object // GetGrpcConn returns the grpc ClientConn object
func (gc *grpcConn) GetGrpcConn() *grpc.ClientConn { func (gc *grpcConn) GetGrpcConn() *grpc.ClientConn {
return gc.grpcConn return gc.connection
} }
// Connect initializes the appropriate connection using helper functions. // Connect initializes the appropriate connection using helper functions.
...@@ -35,7 +34,7 @@ func (gc *grpcConn) Connect() error { ...@@ -35,7 +34,7 @@ func (gc *grpcConn) Connect() error {
return gc.connectGrpcHelper() return gc.connectGrpcHelper()
} }
// IsWeb returns true if the webConn is configured for web connections // IsWeb returns true if the connection is configured for web connections
func (gc *grpcConn) IsWeb() bool { func (gc *grpcConn) IsWeb() bool {
return false return false
} }
...@@ -90,7 +89,7 @@ func (gc *grpcConn) connectGrpcHelper() (err error) { ...@@ -90,7 +89,7 @@ func (gc *grpcConn) connectGrpcHelper() (err error) {
} }
// Create the connection // Create the connection
gc.grpcConn, err = grpc.DialContext(ctx, gc.h.GetAddress(), gc.connection, err = grpc.DialContext(ctx, gc.h.GetAddress(),
dialOpts...) dialOpts...)
if err != nil { if err != nil {
...@@ -113,12 +112,12 @@ func (gc *grpcConn) connectGrpcHelper() (err error) { ...@@ -113,12 +112,12 @@ func (gc *grpcConn) connectGrpcHelper() (err error) {
return return
} }
// Close calls the internal Close function on the grpcConn // Close calls the internal Close function on the connection
func (gc *grpcConn) Close() error { func (gc *grpcConn) Close() error {
if gc.grpcConn == nil { if gc.connection == nil {
return nil return nil
} }
return gc.grpcConn.Close() return gc.connection.Close()
} }
// disconnect closes the grpcConn connection while not under a write lock. // disconnect closes the grpcConn connection while not under a write lock.
...@@ -127,14 +126,14 @@ func (gc *grpcConn) disconnect() { ...@@ -127,14 +126,14 @@ func (gc *grpcConn) disconnect() {
// it's possible to close a host which never sent so that it never made a // it's possible to close a host which never sent so that it never made a
// connection. In that case, we should not close a connection which does not // connection. In that case, we should not close a connection which does not
// exist // exist
if gc.grpcConn != nil { if gc.connection != nil {
jww.INFO.Printf("Disconnected from %s at %s", gc.h.GetId(), gc.h.GetAddress()) jww.INFO.Printf("Disconnected from %s at %s", gc.h.GetId(), gc.h.GetAddress())
err := gc.grpcConn.Close() err := gc.connection.Close()
if err != nil { if err != nil {
jww.ERROR.Printf("Unable to close connection to %s: %+v", jww.ERROR.Printf("Unable to close connection to %s: %+v",
gc.h.GetAddress(), errors.New(err.Error())) gc.h.GetAddress(), errors.New(err.Error()))
} else { } else {
gc.grpcConn = nil gc.connection = nil
} }
} }
} }
...@@ -142,10 +141,10 @@ func (gc *grpcConn) disconnect() { ...@@ -142,10 +141,10 @@ func (gc *grpcConn) disconnect() {
// isAlive returns true if the grpcConn is non-nil and alive // isAlive returns true if the grpcConn is non-nil and alive
// must already be under the connectionMux // must already be under the connectionMux
func (gc *grpcConn) isAlive() bool { func (gc *grpcConn) isAlive() bool {
if gc.grpcConn == nil { if gc.connection == nil {
return false return false
} }
state := gc.grpcConn.GetState() state := gc.connection.GetState()
return state == connectivity.Idle || state == connectivity.Connecting || return state == connectivity.Idle || state == connectivity.Connecting ||
state == connectivity.Ready state == connectivity.Ready
} }
...@@ -83,18 +83,11 @@ type Host struct { ...@@ -83,18 +83,11 @@ type Host struct {
// NewHost creates a new host object which will use GRPC. // NewHost creates a new host object which will use GRPC.
func NewHost(id *id.ID, address string, cert []byte, func NewHost(id *id.ID, address string, cert []byte,
params HostParams) (host *Host, err error) { params HostParams) (host *Host, err error) {
return newHost(id, address, cert, params, false) return newHost(id, address, cert, params)
}
// NewHostWeb creates a new host object which will use the grpcweb library.
func NewHostWeb(id *id.ID, address string, cert []byte,
params HostParams) (host *Host, err error) {
return newHost(id, address, cert, params, true)
} }
// newHost is a helper which creates a new Host object // newHost is a helper which creates a new Host object
func newHost(id *id.ID, address string, cert []byte, params HostParams, func newHost(id *id.ID, address string, cert []byte, params HostParams) (host *Host, err error) {
isWeb bool) (host *Host, err error) {
windowSize := int32(0) windowSize := int32(0)
...@@ -110,7 +103,7 @@ func newHost(id *id.ID, address string, cert []byte, params HostParams, ...@@ -110,7 +103,7 @@ func newHost(id *id.ID, address string, cert []byte, params HostParams,
windowSize: &windowSize, windowSize: &windowSize,
} }
host.connection = newConnection(isWeb, host) host.connection = newConnection(params.ConnectionType, host)
if params.EnableCoolOff { if params.EnableCoolOff {
host.coolOffBucket = rateLimiting.CreateBucket( host.coolOffBucket = rateLimiting.CreateBucket(
......
...@@ -56,6 +56,9 @@ type HostParams struct { ...@@ -56,6 +56,9 @@ type HostParams struct {
// ProxyErrorMetricParams are the parameters used for the proxy error // ProxyErrorMetricParams are the parameters used for the proxy error
// tracker that uses exponential moving average (exponential.MovingAvg). // tracker that uses exponential moving average (exponential.MovingAvg).
ProxyErrorMetricParams exponential.MovingAvgParams ProxyErrorMetricParams exponential.MovingAvgParams
// ConnectionType describes the method for the underlying host connection
ConnectionType ConnectionType
} }
// GetDefaultHostParams Get default set of host params // GetDefaultHostParams Get default set of host params
...@@ -81,5 +84,6 @@ func GetDefaultHostParams() HostParams { ...@@ -81,5 +84,6 @@ func GetDefaultHostParams() HostParams {
PermitWithoutStream: true, PermitWithoutStream: true,
}, },
ProxyErrorMetricParams: exponential.DefaultMovingAvgParams(), ProxyErrorMetricParams: exponential.DefaultMovingAvgParams(),
ConnectionType: Grpc,
} }
} }
...@@ -11,13 +11,12 @@ import ( ...@@ -11,13 +11,12 @@ import (
// webConn implements the Connection interface // webConn implements the Connection interface
type webConn struct { type webConn struct {
h *Host h *Host
webConn *grpcweb.ClientConn connection *grpcweb.ClientConn
grpcConn *grpc.ClientConn
} }
// GetWebConn returns the grpcweb ClientConn object // GetWebConn returns the grpcweb ClientConn object
func (wc *webConn) GetWebConn() *grpcweb.ClientConn { func (wc *webConn) GetWebConn() *grpcweb.ClientConn {
return wc.webConn return wc.connection
} }
// GetGrpcConn returns the grpc ClientConn object // GetGrpcConn returns the grpc ClientConn object
...@@ -31,7 +30,7 @@ func (wc *webConn) Connect() error { ...@@ -31,7 +30,7 @@ func (wc *webConn) Connect() error {
return wc.connectWebHelper() return wc.connectWebHelper()
} }
// IsWeb returns true if the webConn is configured for web connections // IsWeb returns true if the connection is configured for web connections
func (wc *webConn) IsWeb() bool { func (wc *webConn) IsWeb() bool {
return true return true
} }
...@@ -84,7 +83,7 @@ func (wc *webConn) connectWebHelper() (err error) { ...@@ -84,7 +83,7 @@ func (wc *webConn) connectWebHelper() (err error) {
//} //}
// Create the connection // Create the connection
wc.webConn, err = grpcweb.DialContext(wc.h.GetAddress(), wc.connection, err = grpcweb.DialContext(wc.h.GetAddress(),
dialOpts...) dialOpts...)
if err != nil { if err != nil {
...@@ -110,7 +109,7 @@ func (wc *webConn) connectWebHelper() (err error) { ...@@ -110,7 +109,7 @@ func (wc *webConn) connectWebHelper() (err error) {
// Close handles closing the http connection. // Close handles closing the http connection.
func (wc *webConn) Close() error { func (wc *webConn) Close() error {
// TODO this needs work on the grpc-web-go-client side // TODO this needs work on the grpc-web-go-client side
if wc.webConn == nil { if wc.connection == nil {
return nil return nil
} }
return nil return nil
...@@ -123,9 +122,9 @@ func (wc *webConn) disconnect() { ...@@ -123,9 +122,9 @@ func (wc *webConn) disconnect() {
// it's possible to close a host which never sent so that it never made a // it's possible to close a host which never sent so that it never made a
// connection. In that case, we should not close a connection which does not // connection. In that case, we should not close a connection which does not
// exist // exist
if wc.webConn != nil { if wc.connection != nil {
// TODO webconn cannot close yet, this needs work on that side // TODO webconn cannot close yet, this needs work on that side
wc.webConn = nil wc.connection = nil
} }
} }
...@@ -134,7 +133,7 @@ func (wc *webConn) disconnect() { ...@@ -134,7 +133,7 @@ func (wc *webConn) disconnect() {
// must already be under the connectionMux // must already be under the connectionMux
func (wc *webConn) isAlive() bool { func (wc *webConn) isAlive() bool {
// TODO this cannot be determined until grpcweb clients have a persistent connection // TODO this cannot be determined until grpcweb clients have a persistent connection
if wc.webConn == nil { if wc.connection == nil {
return false return false
} }
return true return true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment