Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router: add subscribe htlc events to router client #23

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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():
carlaKC marked this conversation as resolved.
Show resolved Hide resolved
errChan <- ctx.Err()
return
}
}
}()

return htlcChan, errChan, nil
}