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

実践Server Side Swift

Yuki Takei
February 24, 2017

実践Server Side Swift

Swift実践入門刊行記念イベント Feb 24 2017

Yuki Takei

February 24, 2017
Tweet

More Decks by Yuki Takei

Other Decks in Technology

Transcript

  1. • 2015೥12݄ΑΓɺServer Side SwiftͷϥΠϒϥϦ։ൃ΍ί ϛϡχςΟ΁ͷߩݙΛελʔτ • 2016೥2݄ʹSlimaneΛϦϦʔε(ݱࡏ։ൃ͸Stop) • 2016೥3݄͔ΒTokyo Server

    Side Swift Meetup։࢝ • 2016೥4݄͔ΒOpen-SwiftɺZewoίϛϡχςΟʹࢀՃ • 2016೥10݄ΑΓServer Work GroupͷεςʔΫϗϧμʔʹ • 2017೥1݄ΑΓSwiftΛόοΫΤϯυαʔϏεͷݴޠͱͯ͠ɺ αʔϏε։ൃΛελʔτ ๻ͱServer Side SwiftͷาΈ
  2. • Himotoki • SwiftyJSON • swift-protobuf • RXSwift • JSONWebToken.swift

    αʔόʔαΠυͰ΋࢖͑ΔiOSͷϥΠϒϥϦʢ΄ΜͷҰ෦ʣ
  3. Dispatch Queue Serial Task Task Task Task Task queue.async {}

    Concurrent Task Task Task Task Task Task Task Task queue.async {} executing waiting balancing thread 1 thread 1 thread 2
  4. Dispatch Source Dispatch Source Socket ؂ࢹ Event Event Event ιέοτ΍ϑΝΠϧͷهड़ࢠΛ؂ࢹͯ͠ɺ

    ॻ͖ࠐΈ͕͋ͬͨλΠϛϯάͰΠϕϯτΛൃՐ select΍kqueueͱ͍ͬͨඇಉظܥͷsystem callΛந৅Խ͍ͯ͠Δ
  5. • noppoMan/Prorosum …… HTTP Server • noppoMan/WebAppKit …… Routing, Middleware

    • noppoMan/SwiftKnex …… Mysql Client, Schema Migrator • apple/swift-protobuf …… Entities and JSON serializer • kylef/JSONWebToken.swift …… JWT encode/decode ࠓճ࢖ͬͨϥΠϒϥϦ
  6. ProrsumͷαʔόʔΞʔΩςΫνϟ DispatchSource + DispatchQueueΛ࢖ͬͨ Πϕϯτۦಈ + WorkerThreadͷϋΠϒϦοτϞσϧ main thread DispatchSourceͰɺ

    listeningSocketΛ؂ࢹ Queue Queue Queue Queue Queue Queue Queue Queue Queue client DispatchQueue(concurrent) nonblocking I/O blocking I/O
  7. thread Prorsumͷඇಉظ/ಉظॲཧϞσϧ Task1 Task2 Task3 send send send main thread

    receive Channel thread thread ProrsumͰ͸ݪଇͱͯ͠callbackʹΑΔϑϩʔ੍ޚ͸ߦΘͳ͍ɻ Prorosumͷఏڙ͢Δgoؔ਺ͱchannelʹΑΓɺฒྻॲཧͱͦͷಉظΛऔΔ Async Async Async Sync(Blocking)
  8. thread લճͷਤʹ౰ͯ͸ΊΔͱ Task1 Task2 Task3 send(1) send(2) send(3) main thread

    Channel thread thread go() go() go() receive(1) receive(2) receive(3)
  9. ProrsumͷύϑΥʔϚϯε Request/sec 0 17500 35000 52500 70000 Prorsum Kitura Go

    1.7 HTTP Server Express 14,769 64,768 17,144 29,436 ɾwrk -d 30s -t 4 -c 20 ɾResponded with the 10 length of random JSON array ɾMachine: MacOS Sierra, 8 logical cores, 8GB RAM