Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
fix: ldk broadcaster cleanup channels, add timeout for sending events
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Mar 8, 2024
1 parent 737a35e commit 72864f5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions ldk_event_broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"slices"
"time"

"github.com/getAlby/ldk-node-go/ldk_node"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -48,9 +49,14 @@ func (s *ldkEventBroadcastServer) CancelSubscription(channel chan *ldk_node.Even
func (s *ldkEventBroadcastServer) serve(ctx context.Context) {
defer func() {
for _, listener := range s.listeners {
if listener != nil {
func() {
defer func() {
if r := recover(); r != nil {
s.logger.Errorf("Failed to close channel: %v", r)
}
}()
close(listener)
}
}()
}
}()

Expand All @@ -69,7 +75,7 @@ func (s *ldkEventBroadcastServer) serve(ctx context.Context) {
}
}
case event := <-s.source:
s.logger.Debugf("Sending LDK event +%v to %d listeners", *event, len(s.listeners))
s.logger.Debugf("Sending LDK event %+v to %d listeners", *event, len(s.listeners))
for _, listener := range s.listeners {
func() {
// if we fail to send the event to the listener it was probably closed
Expand All @@ -78,7 +84,13 @@ func (s *ldkEventBroadcastServer) serve(ctx context.Context) {
s.logger.Errorf("Failed to send event to listener: %v", r)
}
}()
listener <- event

select {
case listener <- event:
s.logger.Debugln("sent event to listener")
case <-time.After(5 * time.Second):
s.logger.Errorf("Timeout sending %+v to listener", *event)
}
}()
}
}
Expand Down

0 comments on commit 72864f5

Please sign in to comment.