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

Building the Server-side Swift ecosystem

Ian Partridge
September 09, 2019

Building the Server-side Swift ecosystem

One year ago, at try! Swift NYC 2018, the teams from Apple, Vapor and Kitura announced the Swift Server Working Group (SSWG) - an effort to mature the Swift ecosystem for developing and deploying server applications. Since then, six new libraries have been welcomed into the project, more are under development, and big improvements have happened in the Swift on Linux releases. In this talk I'll bring everyone up to date, and show how you can get involved. I'll finish by introducing Appsody, a new open source project for end-to-end development of cloud-native Swift applications - ready for your next SSWG-filled server-side Swift project!

Ian Partridge

September 09, 2019
Tweet

More Decks by Ian Partridge

Other Decks in Programming

Transcript

  1. SwiftNIO • Asynchronous network application framework • High performance protocol

    implementations • HTTP • WebSocket • SwiftNIO 2 • HTTP/2 in pure Swift! • TLS on BoringSSL • SwiftNIO = 32kloc. SwiftNIO tests = 34kloc. /apple/swift-nio/
  2. Logging import Logging var logger = Logger(label: "com.acme.app.main") logger.info("Hello World!”)

    2019-09-07T18:30:02+0000 info: Hello world! /apple/swift-log/ do { try App.startup() } catch { logger.fatal("Boom") 2019-09-07T18:30:02+0000 fatal: Boom } logger[metadataKey: "current-request"] = "\(UUID())" logger.info("new request”) 2019-09-07T18:30:02+0000 info: current-request= F8633013-3DD8-481C-9256-B296E43443ED new request logger.logLevel = .debug
  3. Metrics import Metrics let counter = Counter(label: "com.acme.app.numberOfRequests") counter.increment(by: 5)

    /apple/swift-metrics/ let recorder = Recorder(label: "com.acme.app.responseSizes") recorder.record(8) let gauge = Gauge(label: "com.acme.app.threadCount") gauge.record(10) let timer = Timer(label: "com.acme.app.requestDuration") timer.recordMilliseconds(3.42)
  4. PostgreSQL /vapor/postgres-nio/ import PostgresNIO // Connect to the database let

    futureConnection = try PostgresConnection.connect( to: .makeAddressResolvingHost("my.psql.server", port: 5432), on: eventLoop) // Wait for the EventLoopFuture<PostgresConnection> to resolve let connection = futureConnection.wait() // Run a query, getting an EventLoopFuture<[PostgresRow]> let futureVersion = try connection.simpleQuery("SELECT version()") let version = futureVersion.wait() print(version) // [["version": "11.0.0"]] let futureRows = try connection.query("SELECT * FROM cars WHERE name = $1", ["Mustang"]) futureRows.whenSuccess { rows in print(rows) // [["id": 24, "name": "Mustang"]] }
  5. Redis /Mordil/swift-redi-stack/ import RediStack let connection = RedisConnection.connect( to: try

    .init(ipAddress: "127.0.0.1", port: RedisConnection.defaultPort), on: eventLoop ).wait() let result = try connection.set("my_key", to: "some value") .flatMap { return connection.get("my_key") } .wait() print(result) // Optional("some value”)
  6. Docker /apple/swift-docker • Image sizes • Swift 4.2: 1.48GB •

    apt-get -q install -y make libc6-dev clang-3.8 curl libedit- dev libpython2.7 libicu-dev libssl-dev libxml2 tzdata git libcurl4-openssl- dev pkg-config • Swift 5.0: 1.35GB
 • “Slim images” • Swift 5.0-slim: 194MB
  7. Docker /apple/swift-docker FROM swift:5.0.3 as build WORKDIR /workdir COPY .

    . RUN swift build -c release FROM swift:5.0.3-slim as run WORKDIR /workdir COPY --from=build /workdir . CMD [".build/x86_64-unknown-linux/debug/docker-test"]
  8. Linux releases • Many months between releases • Agreement from

    Swift core team for Linux-only releases • Increased cadence • 5.0.0 - March 25 • 5.0.1 - April 18 • 5.0.2 - July 15 • 5.0.3 - August 30 • 5.0.4 - open now!
  9. The future… • More databases • MongoDB • MySQL •

    … • Backtraces on crashes • Distributed tracing