Skip to content

feat: added link_timeout and demo - #31

Open
matthewtolman wants to merge 1 commit into
CarterLi:asyncfrom
matthewtolman:async
Open

matthewtolman wants to merge 1 commit into
CarterLi:asyncfrom
matthewtolman:async

Conversation

@matthewtolman

Copy link
Copy Markdown

What Changed

Added wrapper for io_uring_prep_link_timeout which allows adding timeouts to I/O operations (e.g. read).

I/O Uring will dispatch two events for a link timeout, one for the original I/O operation (e.g. read) with -ETIMEDOUT when there's a timeout, and another for the timeout itself with code -ETIME on a timeout.

If there isn't a timeout, then two events are still emitted, one for the original I/O operation (e.g. read) with the normal return value, and another for the timeout itself with code -ECANCELLED.

To prevent confusion around doing two co_await or managing two events for a timeout event, I opted to set the timeout to have an empty callback. Since it's using an empty callback and not a coroutine, there is no need to keep the return object "in scope", so I don't return anything. That said, I'm open to feedback on changing the API.

I also added a demo on how to use it. The demo adapts the echo server demo and adds a timeout.

Usage:

// Timeout duration
__kernel_timespec timeout = { .tv_sec = 1, .tv_nsec = 0 };

// Queue an operation (but don't submit yet)
// Operation MUST be linked to follow up operation
auto task = service.read(fd, buff, buffLen, -1, IOSQE_IO_LINK);

// Link a timeout to our queued I/O operation
// Since this has an internal callback, we don't save it or see the event on the CQE
service.link_timeout(&timeout);

// Now we can dispatch the task
auto res = co_await task;

// check for errors
if (res < 0) {
  // See if we got a timeout
  if (res == -ETIMEDOUT) {
    fmt::println("Timeout detected!");
  }
  co_return;
}

// process bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant