Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Joannis Orlandos - Asynchronous programming in ...

Joannis Orlandos - Asynchronous programming in Swift

This was presented at the Swift Usergroup Netherlands. Would you like to speak/present? Visit https://swift.amsterdam for more information.

Asynchronous programming is essential for performant applications but can be confusing to use. This presentation will learn you about promises, futures and streams and how to use them effectively (and creatively) to write performant and comprehensible applications.

Swift Usergroup Netherlands

October 24, 2017
Tweet

More Decks by Swift Usergroup Netherlands

Other Decks in Programming

Transcript

  1. WHAT IS ASYNC? > Reactive/Event driven > Promises & Futures

    > Event loops > Nonblocking > Streams
  2. VAPOR 2 // Creating a new user in a JSON

    API drop.post("users") { request in // Extract form data guard let json = request.json, let email = json["email"]?.string, let password = json["password"]?.string else { throw Abort.badRequest } // Create a new user let user = try User(email: email, hasingPassword: password) // Success is dependent on successful insertion try user.save() return Response(status: .ok) }
  3. VAPOR 3 /// Type safe registration form struct RegistrationRequest: Decodable

    { var email: String var password: String } app.post("users") { request in // Extract form data let form = try JSONDecoder().decode(RegistrationRequest.self, from: request.body.data) // Create a new user let user = try User(email: form.email, hasingPassword: form.password) // Success is dependent on successful insertion return try user.save().map { // This transform will be ons successful save return Response(status: .ok) } }
  4. VAPOR 3 > Less dependent on external sources > Same

    amount of code (20 lines) > Much more performant > Much more scalable !
  5. THE OBJECTS // Create a promise // Promises are write-only,

    emitting an event let promise = Promise<String>() // A future can be extracted from a promise // Futures are read-only, receiving the promised event let future: Future<String> = promise.future
  6. DELIVERING A PROMISE let promise = Promise<String>() // Complete the

    promise whenever it's ready promise.complete("Hello world") // Or fail the promise if something went wrong promise.fail(error)
  7. RECEIVING THE RESULT let future: Future<String> = promise.future // `then`

    will be called on success future.then { string in print(string) // `catch` will be called on error }.catch { error in // Handle error }
  8. TRANSFORM RESULTS struct UserSession: SessionCookie { var user: Reference<User> }

    app.get("profile") { request in let session = try request.getSessionCookie() as UserSession // Fetch the user return try session.user.resolve().map { user in // Map the user to a ResponseRepresentable // Views are ResponseRepresentable return try view.make("profile", context: user.profile, for: request) } }
  9. NESTED ASYNC OPERATIONS app.get("friends") { request in let session =

    try request.getSessionCookie() as UserSession let promise = Promise<View>() // Fetch the user try session.user.resolve().then { user in // Returns all the user's friends try user.friends.resolve().then { friends in return try view.make("friends", context: friends, for: request).then { renderedView in promise.complete(renderedView) }.catch(promise.fail) }.catch(promise.fail) }.catch(promise.fail) return promise.future }
  10. NESTED ASYNC OPERATIONS app.get("friends") { request in let session =

    try request.getSessionCookie() as UserSession // Fetch the user return try session.user.resolve().flatMap { user in // Returns all the user's friends return try user.friends.resolve() }.map { friends in // FlatMap replaced this future with return try view.make("friends", context: friends, for: request) } }
  11. YOU CAN ADD MANY CALLBACKS let future: Future<String> = generateLogMessage()

    future.then(print) future.then(log.error) future.map { message in try LogMessage(message).save() }.catch { saveFailure in log.fatal(saveFailure) }
  12. SYNCHRONOUS APIS ARE STILL USABLE WHEN WORKING WITH SYNCHRONOUS APIS,

    YOU CAN BLOCK THE ASYNC OPERATION // Throws an error if the promise failed // Returns the expected result by blocking the thread until completion let result = try future.blockingAwait(timeout: .seconds(5))
  13. STREAM An asynchronous sequence of events with 2 primary types

    InputStream & OutputStream ... and completely protocol oriented
  14. OutputStream Emits events of a specific type // TCP Socket

    outputting data socket.flatMap { byteBuffer in // returns a `String?` // `flatMap` will filter out invalid strings return String(bytes: byteBuffer, encoding: .utf8) // Prints all valid strings // `print` will print the String and return `Void`, creating a Void stream // The stream without data will still be called for each event (such as errors) }.map(print).catch { error in // handle errors }
  15. InputStream Receives emitted events of a specific type class PrintStream:

    InputStream { /// Used to chain errors from stream to stream public var errorStream: ErrorHandler? func inputStream(_ input: String) { print(input) } init() {} } let printStream = PrintStream() // Takes received bytes from the sockets socket.flatMap { // Tries to turn the bytes into a String return String(bytes: byteBuffer, encoding: .utf8) // Drains it into a PrintStream's inputStream }.drain(into: printStream)
  16. USE CASE - THE VAPOR HTTP SERVER let server =

    try TCPServer(port: 8080, worker: Worker(queue: myQueue)) // Servers are a stream of accepted web client connections // Clients are an input and output stream of bytes server.drain { client in let parser = RequestParser() let router = try yourApplication.make(Router.se let serialiser = ResponseSerializer() // Parses client-sent bytes into the RequestParser let requestStream = client.stream(to: parser) // Parses requests to the Vapor router, creating a response let responseStream = requestStream.stream(to: router) // Serializes the responses, creating a byte stream let serializedResponseStream = responseStream.stream(to: serializer) // Drains the serialized responses back into the client socket serializedResponseStream.drain(into: client) }
  17. let client = try TCPClient(worker: Worker(queue: myQueue)) try client.connect(hostname: "example.com",

    port: 80) try client.writable(queue: myQueue).then { try client.send(data) let data = try client.read() } try client.start() ✅
  18. WORKERS PROVIDE CONTEXT > Share 'globals' such as a database

    driver > Guaranteed to be the same thread > Require no thread-safety