Skip to content

Commit

Permalink
Specify the utility of the rpc client WaitGroup wg
Browse files Browse the repository at this point in the history
  • Loading branch information
kerzhner committed Sep 1, 2023
1 parent 737e734 commit bebfe9a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type rpcClient struct {
ledgerChannelUpdates *safesync.Map[chan query.LedgerChannelInfo]
paymentChannelUpdates *safesync.Map[chan query.PaymentChannelInfo]
cancel context.CancelFunc
wg *sync.WaitGroup
routineTracker *sync.WaitGroup
nodeAddress common.Address
logger *slog.Logger
authToken string
Expand All @@ -109,7 +109,7 @@ func NewRpcClient(trans transport.Requester) (RpcClientApi, error) {
ledgerChannelUpdates: &safesync.Map[chan query.LedgerChannelInfo]{},
paymentChannelUpdates: &safesync.Map[chan query.PaymentChannelInfo]{},
cancel: cancel,
wg: &sync.WaitGroup{},
routineTracker: &sync.WaitGroup{},
nodeAddress: common.Address{},
authTokenReady: &sync.WaitGroup{},
logger: slog.Default(),
Expand All @@ -129,7 +129,7 @@ func NewRpcClient(trans transport.Requester) (RpcClientApi, error) {
if err != nil {
return nil, err
}
c.wg.Add(1)
c.routineTracker.Add(1)
go c.subscribeToNotifications(ctx, notificationChan)

c.authTokenReady.Add(1)
Expand Down Expand Up @@ -244,7 +244,7 @@ func (rc *rpcClient) Pay(id types.Destination, amount uint64) (serde.PaymentRequ

func (rc *rpcClient) Close() error {
rc.cancel()
rc.wg.Wait()
rc.routineTracker.Wait()
return rc.transport.Close()
}

Expand All @@ -253,7 +253,7 @@ func (rc *rpcClient) subscribeToNotifications(ctx context.Context, notificationC
for {
select {
case <-ctx.Done():
rc.wg.Done()
rc.routineTracker.Done()
return
case data := <-notificationChan:

Expand Down Expand Up @@ -326,10 +326,10 @@ func waitForAuthorizedRequest[T serde.RequestPayload, U serde.ResponsePayload](r
}

func waitForRequest[T serde.RequestPayload, U serde.ResponsePayload](rc *rpcClient, method serde.RequestMethod, requestData T, authToken string) (U, error) {
rc.wg.Add(1)
defer rc.wg.Done()
rc.routineTracker.Add(1)
defer rc.routineTracker.Done()

res, err := sendRequest[T, U](rc.transport, method, requestData, rc.authToken, rc.logger, rc.wg)
res, err := sendRequest[T, U](rc.transport, method, requestData, rc.authToken, rc.logger, rc.routineTracker)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit bebfe9a

Please sign in to comment.