Skip to content

Commit

Permalink
Get rid of passing setup nodes to transport.NewManager
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Jul 12, 2019
1 parent 6112d99 commit 58e3b0c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
3 changes: 2 additions & 1 deletion pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error)
LogStore: logStore,
DefaultNodes: config.TrustedNodes,
}
node.tm, err = transport.NewManager(tmConfig, config.Routing.SetupNodes, node.messenger)
node.tm, err = transport.NewManager(tmConfig, node.messenger)
if err != nil {
return nil, fmt.Errorf("transport manager: %s", err)
}
node.tm.Logger = node.Logger.PackageLogger("trmanager")
node.tm.SetSetupNodes(config.Routing.SetupNodes)

node.rt, err = config.RoutingTable()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestNodeStartClose(t *testing.T) {
var err error

tmConf := &transport.ManagerConfig{PubKey: cipher.PubKey{}, DiscoveryClient: transport.NewDiscoveryMock()}
node.tm, err = transport.NewManager(tmConf, nil, node.messenger)
node.tm, err = transport.NewManager(tmConf, node.messenger)
require.NoError(t, err)

errCh := make(chan error)
Expand Down
32 changes: 16 additions & 16 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func TestRouterForwarding(t *testing.T) {
f3.SetType("mock2")
f4.SetType("mock2")

m1, err := transport.NewManager(c1, nil, f1)
m1, err := transport.NewManager(c1, f1)
require.NoError(t, err)

m2, err := transport.NewManager(c2, nil, f2, f3)
m2, err := transport.NewManager(c2, f2, f3)
require.NoError(t, err)

m3, err := transport.NewManager(c3, nil, f4)
m3, err := transport.NewManager(c3, f4)
require.NoError(t, err)

rt := routing.InMemoryRoutingTable()
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestRouterAppInit(t *testing.T) {
pk1, sk1 := cipher.GenerateKeyPair()
c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}

m1, err := transport.NewManager(c1, nil)
m1, err := transport.NewManager(c1)
require.NoError(t, err)

conf := &Config{
Expand Down Expand Up @@ -154,10 +154,10 @@ func TestRouterApp(t *testing.T) {
c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}

f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
m1, err := transport.NewManager(c1, nil, f1)
m1, err := transport.NewManager(c1, f1)
require.NoError(t, err)

m2, err := transport.NewManager(c2, nil, f2)
m2, err := transport.NewManager(c2, f2)
require.NoError(t, err)

go m2.Serve(context.TODO()) // nolint
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestRouterLocalApp(t *testing.T) {
logStore := transport.InMemoryTransportLogStore()

pk, sk := cipher.GenerateKeyPair()
m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore}, nil)
m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore})
require.NoError(t, err)

conf := &Config{
Expand Down Expand Up @@ -285,10 +285,10 @@ func TestRouterSetup(t *testing.T) {
c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}

f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
m1, err := transport.NewManager(c1, nil, f1)
m1, err := transport.NewManager(c1, f1)
require.NoError(t, err)

m2, err := transport.NewManager(c2, nil, f2)
m2, err := transport.NewManager(c2, f2)
require.NoError(t, err)

rt := routing.InMemoryRoutingTable()
Expand Down Expand Up @@ -433,10 +433,10 @@ func TestRouterSetupLoop(t *testing.T) {
f1.SetType(dmsg.Type)
f2.SetType(dmsg.Type)

m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, nil, f1)
m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1)
require.NoError(t, err)

m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, nil, f2)
m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, f2)
require.NoError(t, err)
go m2.Serve(context.TODO()) // nolint: errcheck

Expand Down Expand Up @@ -538,10 +538,10 @@ func TestRouterCloseLoop(t *testing.T) {
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f1.SetType(dmsg.Type)

m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, nil, f1)
m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1)
require.NoError(t, err)

m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, nil, f2)
m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, f2)
require.NoError(t, err)
go m2.Serve(context.TODO()) // nolint: errcheck

Expand Down Expand Up @@ -632,10 +632,10 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) {
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f1.SetType(dmsg.Type)

m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, nil, f1)
m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1)
require.NoError(t, err)

m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, nil, f2)
m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, f2)
require.NoError(t, err)
go m2.Serve(context.TODO()) // nolint: errcheck

Expand Down Expand Up @@ -716,7 +716,7 @@ func TestRouterRouteExpiration(t *testing.T) {
logStore := transport.InMemoryTransportLogStore()

pk, sk := cipher.GenerateKeyPair()
m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore}, nil)
m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore})
require.NoError(t, err)

rt := routing.InMemoryRoutingTable()
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func newHsMockEnv() *hsMockEnv {
tr1 := NewMockTransport(in, pk1, pk2)
tr2 := NewMockTransport(out, pk2, pk1)

m1, err1 := NewManager(&ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client}, nil)
m2, err2 := NewManager(&ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client}, nil)
m1, err1 := NewManager(&ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client})
m2, err2 := NewManager(&ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client})

return &hsMockEnv{
client: client,
Expand Down
19 changes: 17 additions & 2 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Manager struct {

// NewManager creates a Manager with the provided configuration and transport factories.
// 'factories' should be ordered by preference.
func NewManager(config *ManagerConfig, setupNodes []cipher.PubKey, factories ...Factory) (*Manager, error) {
func NewManager(config *ManagerConfig, factories ...Factory) (*Manager, error) {
entries, _ := config.DiscoveryClient.GetTransportsByEdge(context.Background(), config.PubKey) // nolint

mEntries := make(map[Entry]struct{})
Expand All @@ -64,10 +64,25 @@ func NewManager(config *ManagerConfig, setupNodes []cipher.PubKey, factories ...
entries: mEntries,
TrChan: make(chan *ManagedTransport, 9), // TODO: eliminate or justify buffering here
doneChan: make(chan struct{}),
setupNodes: setupNodes,
}, nil
}

// SetupNodes returns setup node list contained within the TransportManager.
func (tm *Manager) SetupNodes() []cipher.PubKey {
tm.mu.RLock()
defer tm.mu.RUnlock()

return tm.setupNodes
}

// SetSetupNodes sets setup node list contained within the TransportManager.
func (tm *Manager) SetSetupNodes(nodes []cipher.PubKey) {
tm.mu.Lock()
defer tm.mu.Unlock()

tm.setupNodes = nodes
}

// Factories returns all the factory types contained within the TransportManager.
func (tm *Manager) Factories() []string {
fTypes, i := make([]string, len(tm.factories)), 0
Expand Down
14 changes: 7 additions & 7 deletions pkg/transport/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestTransportManager(t *testing.T) {
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil}

f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, nil, f1)
m1, err := NewManager(c1, f1)
require.NoError(t, err)

assert.Equal(t, []string{"mock"}, m1.Factories())
Expand All @@ -53,7 +53,7 @@ func TestTransportManager(t *testing.T) {
errCh <- m1.Serve(context.TODO())
}()

m2, err := NewManager(c2, nil, f2)
m2, err := NewManager(c2, f2)
require.NoError(t, err)

var mu sync.Mutex
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) {
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil}

f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, nil, f1)
m1, err := NewManager(c1, f1)
require.NoError(t, err)
assert.Equal(t, []string{"mock"}, m1.Factories())

Expand All @@ -147,7 +147,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) {
m1errCh := make(chan error, 1)
go func() { m1errCh <- m1.Serve(context.TODO()) }()

m2, err := NewManager(c2, nil, f2)
m2, err := NewManager(c2, f2)
require.NoError(t, err)

tr2, err := m2.CreateTransport(context.TODO(), pk1, "mock", true)
Expand All @@ -167,7 +167,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) {
require.NoError(t, err)
assert.False(t, dEntry2.IsUp)

m2, err = NewManager(c2, nil, f2)
m2, err = NewManager(c2, f2)
require.NoError(t, err)

m2.reconnectTransports(context.TODO())
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestTransportManagerLogs(t *testing.T) {
c2 := &ManagerConfig{pk2, sk2, client, logStore2, nil}

f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, nil, f1)
m1, err := NewManager(c1, f1)
require.NoError(t, err)

assert.Equal(t, []string{"mock"}, m1.Factories())
Expand All @@ -210,7 +210,7 @@ func TestTransportManagerLogs(t *testing.T) {
errCh <- m1.Serve(context.TODO())
}()

m2, err := NewManager(c2, nil, f2)
m2, err := NewManager(c2, f2)
require.NoError(t, err)

tr2, err := m2.CreateTransport(context.TODO(), pk1, "mock", true)
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func MockTransportManagersPair() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh

f1, f2 := NewMockFactoryPair(pk1, pk2)

if m1, err = NewManager(c1, nil, f1); err != nil {
if m1, err = NewManager(c1, f1); err != nil {
return
}
if m2, err = NewManager(c2, nil, f2); err != nil {
if m2, err = NewManager(c2, f2); err != nil {
return
}

Expand Down

0 comments on commit 58e3b0c

Please sign in to comment.