@@ -34,16 +34,17 @@ type balancerNotifier interface {
3434 OnUpdate (onDiscovery func (ctx context.Context , endpoints []endpoint.Info ))
3535}
3636
37- func New (balancer balancerNotifier , config config.Config ) * Client {
37+ func New (balancer balancerNotifier , config config.Config , knownNodeIDs [] uint32 ) * Client {
3838 return newClient (balancer , func (ctx context.Context , opts ... sessionBuilderOption ) (s * session , err error ) {
3939 return newSession (ctx , balancer , config , opts ... )
40- }, config )
40+ }, config , knownNodeIDs )
4141}
4242
4343func newClient (
4444 balancer balancerNotifier ,
4545 builder sessionBuilder ,
4646 config config.Config ,
47+ knownNodeIDs []uint32 ,
4748) * Client {
4849 var (
4950 ctx = context .Background ()
@@ -55,7 +56,7 @@ func newClient(
5556 cc : balancer ,
5657 build : builder ,
5758 index : make (map [* session ]sessionInfo ),
58- nodes : make (map [uint32 ]map [* session ]struct {}),
59+ nodes : make (map [uint32 ]map [* session ]struct {}, len ( knownNodeIDs ) * 2 ),
5960 idle : list .New (),
6061 waitQ : list .New (),
6162 limit : config .SizeLimit (),
@@ -67,6 +68,9 @@ func newClient(
6768 },
6869 done : make (chan struct {}),
6970 }
71+ for _ , nodeID := range knownNodeIDs {
72+ c .nodes [nodeID ] = make (map [* session ]struct {})
73+ }
7074 if balancer != nil {
7175 balancer .OnUpdate (c .updateNodes )
7276 }
@@ -268,6 +272,8 @@ func (c *Client) appendSessionToNodes(s *session) {
268272 nodeID := s .NodeID ()
269273 if _ , has := c .nodes [nodeID ]; has {
270274 c.nodes [nodeID ][s ] = struct {}{}
275+ } else {
276+ fmt .Println ("" )
271277 }
272278 })
273279}
@@ -286,8 +292,6 @@ func (c *Client) removeSessionFromNodes(s *session) {
286292 delete (sessions , s )
287293 if len (sessions ) == 0 {
288294 delete (c .nodes , nodeID )
289- } else {
290- c .nodes [nodeID ] = sessions
291295 }
292296 }
293297 })
0 commit comments