This module implements the core asynchronous engine / dispatcher.
For more information, see the Concepts chapter of the guide.
Types
PDispatcher = ref object of PDispatcherBase
- Source Edit
TimerCallback = ref object finishAt*: Moment function*: AsyncCallback
- Source Edit
TrackerBase = ref object of RootRef id*: string dump*: proc (): string {....gcsafe, raises: [].} isLeaked*: proc (): bool {....gcsafe, raises: [].}
- Source Edit
TrackerCounter = object opened*: uint64 closed*: uint64
- Source Edit
Consts
MaxEventsCount = 64
- Source Edit
Procs
proc addReader2(fd: AsyncFD; cb: CallbackFunc; udata: pointer = nil): Result[ void, OSErrorCode] {....raises: [], tags: [].}
- Source Edit
proc addTimer(at: int64; cb: CallbackFunc; udata: pointer = nil) {.inline, ...deprecated: "Use addTimer(Duration, cb, udata)", raises: [], tags: [RootEffect].}
- Source Edit
proc addTimer(at: Moment; cb: CallbackFunc; udata: pointer = nil) {.inline, ...deprecated: "Use setTimer/clearTimer instead", raises: [], tags: [RootEffect].}
- cb to be called at the given absolute timestamp at. You can also pass udata to callback. Source Edit Arrange for the callback
proc addTimer(at: uint64; cb: CallbackFunc; udata: pointer = nil) {.inline, ...deprecated: "Use addTimer(Duration, cb, udata)", raises: [], tags: [RootEffect].}
- Source Edit
proc addTracker[T](id: string; tracker: T) {. ...deprecated: "Please use trackCounter facility instead", raises: [].}
- tracker object to current thread dispatcher with identifier id. Source Edit Add new
proc addWriter2(fd: AsyncFD; cb: CallbackFunc; udata: pointer = nil): Result[ void, OSErrorCode] {....raises: [], tags: [].}
- Source Edit
proc callIdle(acb: AsyncCallback) {....raises: [], tags: [].}
-
Schedule cbproc to be called when there no pending network events available.
WARNING! Despite the name, "idle" callbacks called on every loop iteration if there no network events available, not when the loop is actually "idle".
Source Edit proc callIdle(cbproc: CallbackFunc) {....raises: [], tags: [RootEffect].}
- Source Edit
proc callIdle(cbproc: CallbackFunc; data: pointer) {....raises: [], tags: [].}
-
Schedule cbproc to be called when there no pending network events available.
WARNING! Despite the name, "idle" callbacks called on every loop iteration if there no network events available, not when the loop is actually "idle".
Source Edit proc callSoon(acb: AsyncCallback) {....raises: [], tags: [].}
- Schedule cbproc to be called as soon as possible. The callback is called when control returns to the event loop. Source Edit
proc callSoon(cbproc: CallbackFunc) {....raises: [], tags: [RootEffect].}
- Source Edit
proc callSoon(cbproc: CallbackFunc; data: pointer) {....gcsafe, raises: [], tags: [].}
- Schedule cbproc to be called as soon as possible. The callback is called when control returns to the event loop. Source Edit
proc clearTimer(timer: TimerCallback) {.inline, ...raises: [], tags: [].}
- Source Edit
proc closeHandle(fd: AsyncFD; aftercb: CallbackFunc = nil) {....raises: [], tags: [].}
- Source Edit
proc closeSocket(fd: AsyncFD; aftercb: CallbackFunc = nil) {....raises: [], tags: [].}
- Source Edit
func getAsyncTimestamp(a: Duration): auto {.inline, ...raises: [], tags: [].}
-
Return rounded up value of duration with milliseconds resolution.
This function also take care on int32 overflow, because Linux and Windows accepts signed 32bit integer as timeout.
Source Edit proc getGlobalDispatcher(): PDispatcher {....gcsafe, deprecated: "Use getThreadDispatcher() instead", raises: [], tags: [].}
- Source Edit
proc getThreadDispatcher(): PDispatcher {....raises: [], tags: [].}
- Returns current thread's dispatcher instance. Source Edit
proc getTracker(id: string): TrackerBase {. ...deprecated: "Please use getTrackerCounter() instead", raises: [], tags: [].}
- tracker from current thread dispatcher using identifier id. Source Edit Get
proc getTrackerCounter(name: string): TrackerCounter {.noinit, ...raises: [], tags: [].}
- Return value of counter with name name. Source Edit
proc internalCallTick(acb: AsyncCallback) {....raises: [], tags: [].}
- Schedule cbproc to be called after all scheduled callbacks, but only when OS system queue finished processing events. Source Edit
proc internalCallTick(cbproc: CallbackFunc) {....raises: [], tags: [].}
- Source Edit
proc internalCallTick(cbproc: CallbackFunc; data: pointer) {....raises: [], tags: [].}
- Schedule cbproc to be called after all scheduled callbacks when OS system queue processing is done. Source Edit
proc isCounterLeaked(name: string): bool {.noinit, ...raises: [], tags: [].}
- Returns true if leak is detected, number of opened not equal to number of closed requests. Source Edit
proc newDispatcher(): PDispatcher {....raises: [], tags: [].}
- Source Edit
proc poll() {....raises: [], tags: [].}
-
Perform single asynchronous step, processing timers and completing tasks. Blocks until at least one event has completed.
Exceptions raised during async task exection are stored as outcome in the corresponding Future - poll itself does not raise.
Source Edit proc raiseAsDefect(exc: ref Exception; msg: string) {.noreturn, noinline, ...raises: [], tags: [].}
- Source Edit
proc raiseOsDefect(error: OSErrorCode; msg = "") {.noreturn, noinline, ...raises: [], tags: [].}
- Source Edit
proc removeReader2(fd: AsyncFD): Result[void, OSErrorCode] {....raises: [], tags: [].}
- Source Edit
proc removeTimer(at: int64; cb: CallbackFunc; udata: pointer = nil) {.inline, ...deprecated: "Use removeTimer(Duration, cb, udata)", raises: [], tags: [RootEffect].}
- Source Edit
proc removeTimer(at: Moment; cb: CallbackFunc; udata: pointer = nil) {. ...raises: [], tags: [].}
- Remove timer callback cb with absolute timestamp at from waiting queue. Source Edit
proc removeTimer(at: uint64; cb: CallbackFunc; udata: pointer = nil) {.inline, ...deprecated: "Use removeTimer(Duration, cb, udata)", raises: [], tags: [RootEffect].}
- Source Edit
proc removeWriter2(fd: AsyncFD): Result[void, OSErrorCode] {....raises: [], tags: [].}
- Source Edit
proc runForever() {....raises: [], tags: [].}
- Begins a never ending global dispatcher poll loop. Raises different exceptions depending on the platform. Source Edit
proc setGlobalDispatcher(disp: PDispatcher) {....gcsafe, deprecated: "Use setThreadDispatcher() instead", raises: [], tags: [].}
- Source Edit
proc setThreadDispatcher(disp: PDispatcher) {....raises: [], tags: [].}
- Set current thread's dispatcher instance to disp. Source Edit
proc setTimer(at: Moment; cb: CallbackFunc; udata: pointer = nil): TimerCallback {. ...raises: [], tags: [].}
- Arrange for the callback cb to be called at the given absolute timestamp at. You can also pass udata to callback. Source Edit
func toException(v: OSErrorCode): ref OSError {....raises: [], tags: [].}
- Source Edit
proc trackCounter(name: string) {.noinit, ...raises: [], tags: [].}
- Increase tracker counter with name name by 1. Source Edit
proc unregister2(fd: AsyncFD): Result[void, OSErrorCode] {....raises: [], tags: [].}
- Source Edit
proc unregisterAndCloseFd(fd: AsyncFD): Result[void, OSErrorCode] {....raises: [], tags: [].}
- Source Edit
proc untrackCounter(name: string) {.noinit, ...raises: [], tags: [].}
- Decrease tracker counter with name name by 1. Source Edit
Iterators
iterator trackerCounterKeys(loop: PDispatcher): string {....raises: [], tags: [].}
- Source Edit
iterator trackerCounters(loop: PDispatcher): tuple[name: string, value: TrackerCounter] {....raises: [], tags: [].}
- Iterates over loop thread dispatcher tracker counter table, returns all the tracker counter's names and values. Source Edit
Exports
-
AsyncExceptionError, AsyncError, AsyncTimeoutError, state, LocationKind, CallbackFunc, error, id, CancelledError, completed, value, FutureFlags, failed, Future, failed, $, InternalFutureBase, completed, cancelled, init, InternalAsyncCallback, internalInitFutureBase, FutureBase, value, finished, getSrcLocation, FutureError, FutureState, init, location, flags, FutureFlag, SrcLoc, FutureDefect, completed, Hour, init, fromNow, milliseconds, <, fastEpochTime, Minute, secs, Day, micros, >=, ==, nanoseconds, +, >=, nanos, minutes, <=, low, milliseconds, millis, $, micros, asyncTimer, +=, Nanosecond, -, -, Second, >, high, epochNanoSeconds, Microsecond, weeks, microseconds, secs, InfiniteDuration, -=, +, minutes, +=, microseconds, $, hours, toString, hours, Duration, now, <, weeks, Millisecond, <=, millis, >, ==, seconds, isZero, ZeroDuration, isInfinite, SomeIntegerI64, div, +, *, epochSeconds, high, nanoseconds, nanos, low, -, days, -=, Week, days, seconds, *, Moment, async, async, await, await, awaitne, EPERM, ENOBUFS, EFAULT, EKEYREJECTED, EHWPOISON, EHOSTDOWN, EDQUOT, EREMCHG, EISDIR, ESTRPIPE, EMLINK, ENXIO, ETOOMANYREFS, ENOMEDIUM, EOVERFLOW, EAFNOSUPPORT, ENOTCONN, ECHILD, ESOCKTNOSUPPORT, EBFONT, ESRMNT, ENOTEMPTY, ESTALE, EPROTO, EADDRNOTAVAIL, EXDEV, EBADF, EKEYREVOKED, ENOTDIR, EDEADLK, EALREADY, ESPIPE, ENOEXEC, ENETDOWN, EACCES, ELNRNG, ELOOP, ENODEV, ENFILE, ENOPKG, ENOCSI, ECHRNG, EL3HLT, EINVAL, EADDRINUSE, EUNATCH, EBADFD, EMEDIUMTYPE, ERESTART, ESRCH, ETXTBSY, ENETUNREACH, EPROTONOSUPPORT, ECANCELED, EBADMSG, EMFILE, ENOSYS, ERANGE, EDEADLOCK, ENOKEY, EILSEQ, EBUSY, EROFS, ENOTSOCK, ECONNREFUSED, ELIBMAX, EBADRQC, ECOMM, EL2HLT, EIDRM, ENOTBLK, ENOTTY, EOPNOTSUPP, EISNAM, ENOSR, EXFULL, EPROTOTYPE, EUCLEAN, ENOSTR, ENOTRECOVERABLE, ENOANO, ERFKILL, ENOENT, EISCONN, ELIBEXEC, EMULTIHOP, ELIBACC, ENOLCK, EHOSTUNREACH, ENETRESET, ECONNRESET, EINPROGRESS, E2BIG, EIO, ENODATA, ESHUTDOWN, EFBIG, ELIBBAD, EOWNERDEAD, ETIMEDOUT, ECONNABORTED, ENOSPC, EBADSLT, ETIME, EINTR, EEXIST, ENONET, EREMOTEIO, EPFNOSUPPORT, ENOLINK, ENOMSG, EDOM, EMSGSIZE, EPIPE, EBADE, ENOMEM, ELIBSCN, ENOPROTOOPT, EDESTADDRREQ, ENAVAIL, ENOTNAM, EADV, EDOTDOT, EUSERS, EKEYEXPIRED, EBADR, EL3RST, EAGAIN, ENOTUNIQ, EWOULDBLOCK, ENAMETOOLONG, EREMOTE, EL2NSYNC