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

Wait Timeouts and Message Expirations for Channels #2

Closed
marcoonroad opened this issue Jan 31, 2019 · 2 comments
Closed

Wait Timeouts and Message Expirations for Channels #2

marcoonroad opened this issue Jan 31, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@marcoonroad
Copy link
Owner

To avoid blocking forever (while waiting for a message) and to provide a mechanism resembling a cache, timeouts and expirations are good abstractions over channels. The API will be the following, thus:

const message = await sporadicChannels.receive(channel, timeout)

Where timeout is optional, and:

const wasReceived = await sporadicChannels.send(channel, 'Hi, folks!', expiration)

Where expiration is optional too. The receive operation will fail/throw a Timeout error whenever a timeout occurs. For the case of send, it doesn't fails/throws (as is the case of receive), but wasReceived will be false whenever the expiration triggers and the message is erased (and thus, not received). Both timeout and expiration are units of time at ms (units compatible with JavaScript's setTimeout and setInterval).

Further discussions about implementation tradeoffs may be needed, but this API sketch is clean enough to not be changed later.

@marcoonroad marcoonroad added the enhancement New feature or request label Jan 31, 2019
@marcoonroad
Copy link
Owner Author

marcoonroad commented Jan 31, 2019

To check directly if there's any message on channel, the client would just call:

let message = null
try {
  message = await sporadicChannels.receive(channel, 0)
} catch (reason) {
  // handles the case of empty channel here...
}

BUMP:
(It would be great to also cover that example on test suite.)

UPDATE:
Already covered, see:

const result1 = receive(channel, 0) // no block, check if there's a sent message

marcoonroad added a commit that referenced this issue Feb 17, 2019
Signed-off-by: Marco Aurélio da Silva <marcoonroad@gmail.com>
marcoonroad added a commit that referenced this issue Feb 17, 2019
Signed-off-by: Marco Aurélio da Silva <marcoonroad@gmail.com>
marcoonroad added a commit that referenced this issue Feb 17, 2019
…future (see issue #2)

Signed-off-by: Marco Aurélio da Silva <marcoonroad@gmail.com>
@marcoonroad
Copy link
Owner Author

marcoonroad commented Feb 17, 2019

BUMP:

Additional APIs provided:

const wasReceived = await channels.sendAfter(delayA, channel, msg, expiration)

...

const msg = await channels.receiveAfter(delayB, channel, timeout)

Both functions trigger their respective immediate counterparts scheduled on the future. Internally it's a setTimeout + wrapper Promise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant