@@ -20,6 +20,10 @@ const (
20
20
// NotificationTypeReservation is the notification type for reservation
21
21
// notifications.
22
22
NotificationTypeReservation
23
+
24
+ // NotificationTypeStaticLoopInSweepRequest is the notification type for
25
+ // static loop in sweep requests.
26
+ NotificationTypeStaticLoopInSweepRequest
23
27
)
24
28
25
29
// Client is the interface that the notification manager needs to implement in
@@ -79,7 +83,8 @@ func (m *Manager) SubscribeReservations(ctx context.Context,
79
83
80
84
m .addSubscriber (NotificationTypeReservation , sub )
81
85
82
- // Start a goroutine to remove the subscriber when the context is canceled
86
+ // Start a goroutine to remove the subscriber when the context is
87
+ // canceled.
83
88
go func () {
84
89
<- ctx .Done ()
85
90
m .removeSubscriber (NotificationTypeReservation , sub )
@@ -89,6 +94,34 @@ func (m *Manager) SubscribeReservations(ctx context.Context,
89
94
return notifChan
90
95
}
91
96
97
+ // SubscribeStaticLoopInSweepRequests subscribes to the static loop in sweep
98
+ // requests.
99
+ func (m * Manager ) SubscribeStaticLoopInSweepRequests (ctx context.Context ,
100
+ ) <- chan * swapserverrpc.ServerStaticLoopInSweepNotification {
101
+
102
+ notifChan := make (
103
+ chan * swapserverrpc.ServerStaticLoopInSweepNotification , 1 ,
104
+ )
105
+ sub := subscriber {
106
+ subCtx : ctx ,
107
+ recvChan : notifChan ,
108
+ }
109
+
110
+ m .addSubscriber (NotificationTypeStaticLoopInSweepRequest , sub )
111
+
112
+ // Start a goroutine to remove the subscriber when the context is
113
+ // canceled.
114
+ go func () {
115
+ <- ctx .Done ()
116
+ m .removeSubscriber (
117
+ NotificationTypeStaticLoopInSweepRequest , sub ,
118
+ )
119
+ close (notifChan )
120
+ }()
121
+
122
+ return notifChan
123
+ }
124
+
92
125
// Run starts the notification manager. It will keep on running until the
93
126
// context is canceled. It will subscribe to notifications and forward them to
94
127
// the subscribers. On a first successful connection to the server, it will
@@ -160,7 +193,7 @@ func (m *Manager) subscribeNotifications(ctx context.Context,
160
193
for {
161
194
notification , err := notifStream .Recv ()
162
195
if err == nil && notification != nil {
163
- log .Debugf ("Received notification: %v" , notification )
196
+ log .Tracef ("Received notification: %v" , notification )
164
197
m .handleNotification (notification )
165
198
continue
166
199
}
@@ -173,13 +206,13 @@ func (m *Manager) subscribeNotifications(ctx context.Context,
173
206
174
207
// handleNotification handles an incoming notification from the server,
175
208
// forwarding it to the appropriate subscribers.
176
- func (m * Manager ) handleNotification (notification * swapserverrpc.
209
+ func (m * Manager ) handleNotification (ntfn * swapserverrpc.
177
210
SubscribeNotificationsResponse ) {
178
211
179
- switch notification .Notification .(type ) {
180
- case * swapserverrpc.SubscribeNotificationsResponse_ReservationNotification :
212
+ switch ntfn .Notification .(type ) {
213
+ case * swapserverrpc.SubscribeNotificationsResponse_ReservationNotification : // nolint: lll
181
214
// We'll forward the reservation notification to all subscribers.
182
- reservationNtfn := notification .GetReservationNotification ()
215
+ reservationNtfn := ntfn .GetReservationNotification ()
183
216
m .Lock ()
184
217
defer m .Unlock ()
185
218
@@ -189,10 +222,23 @@ func (m *Manager) handleNotification(notification *swapserverrpc.
189
222
190
223
recvChan <- reservationNtfn
191
224
}
225
+ case * swapserverrpc.SubscribeNotificationsResponse_StaticLoopInSweep : // nolint: lll
226
+ // We'll forward the static loop in sweep request to all
227
+ // subscribers.
228
+ staticLoopInSweepRequestNtfn := ntfn .GetStaticLoopInSweep ()
229
+ m .Lock ()
230
+ defer m .Unlock ()
231
+
232
+ for _ , sub := range m .subscribers [NotificationTypeStaticLoopInSweepRequest ] { // nolint: lll
233
+ recvChan := sub .recvChan .(chan * swapserverrpc.
234
+ ServerStaticLoopInSweepNotification )
235
+
236
+ recvChan <- staticLoopInSweepRequestNtfn
237
+ }
192
238
193
239
default :
194
240
log .Warnf ("Received unknown notification type: %v" ,
195
- notification )
241
+ ntfn )
196
242
}
197
243
}
198
244
0 commit comments