Skip to content

Commit

Permalink
Merge pull request #23 from carlaKC/router-htlcsubscription
Browse files Browse the repository at this point in the history
router: add subscribe htlc events to router client
  • Loading branch information
carlaKC authored Nov 17, 2020
2 parents 387423d + fc0e642 commit b9a8c8f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions router_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type RouterClient interface {
// payment update stream and an error stream.
TrackPayment(ctx context.Context, hash lntypes.Hash) (
chan PaymentStatus, chan error, error)

// SubscribeHtlcEvents subscribes to a stream of htlc events from the
// router.
SubscribeHtlcEvents(ctx context.Context) (<-chan *routerrpc.HtlcEvent,
<-chan error, error)
}

// PaymentStatus describe the state of a payment.
Expand Down Expand Up @@ -369,3 +374,48 @@ func marshallHopHint(hint zpay32.HopHint) (*lnrpc.HopHint, error) {
NodeId: nodeID.String(),
}, nil
}

// SubscribeHtlcEvents subscribes to a stream of htlc events from the router.
func (r *routerClient) SubscribeHtlcEvents(ctx context.Context) (
<-chan *routerrpc.HtlcEvent, <-chan error, error) {

stream, err := r.client.SubscribeHtlcEvents(
r.routerKitMac.WithMacaroonAuth(ctx),
&routerrpc.SubscribeHtlcEventsRequest{},
)
if err != nil {
return nil, nil, err
}

// Buffer our error channel by 1 so we don't need to worry about the
// client not listening or shutting down when we send an error.
errChan := make(chan error, 1)
htlcChan := make(chan *routerrpc.HtlcEvent)

go func() {
// Close our error and htlc channel when this loop exits to
// signal that we will no longer be sending results.
defer close(errChan)
defer close(htlcChan)

for {
htlc, err := stream.Recv()
if err != nil {
errChan <- err
return
}

// Send the update to into our events channel, or exit
// if our context has been cancelled.
select {
case htlcChan <- htlc:

case <-ctx.Done():
errChan <- ctx.Err()
return
}
}
}()

return htlcChan, errChan, nil
}

0 comments on commit b9a8c8f

Please sign in to comment.