EventLoop

public protocol EventLoop : EventLoopGroup

An EventLoop processes IO / tasks in an endless loop for Channels until it’s closed.

Usually multiple Channels share the same EventLoop for processing IO / tasks and so share the same processing Thread. For a better understanding of how such an EventLoop works internally the following pseudo code may be helpful:

while eventLoop.isOpen {
    /// Block until there is something to process for 1...n Channels
    let readyChannels = blockUntilIoOrTasksAreReady()
    /// Loop through all the Channels
    for channel in readyChannels {
        /// Process IO and / or tasks for the Channel.
        /// This may include things like:
        ///    - accept new connection
        ///    - connect to a remote host
        ///    - read from socket
        ///    - write to socket
        ///    - tasks that were submitted via EventLoop methods
        /// and others.
        processIoAndTasks(channel)
    }
}

Because an EventLoop may be shared between multiple Channels it’s important to NOT block while processing IO / tasks. This also includes long running computations which will have the same effect as blocking in this case.

  • Returns true if the current Thread is the same as the Thread that is tied to this EventLoop. false otherwise.

    Declaration

    Swift

    var inEventLoop: Bool { get }
  • Submit a given task to be executed by the EventLoop

    Declaration

    Swift

    func execute(_ task: @escaping () -> Void)
  • submit(_:) Default implementation

    Submit a given task to be executed by the EventLoop. Once the execution is complete the returned EventLoopFuture is notified.

    Default Implementation

    Declaration

    Swift

    func submit<T>(_ task: @escaping () throws -> T) -> EventLoopFuture<T>

    Parameters

    task

    The closure that will be submitted to the EventLoop for execution.

    Return Value

    EventLoopFuture that is notified once the task was executed.

  • Schedule a task that is executed by this SelectableEventLoop after the given amount of time.

    Declaration

    Swift

    func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled<T>
  • newPromise(file:line:) Extension method

    Creates and returns a new EventLoopPromise that will be notified using this EventLoop as execution Thread.

    Declaration

    Swift

    public func newPromise<T>(file: StaticString = #file, line: UInt = #line) -> EventLoopPromise<T>
  • newFailedFuture(error:) Extension method

    Creates and returns a new EventLoopFuture that is already marked as failed. Notifications will be done using this EventLoop as execution Thread.

    Declaration

    Swift

    public func newFailedFuture<T>(error: Error) -> EventLoopFuture<T>

    Parameters

    error

    the Error that is used by the EventLoopFuture.

    Return Value

    a failed EventLoopFuture.

  • newSucceededFuture(result:) Extension method

    Creates and returns a new EventLoopFuture that is already marked as success. Notifications will be done using this EventLoop as execution Thread.

    Declaration

    Swift

    public func newSucceededFuture<T>(result: T) -> EventLoopFuture<T>

    Parameters

    result

    the value that is used by the EventLoopFuture.

    Return Value

    a succeeded EventLoopFuture.

  • next() Extension method

    Declaration

    Swift

    public func next() -> EventLoop
  • close() Extension method

    Undocumented

    Declaration

    Swift

    public func close() throws
  • Schedule a repeated task to be executed by the EventLoop with a fixed delay between the end and start of each task.

    Declaration

    Swift

    @discardableResult
    public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping (RepeatedTask) throws -> Void) -> RepeatedTask

    Parameters

    initialDelay

    The delay after which the first task is executed.

    delay

    The delay between the end of one task and the start of the next.

    task

    The closure that will be executed.

  • Schedule a repeated task to be executed by the EventLoop with a fixed delay between the end and start of each task.

    Declaration

    Swift

    @discardableResult
    public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping (RepeatedTask) -> EventLoopFuture<Void>) -> RepeatedTask

    Parameters

    initialDelay

    The delay after which the first task is executed.

    delay

    The delay between the end of one task and the start of the next.

    task

    The closure that will be executed.

  • makeIterator() Extension method

    Returns an EventLoopIterator over this EventLoop.

    Note

    The return value of makeIterator is currently optional as requiring it would be SemVer major. From NIO 2.0.0 on it will return a non-optional iterator.

    Declaration

    Swift

    public func makeIterator() -> EventLoopIterator?

    Return Value

    EventLoopIterator