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

Swiftに適したサーバーアーキテクチャを再考して実装までしてみる

Yuki Takei
December 19, 2016

 Swiftに適したサーバーアーキテクチャを再考して実装までしてみる

Yuki Takei(noppoMan) @Tokyo-Server-Side Swift Meetup#5 Dec, 19, 2016

Yuki Takei

December 19, 2016
Tweet

More Decks by Yuki Takei

Other Decks in Technology

Transcript

  1. OSS • Slimane -> Web Framework • Skelton -> Event

    Driven HTTP Server • Suv -> Async Net, I/O Platform (libuv based)
  2. ϚϧνεϨουϞσϧ main process worker thread worker thread worker thread client

    client client thread ϦΫΤετຖʹthreadΛ࡞Γ(࠷ۙ͸Pool͕ओྲྀ)ɺͦͷthread্ͰϦΫΤετΛࡹ͘ɻ ϓϩηεͱҧ͍ϝϞϦΛڞ༗Ͱ͖ΔɻʢϩοΫʹΑΔഉଞ੍ޚඞਢʣ C10K໰୊͕࿩୊ʹͳͬͨɻ thread thread
  3. Grand Central Dispatch(GCD) • DispatchQueue • DispatchSource • DispatchGroup •

    DispatchSemaphore ௨ৗ͸ΞϓϦέʔγϣϯதʹهड़͢ΔεϨου؅ཧ༻ͷίʔυΛɺ γεςϜϨϕϧͰ࣮૷ͨ͠΋ͷ (By Apple ฒߦϓϩάϥϛϯάΨΠυ) https://developer.apple.com/jp/documentation/ConcurrencyProgrammingGuide.pdf ୅දతͳ΋ͷ͸ҎԼʁ
  4. Dispatch Queue • λεΫΛqueuingͯ͠ɺ͋ΔThread্Ͱඇಉظ/ಉظత ʹ࣮ߦ͢Δ͜ͱ͕ग़དྷΔAPI • mainQueue, GlobalQueueɺϢʔβʔ࡞੒Queueʹ෼͔ ΕΔ •

    SerialͱConcurrentͷattribute͕͋Δ • queueʹରͯ͠༏ઌॱҐͷίϯτϩʔϧ͕Մೳ • ҰͭͷQueue͸࣮ߦ׬ྃ·ͰϒϩοΫ͞ΕΔ
  5. Dispatch Queue Serial Task Task Task Task Task queue.async {}

    Concurrent Task Task Task Task Task Task Task Task queue.async {} executing waiting balancing
  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. callbackͷ໰୊఺ • ͦ΋ͦ΋Swiftඪ४Ͱ͸ඇಉظύϥμΠϜʹऑ͍ (Promise΍ FutureɺAsync/Await͕ݴޠػೳʹͳ͍) • ΤϥʔϋϯυϦϯά͕೉͍͠(callbackຖʹΤϥʔॲཧ͕ඞཁ) • try catchͱ૬ੑѱ͍

    • ॥؀ࢀরΛ࡞Γ΍͍͢ʢΩϟϓνϟϦετΛॻ͖๨ΕΔʣ • return͠๨Εͯcallback͕2ճݺ͹ΕΔ໰୊ • @escapingͱ͔ɺ(T)->Voidͱ͔Կճ΋ॻ͘ͷ໘౗
  8. • ࣮͸MutexLockΛ࢖ͬͨ୯ͳΔδΣωϦοΫͳڞ༗ϝϞϦ • lock͕͔͔ΔͨΊɺ࢖͍ա͗ͨΒੑೳ͸ྼԽ͢Δ • Buffer͸LinkedListͰ࣮૷͞Ε͍ͯΔ • Buffer capacityΛӽ͑ͯૹ৴͢Δͱthrow͢Δ •

    Goͱҧ͍ɺtry catchͰΤϥʔͷัଊ͕Մೳ • Processؒͷ௨৴͸Ͱ͖ͳ͍(ErlangͷActorͳͲͱ͸׬શ ʹผ෺) ProrsumͷChannelʹؔͯ͠
  9. Benchmarking 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
  10. swift-server/work-groupʹͯҎԼͷϥΠϒϥϦΛ Swiftඪ४Ͱ࢖͑ΔΑ͏ʹ࢓༷ࡦఆதͰ͢ɻ • RFC 7230-7235(HTTP/1.1) • RFC 6455 (WebSocket) •

    RFC 7540(HTTP/2.0) • TCP/IP(v6,v4), UDP including I/O • TLS/Encryption https://github.com/swift-server/work-group