Change Listener and Change Waiter - adamcfraser/cbnotes GitHub Wiki

Every client changes feed running in continuous mode (wait=true) instantiates a changeWaiter (at the start of MultiChangesFeed in changes.go).

ChangeWaiter is created with set of waitKeys, made up of the user's channels and user/role doc ids for the user.

At the end of each changes feed loop iteration, it calls Wait(keys, counter).

Wait runs a loop like:

  • get the high counter for (keys). If it doesn't match counter, return the new value
  • if it matches, does a Wait() on the listener sync.Cond tapNotifier.

The result is that Wait(keys, counter) suspends the calling goroutine until a doc comes through in one of the channels, or the user/role doc comes through.

The sync.Cond (tapNotifier) does a Broadcast when Notify(keys) is called.

(which does a wait on listener.tapNotifier).

ChangeListener maintains a set of counters (one per key, where keys are created when a ChangeWaiter does a wait).

On Notify

Calls to Notify(keys):

  • When a user or role doc comes through on the tap feed, calls Notify(docId)
  • change_cache.onChange calls Notify with the set of channels. Called during DocChanged when a new doc comes in on the tap feed, and during the periodic Cleanup function.