Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3pt] Document net.box watchers #2409

Closed
TarantoolBot opened this issue Nov 8, 2021 · 0 comments · Fixed by #2858
Closed

[3pt] Document net.box watchers #2409

TarantoolBot opened this issue Nov 8, 2021 · 0 comments · Fixed by #2858
Assignees
Labels
feature A new functionality reference [location] Tarantool manual, Reference part server [area] Task relates to Tarantool's server (core) functionality

Comments

@TarantoolBot
Copy link
Collaborator

TarantoolBot commented Nov 8, 2021

Document conn:watch method in net.box

Product: Tarantool
Root document: https://www.tarantool.io/en/doc/latest/reference/reference_lua/net_box/
SME: @ locker
Peer reviewer: @

Details

Using the new watch method of a net.box connection, one can subscribe
to events broadcasted by a remote host. The method has the same syntax
as the box.watch() function, which is used for subscribing to events
locally. It takes a key name (string) to subscribe to and a callback to
invoke when the key value is updated. It returns a watcher handle that
can be used to unregister the watcher. Note, garbage collection of a
watcher handle doesnt result in unregistering the watcher so it's okay
to discard the result of box.watch if the watcher is never going to be
unregistered.

A watcher callback is first invoked unconditionally after the watcher
registration. Subsequent invocations are triggered by box.broadcast()
called on the remote host. A watcher callback is passed the name of the
key the watcher was subscribed to and the current key value. A watcher
callback is always executed in a new fiber so it's okay to yield inside
it. A watcher callback never runs in parallel with itself: if the key
to which a watcher is subscribed is updated while the watcher callback
is running, the callback will be invoked again with the new value as
soon as it returns.

Watchers survive reconnect (see reconnect_after connection option):
all registered watchers are automatically resubscribed as soon as the
connection is reestablished.

If a remote host supports watchers, the 'watchers' key will be set in
connection's peer_protocol_features.

Example usage:

  • Server:
    -- Broadcast value 123 for key 'foo'.
    box.broadcast('foo', 123)
  • Client:
    conn = net.box.connect(URI)
    -- Subscribe to updates of key 'foo'.
    w = conn:watch('foo', function(key, value)
        assert(key == 'foo')
        -- do something with value
    end)
    -- Unregister the watcher when it's no longer needed.
    w:unregister()

Requested by @locker in tarantool/tarantool@eb7712e.

@xuniq xuniq added feature A new functionality reference [location] Tarantool manual, Reference part server [area] Task relates to Tarantool's server (core) functionality labels Nov 18, 2021
@xuniq xuniq added this to the Estimate [@xuniq] milestone Nov 18, 2021
@xuniq xuniq changed the title Document net.box watchers [3pt?] Document net.box watchers Nov 18, 2021
@xuniq xuniq removed this from the Estimate [@xuniq] milestone Nov 26, 2021
@xuniq xuniq changed the title [3pt?] Document net.box watchers [3pt] Document net.box watchers Dec 10, 2021
@xuniq xuniq added this to the box.watch/box.broadcast milestone Dec 10, 2021
@veod32 veod32 modified the milestones: box.watch/box.broadcast, net.box Jan 13, 2022
@xuniq xuniq self-assigned this Jun 23, 2022
@xuniq xuniq added 1sp and removed 3sp labels Jul 6, 2022
p7nov pushed a commit that referenced this issue Aug 11, 2022
Fixes #2551
Fixes #2409
Fixes #2407

* Add new functions to module box
* Update watch and broadcast methods
* Add System events section
* Add glossary
* Proofread text and add links

Written by Kseniia Antonova
Reviewed by Vladimir Davydov and Dmitry Oboukhov
Proofread by Patience Daur

Co-authored-by: Kseniia Antonova <xuniq.is.here@gmail.com>
Co-authored-by: Patience Daur <patiencedaur@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new functionality reference [location] Tarantool manual, Reference part server [area] Task relates to Tarantool's server (core) functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants