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

async/await and AsyncSequence Prototype #44

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft

Conversation

dnadoba
Copy link
Member

@dnadoba dnadoba commented Apr 3, 2021

Just noticed that SwiftNIO has basic support for async/await 🎉
The PR is not meant to be merged. Just a quick outlook into the future.

The internal implementation is really ugly but it works and the call site looks really nice:

let bootstrap = ClientBootstrap(
    config: ClientSetupConfig(
        timeBetweenKeepaliveFrames: 0,
        maxLifetime: 30_000,
        metadataEncodingMimeType: "message/x.rsocket.routing.v0",
        dataEncodingMimeType: "application/json"
    ),
    transport: WSTransport(),
    timeout: .seconds(30)
)
let client = try await bootstrap.connect(host: host, port: port, uri: uri)

let stream = client.requester.requestStream(payload: Payload(
    metadata: route("searchTweets"),
    data: Data(searchString.utf8)
))

for try await payload in stream.prefix(limit) {
    let json = try JSONSerialization.jsonObject(with: payload.data, options: [])
    let data = try JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted])
    let string = String(decoding: data, as: UTF8.self)
    print(string)
}

Take a look at Sources/Examples/AsyncTwitterClient/main.swift if you want to see how async/await could look like for RSocket. The complete implementation is in Sources/RSocketAsync/AsyncAwait.swift.

If you want to play around with it, you need a download a snapshot from https://swift.org/download/#snapshots with the lates changes from the main branch. async/await is available since Swift 5.4 and AsyncSequence only in Swift 5.5.
As even Swift 5.4 is not yet release, this is highly experimental.

@nkristek
Copy link

nkristek commented Apr 6, 2021

The API looks very nice!

Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
… unsafe flag to compile on latest toolchain

by removing this flag, this should compile on older compilers too

Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>

Conflicts:
	.github/workflows/swift.yml
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
Signed-off-by: David Nadoba <dnadoba@gmail.com>
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.

None yet

2 participants