From ac1e2bda7d73cf46caf1c0313140b3fbff36d10f Mon Sep 17 00:00:00 2001 From: Waldz Date: Tue, 23 Oct 2018 22:25:05 +0300 Subject: [PATCH] Dont need state for connection.ConnectionCreator --- cmd/di.go | 9 ++++--- core/connection/interface.go | 6 ----- core/connection/manager.go | 30 ++++++++++++--------- core/connection/manager_test.go | 2 +- core/connection/registry.go | 4 +-- core/connection/registry_test.go | 6 ++--- services/noop/connection_factory.go | 15 +++++------ services/openvpn/connection_factory_test.go | 4 +-- 8 files changed, 37 insertions(+), 39 deletions(-) diff --git a/cmd/di.go b/cmd/di.go index 10c05df6a8..fec3a247ea 100644 --- a/cmd/di.go +++ b/cmd/di.go @@ -180,7 +180,7 @@ func (di *Dependencies) bootstrapNodeComponents(nodeOptions node.Options) { di.StatsKeeper = stats.NewSessionStatsKeeper(time.Now) di.ConnectionRegistry = connection.NewRegistry() - di.ConnectionManager = connection.NewManager(di.MysteriumClient, dialogFactory, promiseIssuerFactory, di.ConnectionRegistry, di.StatsKeeper) + di.ConnectionManager = connection.NewManager(di.MysteriumClient, dialogFactory, promiseIssuerFactory, di.ConnectionRegistry.CreateConnection, di.StatsKeeper) router := tequilapi.NewAPIRouter() tequilapi_endpoints.AddRouteForStop(router, utils.SoftKiller(di.Shutdown)) @@ -197,7 +197,7 @@ func (di *Dependencies) bootstrapNodeComponents(nodeOptions node.Options) { } func (di *Dependencies) bootstrapServiceOpenvpn(nodeOptions node.Options) { - di.ConnectionRegistry.Register("openvpn", openvpn.NewProcessBasedConnectionFactory( + connectionFactory := openvpn.NewProcessBasedConnectionFactory( di.MysteriumClient, nodeOptions.Openvpn.BinaryPath, nodeOptions.Directories.Config, @@ -205,11 +205,12 @@ func (di *Dependencies) bootstrapServiceOpenvpn(nodeOptions node.Options) { di.StatsKeeper, di.LocationOriginal, di.SignerFactory, - )) + ) + di.ConnectionRegistry.Register("openvpn", connectionFactory.CreateConnection) } func (di *Dependencies) bootstrapServiceNoop(nodeOptions node.Options) { - di.ConnectionRegistry.Register("dummy", &service_noop.ConnectionFactory{}) + di.ConnectionRegistry.Register("dummy", service_noop.NewConnectionCreator()) } // BootstrapServiceComponents initiates ServiceManager dependency diff --git a/core/connection/interface.go b/core/connection/interface.go index 766362ffb7..1492e39614 100644 --- a/core/connection/interface.go +++ b/core/connection/interface.go @@ -36,12 +36,6 @@ type Connection interface { // StateChannel is the channel we receive state change events on type StateChannel chan State -// ConnectionCreator creates new vpn client by given session, -// consumer identity, provider identity and uses state channel to report state changes -type ConnectionCreator interface { - CreateConnection(ConnectOptions, StateChannel) (Connection, error) -} - // PromiseIssuer issues promises from consumer to provider. // Consumer signs those promises. type PromiseIssuer interface { diff --git a/core/connection/manager.go b/core/connection/manager.go index 15dfa898e4..c749d00a94 100644 --- a/core/connection/manager.go +++ b/core/connection/manager.go @@ -47,13 +47,17 @@ var ( ErrUnsupportedServiceType = errors.New("unsupported service type in proposal") ) +// ConnectionCreator creates new vpn client by given session, +// consumer identity, provider identity and uses state channel to report state changes +type ConnectionCreator func(ConnectOptions, StateChannel) (Connection, error) + type connectionManager struct { //these are passed on creation - mysteriumClient server.Client - newDialog DialogCreator - newPromiseIssuer PromiseIssuerCreator - connectionCreator ConnectionCreator - statsKeeper stats.SessionStatsKeeper + mysteriumClient server.Client + newDialog DialogCreator + newPromiseIssuer PromiseIssuerCreator + newConnection ConnectionCreator + statsKeeper stats.SessionStatsKeeper //these are populated by Connect at runtime ctx context.Context mutex sync.RWMutex @@ -70,13 +74,13 @@ func NewManager( statsKeeper stats.SessionStatsKeeper, ) *connectionManager { return &connectionManager{ - statsKeeper: statsKeeper, - mysteriumClient: mysteriumClient, - newDialog: dialogCreator, - newPromiseIssuer: promiseIssuerCreator, - connectionCreator: connectionCreator, - status: statusNotConnected(), - cleanConnection: warnOnClean, + statsKeeper: statsKeeper, + mysteriumClient: mysteriumClient, + newDialog: dialogCreator, + newPromiseIssuer: promiseIssuerCreator, + newConnection: connectionCreator, + status: statusNotConnected(), + cleanConnection: warnOnClean, } } @@ -149,7 +153,7 @@ func (manager *connectionManager) startConnection(consumerID, providerID identit stateChannel := make(chan State, 10) - connection, err := manager.connectionCreator.CreateConnection( + connection, err := manager.newConnection( ConnectOptions{ SessionID: sessionID, SessionConfig: sessionConfig, diff --git a/core/connection/manager_test.go b/core/connection/manager_test.go index b41829df1d..f74af91267 100644 --- a/core/connection/manager_test.go +++ b/core/connection/manager_test.go @@ -98,7 +98,7 @@ func (tc *testContext) SetupTest() { tc.fakeStatsKeeper = &fakeSessionStatsKeeper{} - tc.connManager = NewManager(tc.fakeDiscoveryClient, dialogCreator, promiseIssuerFactory, tc.fakeConnectionFactory, tc.fakeStatsKeeper) + tc.connManager = NewManager(tc.fakeDiscoveryClient, dialogCreator, promiseIssuerFactory, tc.fakeConnectionFactory.CreateConnection, tc.fakeStatsKeeper) } func (tc *testContext) TestWhenNoConnectionIsMadeStatusIsNotConnected() { diff --git a/core/connection/registry.go b/core/connection/registry.go index c0dec26436..33cb091c39 100644 --- a/core/connection/registry.go +++ b/core/connection/registry.go @@ -36,10 +36,10 @@ func (registry *Registry) Register(serviceType string, creator ConnectionCreator // CreateConnection create plugable connection func (registry *Registry) CreateConnection(options ConnectOptions, stateChannel StateChannel) (Connection, error) { - creator, exists := registry.creators[options.Proposal.ServiceType] + createConnection, exists := registry.creators[options.Proposal.ServiceType] if !exists { return nil, ErrUnsupportedServiceType } - return creator.CreateConnection(options, stateChannel) + return createConnection(options, stateChannel) } diff --git a/core/connection/registry_test.go b/core/connection/registry_test.go index 567970a95e..1f4e685122 100644 --- a/core/connection/registry_test.go +++ b/core/connection/registry_test.go @@ -24,12 +24,12 @@ import ( "github.com/stretchr/testify/assert" ) -var _ ConnectionCreator = &Registry{} +var _ ConnectionCreator = (&Registry{}).CreateConnection var ( connectionMock = &vpnClientFake{} - connectionFactory = &connectionFactoryFake{ - fakeVpnClient: connectionMock, + connectionFactory = func(connectionParams ConnectOptions, stateChannel StateChannel) (Connection, error) { + return connectionMock, nil } ) diff --git a/services/noop/connection_factory.go b/services/noop/connection_factory.go index d4450c8c08..2bde135994 100644 --- a/services/noop/connection_factory.go +++ b/services/noop/connection_factory.go @@ -21,12 +21,11 @@ import ( "github.com/mysteriumnetwork/node/core/connection" ) -// ConnectionFactory creates noop connections -type ConnectionFactory struct{} - -// CreateConnection implements the connection.ConnectionCreator interface -func (establisher *ConnectionFactory) CreateConnection(options connection.ConnectOptions, stateChannel connection.StateChannel) (connection.Connection, error) { - return &Connection{ - stateChannel: stateChannel, - }, nil +// NewConnectionCreator creates noop connections +func NewConnectionCreator() connection.ConnectionCreator { + return func(options connection.ConnectOptions, stateChannel connection.StateChannel) (connection.Connection, error) { + return &Connection{ + stateChannel: stateChannel, + }, nil + } } diff --git a/services/openvpn/connection_factory_test.go b/services/openvpn/connection_factory_test.go index 031f5759d5..1be4f61cca 100644 --- a/services/openvpn/connection_factory_test.go +++ b/services/openvpn/connection_factory_test.go @@ -47,9 +47,9 @@ func (cf *cacheFake) RefreshAndGet() (location.Location, error) { return cf.location, cf.err } -var _ connection.ConnectionCreator = &ProcessBasedConnectionFactory{} +var _ connection.ConnectionCreator = (&ProcessBasedConnectionFactory{}).CreateConnection -func fakeSignerFactory(id identity.Identity) identity.Signer { +func fakeSignerFactory(_ identity.Identity) identity.Signer { return &identity.SignerFake{} }