88 "sync"
99 "time"
1010
11+ "github.com/btcsuite/btclog"
1112 "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1213 "github.com/lightninglabs/lightning-node-connect/gbn"
1314 "github.com/lightninglabs/lightning-node-connect/hashmailrpc"
@@ -124,13 +125,15 @@ type ClientConn struct {
124125 quit chan struct {}
125126 cancel func ()
126127 closeOnce sync.Once
128+
129+ log btclog.Logger
127130}
128131
129132// NewClientConn creates a new client connection with the given receive and send
130133// session identifiers. The context given as the first parameter will be used
131134// throughout the connection lifetime.
132135func NewClientConn (ctx context.Context , sid [64 ]byte , serverHost string ,
133- client hashmailrpc.HashMailClient ,
136+ client hashmailrpc.HashMailClient , logger btclog. Logger ,
134137 onNewStatus func (status ClientStatus )) (* ClientConn , error ) {
135138
136139 receiveSID := GetSID (sid , true )
@@ -141,7 +144,7 @@ func NewClientConn(ctx context.Context, sid [64]byte, serverHost string,
141144 sendSID : sendSID [:],
142145 }
143146
144- log .Debugf ("New client conn, read_stream=%x, write_stream=%x" ,
147+ logger .Debugf ("New conn, read_stream=%x, write_stream=%x" ,
145148 receiveSID [:], sendSID [:])
146149
147150 ctxc , cancel := context .WithCancel (ctx )
@@ -166,6 +169,7 @@ func NewClientConn(ctx context.Context, sid [64]byte, serverHost string,
166169 onNewStatus : onNewStatus ,
167170 quit : make (chan struct {}),
168171 cancel : cancel ,
172+ log : logger ,
169173 }
170174 c .connKit = & connKit {
171175 ctx : ctxc ,
@@ -201,10 +205,11 @@ func RefreshClientConn(ctx context.Context, c *ClientConn) (*ClientConn,
201205 c .statusMu .Lock ()
202206 defer c .statusMu .Unlock ()
203207
204- log .Debugf ("Refreshing client conn, read_stream=%x, write_stream=%x" ,
208+ c . log .Debugf ("Refreshing client conn, read_stream=%x, write_stream=%x" ,
205209 c .receiveSID [:], c .sendSID [:])
206210
207211 cc := & ClientConn {
212+ log : c .log ,
208213 transport : c .transport .Refresh (),
209214 status : ClientStatusNotConnected ,
210215 onNewStatus : c .onNewStatus ,
@@ -299,7 +304,7 @@ func (c *ClientConn) recv(ctx context.Context) ([]byte, error) {
299304 return nil , err
300305 }
301306
302- log .Debugf ("Client: got failure on receive " +
307+ c . log .Debugf ("Got failure on receive " +
303308 "socket/stream, re-trying: %v" , err )
304309
305310 c .setStatus (errStatus )
@@ -343,8 +348,8 @@ func (c *ClientConn) send(ctx context.Context, payload []byte) error {
343348 return err
344349 }
345350
346- log .Debugf ("Client: got failure on send " +
347- "socket/stream, re-trying: %v" , err )
351+ c . log .Debugf ("Got failure on send socket/stream, " +
352+ "re-trying: %v" , err )
348353
349354 c .setStatus (errStatus )
350355 c .createSendMailBox (ctx , retryWait )
@@ -377,13 +382,14 @@ func (c *ClientConn) createReceiveMailBox(ctx context.Context,
377382 waiter .Wait ()
378383
379384 if err := c .transport .ConnectReceive (ctx ); err != nil {
380- log .Errorf ("Client: error connecting to receive " +
385+ c . log .Errorf ("Error connecting to receive " +
381386 "socket/stream: %v" , err )
382387
383388 continue
384389 }
385390
386- log .Debugf ("Client: receive mailbox initialized" )
391+ c .log .Debugf ("Receive mailbox initialized" )
392+
387393 return
388394 }
389395}
@@ -406,14 +412,17 @@ func (c *ClientConn) createSendMailBox(ctx context.Context,
406412
407413 waiter .Wait ()
408414
409- log .Debugf ("Client: Attempting to create send socket/stream" )
415+ c .log .Debugf ("Attempting to create send socket/stream" )
416+
410417 if err := c .transport .ConnectSend (ctx ); err != nil {
411- log .Debugf ("Client: error connecting to send " +
418+ c . log .Debugf ("Error connecting to send " +
412419 "stream/socket %v" , err )
420+
413421 continue
414422 }
415423
416- log .Debugf ("Client: Connected to send socket/stream" )
424+ c .log .Debugf ("Connected to send socket/stream" )
425+
417426 return
418427 }
419428}
@@ -460,29 +469,33 @@ func (c *ClientConn) SetSendTimeout(timeout time.Duration) {
460469func (c * ClientConn ) Close () error {
461470 var returnErr error
462471 c .closeOnce .Do (func () {
463- log .Debugf ("Closing client connection" )
472+ c . log .Debugf ("Closing connection" )
464473
465474 if c .gbnConn != nil {
466475 if err := c .gbnConn .Close (); err != nil {
467- log .Debugf ("Error closing gbn connection: %v" ,
476+ c . log .Debugf ("Error closing gbn connection: %v" ,
468477 err )
469478
470479 returnErr = err
471480 }
472481 }
473482
474483 c .receiveMu .Lock ()
475- log .Debugf ("closing receive stream/socket" )
484+ c . log .Debugf ("Closing receive stream/socket" )
476485 if err := c .transport .CloseReceive (); err != nil {
477- log .Errorf ("Error closing receive stream/socket: %v" , err )
486+ c .log .Errorf ("Error closing receive stream/socket: %v" ,
487+ err )
488+
478489 returnErr = err
479490 }
480491 c .receiveMu .Unlock ()
481492
482493 c .sendMu .Lock ()
483- log .Debugf ("closing send stream/socket" )
494+ c . log .Debugf ("Closing send stream/socket" )
484495 if err := c .transport .CloseSend (); err != nil {
485- log .Errorf ("Error closing send stream/socket: %v" , err )
496+ c .log .Errorf ("Error closing send stream/socket: %v" ,
497+ err )
498+
486499 returnErr = err
487500 }
488501 c .sendMu .Unlock ()
0 commit comments