website/docs/dev/collection-broker.md
The role of the collection broker is to facilitate blocking commands on collections in Garnet.
A blocking command is a command that returns immediately if an item in a collection is available or blocks the client until an item is avilable (see: BLPOP, BLMOVE etc).
The broker runs its main loop on a dedicated Task whenever there are active clients waiting on collection items.
CollectionItemBroker.GetCollectionItemAsync
brokerEventsQueue).CollectionItemObserver object is created and an event of type NewObserverEvent is pushed into the event queue.CollectionItemBroker.HandleCollectionUpdate\
CollectionUpdatedEvent is pushed into the event queue.CollectionItemBroker.Start) continuously listens on an AsyncQueue for new incoming events and synchronously calls HandleBrokerEvent for each new event.\
NewObserverEvent, InitializeObserver is called. InitializeObserver takes an array of keys and checks the collection values for available item in the order in which they where specified by the client.
If an item is found, the observer is updated which sets the result and signals the semaphore to release the awaiting thread. If no item is found, the observer is being added to each key's observer queue.CollectionUpdatedEvent, TryAssignItemFromKey is called. This method gets the key's observer queue and tries to assign the next available item from the collection stored at key to the next observer.
If an available item was indeed found, the observer is updated which sets the result and signals the semaphore to release the awaiting thread.RespServerSession that has an active observer is disposed, its Dispose method will call CollectionItemBroker.HandleSessionDisposed, which in turn will update the observer and signal the semaphore awaiter to stop. Once the observer's status is changed to SessionDisposed, the main loop will not assign it any item.