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

Add Async Await support to NIOTSListenerBootstrap childChannelInitializer #134

Open
hishnash opened this issue Oct 31, 2021 · 1 comment

Comments

@hishnash
Copy link

Expected behavior

When using NIOTSListenerBootstrap I would expect to be able to provide an async block since the common methods one would call within this have await-able versions. eg channel.pipeline.addHandler.

SwiftNIO version/commit hash

swift-nio==2.33.0
swift-swift-nio-transport-services==1.11.3

Swift & OS version (output of swift --version && uname -a)

swift-driver version: 1.26.9 Apple Swift version 5.5.1 (swiftlang-1300.0.31.4 clang-1300.0.29.6)
Target: x86_64-apple-macosx12.0
Darwin Matthauss-MacBook-Pro-2.local 21.2.0 Darwin Kernel Version 21.2.0: Wed Oct 20 00:23:27 PDT 2021; root:xnu-8019.60.40.0.1~26/RELEASE_X86_64 x86_64

P.S maybe there is an easy way to create an async task connected to the event loop that returns a EventLoopFuture<Void> but I cant find such a method on the channel.eventloop (more of an issue for SwiftNIO repo through)

@Lukasa
Copy link
Collaborator

Lukasa commented Nov 1, 2021

Yeah, this is something we want to do, but thus far haven't got around to it. In the meantime, you can use this:

extension NIOTSListenerBootstrap {
    public func serverChannelInitializer(_ initializer: @escaping @Sendable (Channel) async throws -> Void) -> Self {
        return self.serverChannelInitializer { channel in
            let promise = channel.eventLoop.makePromise(of: Void.self)
            promise.completeWithTask {
                await initializer(channel)
            }
            return promise.futureResult
        }
    }

    public func childChannelInitializer(_ initializer: @escaping @Sendable (Channel) async throws -> Void) -> Self {
        return self.childChannelInitializer { channel in
            let promise = channel.eventLoop.makePromise(of: Void.self)
            promise.completeWithTask {
                await initializer(channel)
            }
            return promise.futureResult
        }
    }
}

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

No branches or pull requests

2 participants