@@ -208,33 +208,47 @@ type ClientSubscription struct {
208208 channel reflect.Value
209209 namespace string
210210 subid string
211- in chan json.RawMessage
212211
213- quitOnce sync.Once // ensures quit is closed once
214- quit chan struct {} // quit is closed when the subscription exits
215- errOnce sync.Once // ensures err is closed once
216- err chan error
212+ // The in channel receives notification values from client dispatcher.
213+ in chan json.RawMessage
214+
215+ // The error channel receives the error from the forwarding loop.
216+ // It is closed by Unsubscribe.
217+ err chan error
218+ errOnce sync.Once
219+
220+ // Closing of the subscription is requested by sending on 'quit'. This is handled by
221+ // the forwarding loop, which closes 'forwardDone' when it has stopped sending to
222+ // sub.channel. Finally, 'unsubDone' is closed after unsubscribing on the server side.
223+ quit chan error
224+ forwardDone chan struct {}
225+ unsubDone chan struct {}
217226}
218227
228+ // This is the sentinel value sent on sub.quit when Unsubscribe is called.
229+ var errUnsubscribed = errors .New ("unsubscribed" )
230+
219231func newClientSubscription (c * Client , namespace string , channel reflect.Value ) * ClientSubscription {
220232 sub := & ClientSubscription {
221- client : c ,
222- namespace : namespace ,
223- etype : channel .Type ().Elem (),
224- channel : channel ,
225- quit : make (chan struct {}),
226- err : make (chan error , 1 ),
227- in : make (chan json.RawMessage ),
233+ client : c ,
234+ namespace : namespace ,
235+ etype : channel .Type ().Elem (),
236+ channel : channel ,
237+ in : make (chan json.RawMessage ),
238+ quit : make (chan error ),
239+ forwardDone : make (chan struct {}),
240+ unsubDone : make (chan struct {}),
241+ err : make (chan error , 1 ),
228242 }
229243 return sub
230244}
231245
232246// Err returns the subscription error channel. The intended use of Err is to schedule
233247// resubscription when the client connection is closed unexpectedly.
234248//
235- // The error channel receives a value when the subscription has ended due
236- // to an error. The received error is nil if Close has been called
237- // on the underlying client and no other error has occurred.
249+ // The error channel receives a value when the subscription has ended due to an error. The
250+ // received error is nil if Close has been called on the underlying client and no other
251+ // error has occurred.
238252//
239253// The error channel is closed when Unsubscribe is called on the subscription.
240254func (sub * ClientSubscription ) Err () <- chan error {
@@ -244,49 +258,71 @@ func (sub *ClientSubscription) Err() <-chan error {
244258// Unsubscribe unsubscribes the notification and closes the error channel.
245259// It can safely be called more than once.
246260func (sub * ClientSubscription ) Unsubscribe () {
247- sub .quitWithError (true , nil )
248- sub .errOnce .Do (func () { close (sub .err ) })
249- }
250-
251- func (sub * ClientSubscription ) quitWithError (unsubscribeServer bool , err error ) {
252- sub .quitOnce .Do (func () {
253- // The dispatch loop won't be able to execute the unsubscribe call
254- // if it is blocked on deliver. Close sub.quit first because it
255- // unblocks deliver.
256- close (sub .quit )
257- if unsubscribeServer {
258- sub .requestUnsubscribe ()
259- }
260- if err != nil {
261- if err == ErrClientQuit {
262- err = nil // Adhere to subscription semantics.
263- }
264- sub .err <- err
261+ sub .errOnce .Do (func () {
262+ select {
263+ case sub .quit <- errUnsubscribed :
264+ <- sub .unsubDone
265+ case <- sub .unsubDone :
265266 }
267+ close (sub .err )
266268 })
267269}
268270
271+ // deliver is called by the client's message dispatcher to send a notification value.
269272func (sub * ClientSubscription ) deliver (result json.RawMessage ) (ok bool ) {
270273 select {
271274 case sub .in <- result :
272275 return true
273- case <- sub .quit :
276+ case <- sub .forwardDone :
274277 return false
275278 }
276279}
277280
278- func (sub * ClientSubscription ) start () {
279- sub .quitWithError (sub .forward ())
281+ // close is called by the client's message dispatcher when the connection is closed.
282+ func (sub * ClientSubscription ) close (err error ) {
283+ select {
284+ case sub .quit <- err :
285+ case <- sub .forwardDone :
286+ }
287+ }
288+
289+ // run is the forwarding loop of the subscription. It runs in its own goroutine and
290+ // is launched by the client's handler after the subscription has been created.
291+ func (sub * ClientSubscription ) run () {
292+ defer close (sub .unsubDone )
293+
294+ unsubscribe , err := sub .forward ()
295+
296+ // The client's dispatch loop won't be able to execute the unsubscribe call if it is
297+ // blocked in sub.deliver() or sub.close(). Closing forwardDone unblocks them.
298+ close (sub .forwardDone )
299+
300+ // Call the unsubscribe method on the server.
301+ if unsubscribe {
302+ sub .requestUnsubscribe ()
303+ }
304+
305+ // Send the error.
306+ if err != nil {
307+ if err == ErrClientQuit {
308+ // ErrClientQuit gets here when Client.Close is called. This is reported as a
309+ // nil error because it's not an error, but we can't close sub.err here.
310+ err = nil
311+ }
312+ sub .err <- err
313+ }
280314}
281315
316+ // forward is the forwarding loop. It takes in RPC notifications and sends them
317+ // on the subscription channel.
282318func (sub * ClientSubscription ) forward () (unsubscribeServer bool , err error ) {
283319 cases := []reflect.SelectCase {
284320 {Dir : reflect .SelectRecv , Chan : reflect .ValueOf (sub .quit )},
285321 {Dir : reflect .SelectRecv , Chan : reflect .ValueOf (sub .in )},
286322 {Dir : reflect .SelectSend , Chan : sub .channel },
287323 }
288324 buffer := list .New ()
289- defer buffer . Init ()
325+
290326 for {
291327 var chosen int
292328 var recv reflect.Value
@@ -301,7 +337,15 @@ func (sub *ClientSubscription) forward() (unsubscribeServer bool, err error) {
301337
302338 switch chosen {
303339 case 0 : // <-sub.quit
304- return false , nil
340+ if ! recv .IsNil () {
341+ err = recv .Interface ().(error )
342+ }
343+ if err == errUnsubscribed {
344+ // Exiting because Unsubscribe was called, unsubscribe on server.
345+ return true , nil
346+ }
347+ return false , err
348+
305349 case 1 : // <-sub.in
306350 val , err := sub .unmarshal (recv .Interface ().(json.RawMessage ))
307351 if err != nil {
@@ -311,6 +355,7 @@ func (sub *ClientSubscription) forward() (unsubscribeServer bool, err error) {
311355 return true , ErrSubscriptionQueueOverflow
312356 }
313357 buffer .PushBack (val )
358+
314359 case 2 : // sub.channel<-
315360 cases [2 ].Send = reflect.Value {} // Don't hold onto the value.
316361 buffer .Remove (buffer .Front ())
0 commit comments