|
| 1 | +package notifications |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/lightninglabs/loop/swapserverrpc" |
| 9 | + "google.golang.org/grpc" |
| 10 | +) |
| 11 | + |
| 12 | +type NotificationType int |
| 13 | + |
| 14 | +const ( |
| 15 | + NotificationTypeUnknown NotificationType = iota |
| 16 | + NotificationTypeReservation |
| 17 | +) |
| 18 | + |
| 19 | +type NotificationsClient interface { |
| 20 | + SubscribeNotifications(ctx context.Context, |
| 21 | + in *swapserverrpc.SubscribeNotificationsRequest, |
| 22 | + opts ...grpc.CallOption) ( |
| 23 | + swapserverrpc.SwapServer_SubscribeNotificationsClient, error) |
| 24 | +} |
| 25 | + |
| 26 | +// Config contains all the services that the notification manager needs to |
| 27 | +// operate. |
| 28 | +type Config struct { |
| 29 | + // Client is the client used to communicate with the swap server. |
| 30 | + Client NotificationsClient |
| 31 | + |
| 32 | + // FetchL402 is the function used to fetch the l402 token. |
| 33 | + FetchL402 func(context.Context) error |
| 34 | +} |
| 35 | + |
| 36 | +// Manager is a manager for notifications that the swap server sends to the |
| 37 | +// client. |
| 38 | +type Manager struct { |
| 39 | + cfg *Config |
| 40 | + |
| 41 | + hasL402 bool |
| 42 | + |
| 43 | + subscribers map[NotificationType][]subscriber |
| 44 | + sync.Mutex |
| 45 | +} |
| 46 | + |
| 47 | +// NewManager creates a new notification manager. |
| 48 | +func NewManager(cfg *Config) *Manager { |
| 49 | + return &Manager{ |
| 50 | + cfg: cfg, |
| 51 | + subscribers: make(map[NotificationType][]subscriber), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +type subscriber struct { |
| 56 | + subCtx context.Context |
| 57 | + recvChan interface{} |
| 58 | +} |
| 59 | + |
| 60 | +// SubscribeReservations subscribes to the reservation notifications. |
| 61 | +func (m *Manager) SubscribeReservations(ctx context.Context, |
| 62 | +) <-chan *swapserverrpc.ServerReservationNotification { |
| 63 | + |
| 64 | + // We'll create a channel that we'll use to send the notifications to the |
| 65 | + // caller. |
| 66 | + notifChan := make( |
| 67 | + chan *swapserverrpc.ServerReservationNotification, //nolint:lll |
| 68 | + ) |
| 69 | + sub := subscriber{ |
| 70 | + subCtx: ctx, |
| 71 | + recvChan: notifChan, |
| 72 | + } |
| 73 | + |
| 74 | + m.Lock() |
| 75 | + m.subscribers[NotificationTypeReservation] = append( |
| 76 | + m.subscribers[NotificationTypeReservation], |
| 77 | + sub, |
| 78 | + ) |
| 79 | + m.Unlock() |
| 80 | + |
| 81 | + return notifChan |
| 82 | +} |
| 83 | + |
| 84 | +// Run starts the notification manager. |
| 85 | +func (n *Manager) Run(ctx context.Context, readyChan chan<- struct{}) error { |
| 86 | + // In order to create a valid l402 we first are going to call |
| 87 | + // the FetchL402 method. As a client might not have outbound capacity |
| 88 | + // yet, we'll retry until we get a valid response. |
| 89 | + if !n.hasL402 { |
| 90 | + n.fetchL402(ctx) |
| 91 | + } |
| 92 | + |
| 93 | + var closedChan bool |
| 94 | + |
| 95 | + // Start the notification runloop. |
| 96 | + for { |
| 97 | + // Return if the context has been canceled. |
| 98 | + select { |
| 99 | + case <-ctx.Done(): |
| 100 | + return nil |
| 101 | + default: |
| 102 | + } |
| 103 | + |
| 104 | + callCtx, cancel := context.WithCancel(ctx) |
| 105 | + notifStream, err := n.cfg.Client.SubscribeNotifications( |
| 106 | + callCtx, &swapserverrpc.SubscribeNotificationsRequest{}, |
| 107 | + ) |
| 108 | + if err != nil { |
| 109 | + cancel() |
| 110 | + log.Errorf("Error subscribing to notifications: %v", err) |
| 111 | + continue |
| 112 | + } |
| 113 | + if !closedChan { |
| 114 | + close(readyChan) |
| 115 | + } |
| 116 | + |
| 117 | + log.Debugf("Successfully subscribed to server notifications") |
| 118 | + |
| 119 | + for { |
| 120 | + notification, err := notifStream.Recv() |
| 121 | + if err == nil && notification != nil { |
| 122 | + log.Debugf("Received notification: %v", notification) |
| 123 | + n.handleNotification(notification) |
| 124 | + continue |
| 125 | + } |
| 126 | + log.Errorf("Error receiving "+ |
| 127 | + "notification: %v", err) |
| 128 | + |
| 129 | + cancel() |
| 130 | + // Wait for a bit before reconnecting. |
| 131 | + <-time.After(time.Second * 10) |
| 132 | + break |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// fetchL402 fetches the L402 from the server. This method will keep on |
| 138 | +// retrying until it gets a valid response. |
| 139 | +func (m *Manager) fetchL402(ctx context.Context) { |
| 140 | + // Add a 0 timer so that we initially fetch the L402 immediately. |
| 141 | + timer := time.NewTimer(0) |
| 142 | + for { |
| 143 | + select { |
| 144 | + case <-ctx.Done(): |
| 145 | + return |
| 146 | + |
| 147 | + case <-timer.C: |
| 148 | + err := m.cfg.FetchL402(ctx) |
| 149 | + if err != nil { |
| 150 | + log.Warnf("Error fetching L402: %v", err) |
| 151 | + timer.Reset(time.Second * 10) |
| 152 | + continue |
| 153 | + } |
| 154 | + m.hasL402 = true |
| 155 | + |
| 156 | + return |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// handleNotification handles an incoming notification from the server, |
| 162 | +// forwarding it to the appropriate subscribers. |
| 163 | +func (n *Manager) handleNotification(notification *swapserverrpc. |
| 164 | + SubscribeNotificationsResponse) { |
| 165 | + |
| 166 | + switch notification.Notification.(type) { |
| 167 | + case *swapserverrpc.SubscribeNotificationsResponse_ReservationNotification: |
| 168 | + // We'll forward the reservation notification to all subscribers. |
| 169 | + // Cleaning up any subscribers that have been canceled. |
| 170 | + newSubs := []subscriber{} |
| 171 | + reservationNtfn := notification.GetReservationNotification() |
| 172 | + for _, sub := range n.subscribers[NotificationTypeReservation] { |
| 173 | + recvChan := sub.recvChan.(chan *swapserverrpc. |
| 174 | + ServerReservationNotification) |
| 175 | + |
| 176 | + select { |
| 177 | + case <-sub.subCtx.Done(): |
| 178 | + close(recvChan) |
| 179 | + continue |
| 180 | + case recvChan <- reservationNtfn: |
| 181 | + newSubs = append(newSubs, sub) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + n.Lock() |
| 186 | + n.subscribers[NotificationTypeReservation] = newSubs |
| 187 | + n.Unlock() |
| 188 | + |
| 189 | + default: |
| 190 | + log.Warnf("Received unknown notification type: %v", |
| 191 | + notification) |
| 192 | + } |
| 193 | +} |
0 commit comments