55 "crypto/tls"
66 "encoding/base64"
77 "fmt"
8- "log"
98 "math/rand"
109 "strconv"
1110 "strings"
@@ -29,6 +28,7 @@ type grpcClient struct {
2928 channel chan msg
3029 closeFlag * int32
3130 once * sync.Once
31+ logger * logger
3232}
3333
3434func (client * grpcClient ) handleError (handle * connectionHandle , headers metadata.MD , trailers metadata.MD , err error ) error {
@@ -54,7 +54,7 @@ func (client *grpcClient) handleError(handle *connectionHandle, headers metadata
5454 }
5555
5656 client .channel <- msg
57- log . Printf ( "[error] Not leader exception, reconnecting to %v" , endpoint )
57+ client . logger . error ( "not leader exception, reconnecting to %v" , endpoint )
5858 return & Error {code : ErrorNotLeader }
5959 }
6060 }
@@ -71,7 +71,7 @@ func (client *grpcClient) handleError(handle *connectionHandle, headers metadata
7171 return & Error {code : code }
7272 }
7373
74- log . Printf ( "[error] unexpected exception: %v" , err )
74+ client . logger . error ( " unexpected exception: %v" , err )
7575
7676 msg := reconnect {
7777 correlation : handle .Id (),
@@ -176,7 +176,7 @@ func newConnectionHandle(id uuid.UUID, serverInfo *ServerInfo, connection *grpc.
176176 }
177177}
178178
179- func connectionStateMachine (config Configuration , closeFlag * int32 , channel chan msg ) {
179+ func connectionStateMachine (config Configuration , closeFlag * int32 , channel chan msg , logger * logger ) {
180180 state := newConnectionState (config )
181181
182182 for {
@@ -187,7 +187,7 @@ func connectionStateMachine(config Configuration, closeFlag *int32, channel chan
187187 err := state .connection .Close ()
188188
189189 if err != nil {
190- log . Printf ( "[warn] error when closing gRPC connection: %v" , err )
190+ logger . warn ( " error when closing gRPC connection. %v" , err )
191191 }
192192 }
193193
@@ -199,7 +199,7 @@ func connectionStateMachine(config Configuration, closeFlag *int32, channel chan
199199 {
200200 // Means we need to create a grpc connection.
201201 if state .correlation == uuid .Nil {
202- conn , serverInfo , err := discoverNode (state .config )
202+ conn , serverInfo , err := discoverNode (state .config , logger )
203203
204204 if err != nil {
205205 atomic .StoreInt32 (closeFlag , 1 )
@@ -234,7 +234,7 @@ func connectionStateMachine(config Configuration, closeFlag *int32, channel chan
234234 if evt .endpoint == nil {
235235 // Means that in the next iteration cycle, the discovery process will start.
236236 state .correlation = uuid .Nil
237- log . Printf ( "[info] Starting a new discovery process" )
237+ logger . info ( "starting a new discovery process" )
238238 continue
239239 }
240240
@@ -243,18 +243,18 @@ func connectionStateMachine(config Configuration, closeFlag *int32, channel chan
243243 state .connection = nil
244244 }
245245
246- log . Printf ( "[info] Connecting to leader node %s ..." , evt .endpoint .String ())
246+ logger . info ( " Connecting to leader node %s ..." , evt .endpoint .String ())
247247 conn , err := createGrpcConnection (& state .config , evt .endpoint .String ())
248248
249249 if err != nil {
250- log . Printf ( "[error] exception when connecting to suggested node %s" , evt .endpoint .String ())
250+ logger . error ( " exception when connecting to suggested node %s" , evt .endpoint .String ())
251251 state .correlation = uuid .Nil
252252 continue
253253 }
254254
255255 serverInfo , err := getSupportedMethods (context .Background (), & state .config , conn )
256256 if err != nil {
257- log . Printf ( "[error] exception when fetching server features from suggested node %s: %v" , evt .endpoint .String (), err )
257+ logger . error ( " exception when fetching server features from suggested node %s: %v" , evt .endpoint .String (), err )
258258 state .correlation = uuid .Nil
259259 continue
260260 }
@@ -263,7 +263,7 @@ func connectionStateMachine(config Configuration, closeFlag *int32, channel chan
263263 state .connection = conn
264264 state .serverInfo = serverInfo
265265
266- log . Printf ( "[info] Successfully connected to leader node %s" , evt .endpoint .String ())
266+ logger . info ( "successfully connected to leader node %s" , evt .endpoint .String ())
267267 }
268268 }
269269 }
@@ -432,7 +432,7 @@ func allowedNodeState() []gossipApi.MemberInfo_VNodeState {
432432 }
433433}
434434
435- func discoverNode (conf Configuration ) (* grpc.ClientConn , * ServerInfo , error ) {
435+ func discoverNode (conf Configuration , logger * logger ) (* grpc.ClientConn , * ServerInfo , error ) {
436436 var connection * grpc.ClientConn = nil
437437 var serverInfo * ServerInfo = nil
438438 var err error
@@ -461,12 +461,12 @@ func discoverNode(conf Configuration) (*grpc.ClientConn, *ServerInfo, error) {
461461
462462 for attempt < conf .MaxDiscoverAttempts {
463463 attempt += 1
464- log . Printf ( "[info] discovery attempt %v/%v" , attempt , conf .MaxDiscoverAttempts )
464+ logger . info ( " discovery attempt %v/%v" , attempt , conf .MaxDiscoverAttempts )
465465 for _ , candidate := range candidates {
466- log . Printf ( "[debug] trying candidate '%s'..." , candidate )
466+ logger . debug ( " trying candidate '%s'..." , candidate )
467467 connection , err = createGrpcConnection (& conf , candidate )
468468 if err != nil {
469- log . Printf ( "[warn] Error when creating a grpc connection for candidate %s: %v" , candidate , err )
469+ logger . warn ( "error when creating a grpc connection for candidate %s: %v" , candidate , err )
470470
471471 continue
472472 }
@@ -478,7 +478,7 @@ func discoverNode(conf Configuration) (*grpc.ClientConn, *ServerInfo, error) {
478478
479479 s , ok := status .FromError (err )
480480 if ! ok || (s != nil && s .Code () != codes .OK ) {
481- log . Printf ( "[warn] Error when reading gossip from candidate %s: %v" , candidate , err )
481+ logger . warn ( "error when reading gossip from candidate %s: %v" , candidate , err )
482482 cancel ()
483483 continue
484484 }
@@ -488,37 +488,37 @@ func discoverNode(conf Configuration) (*grpc.ClientConn, *ServerInfo, error) {
488488 selected , err := pickBestCandidate (info , conf .NodePreference )
489489
490490 if err != nil {
491- log . Printf ( "[warn] Eror when picking best candidate out of %s gossip response: %v" , candidate , err )
491+ logger . warn ( "error when picking best candidate out of %s gossip response: %v" , candidate , err )
492492 continue
493493 }
494494
495495 selectedAddress := fmt .Sprintf ("%s:%d" , selected .GetHttpEndPoint ().GetAddress (), selected .GetHttpEndPoint ().GetPort ())
496- log . Printf ( "[info] Best candidate found. %s (%s)" , selectedAddress , selected .State .String ())
496+ logger . info ( "best candidate found. %s (%s)" , selectedAddress , selected .State .String ())
497497 if candidate != selectedAddress {
498498 candidate = selectedAddress
499499 _ = connection .Close ()
500500 connection , err = createGrpcConnection (& conf , selectedAddress )
501501
502502 if err != nil {
503- log . Printf ( "[warn] Error when creating gRPC connection for the selected candidate '%s': %v" , selectedAddress , err )
503+ logger . warn ( "error when creating gRPC connection for the selected candidate '%s': %v" , selectedAddress , err )
504504 continue
505505 }
506506 }
507507 }
508508
509- log . Printf ( "[debug] Attempting node supported features retrieval on '%s'..." , candidate )
509+ logger . debug ( "attempting node supported features retrieval on '%s'..." , candidate )
510510 serverInfo , err = getSupportedMethods (context .Background (), & conf , connection )
511511 if err != nil {
512- log . Printf ( "[warn] Error when creating reading server features from the best candidate '%s': %v" , candidate , err )
512+ logger . warn ( "error when creating reading server features from the best candidate '%s': %v" , candidate , err )
513513 _ = connection .Close ()
514514 connection = nil
515515 continue
516516 }
517517
518518 if serverInfo != nil {
519- log . Printf ( "[debug] Retrieved supported features on node '%s' successfully" , candidate )
519+ logger . debug ( "retrieved supported features on node '%s' successfully" , candidate )
520520 } else {
521- log . Printf ( "[debug] Selected node '%s' doesn't support a supported features endpoint" , candidate )
521+ logger . debug ( "selected node '%s' doesn't support a supported features endpoint" , candidate )
522522 }
523523
524524 break
0 commit comments