Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
実践Server Side Swift
Search
Yuki Takei
February 24, 2017
Technology
13
4.1k
実践Server Side Swift
Swift実践入門刊行記念イベント Feb 24 2017
Yuki Takei
February 24, 2017
Tweet
Share
More Decks by Yuki Takei
See All by Yuki Takei
VAR モデルによる OSS プロジェクト同士が生存性に与える 影響の分析
noppoman
0
250
Server Side Swift の個人的な利用シーン
noppoman
2
580
Serverless Server Side Swift
noppoman
2
1.6k
Practical Hexaville(ja)
noppoman
0
460
Server Side Swiftを使った サービス開発とその運用を考える
noppoman
4
1.3k
Swiftに適したサーバーアーキテクチャを再考して実装までしてみる
noppoman
7
2.8k
Swift3 Framework Slimane and Server Side Swift (ja)
noppoman
2
2.2k
About Deploying and Process Management for Slimane
noppoman
2
410
Server Side Framework Slimaneの紹介
noppoman
1
1.1k
Other Decks in Technology
See All in Technology
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
Lexical Analysis
shigashiyama
1
150
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
5
590
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
110
AGIについてChatGPTに聞いてみた
blueb
0
130
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
360
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
2
580
Can We Measure Developer Productivity?
ewolff
1
150
[FOSS4G 2024 Japan LT] LLMを使ってGISデータ解析を自動化したい!
nssv
1
210
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
Featured
See All Featured
Six Lessons from altMBA
skipperchong
27
3.5k
Docker and Python
trallard
40
3.1k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Thoughts on Productivity
jonyablonski
67
4.3k
The Language of Interfaces
destraynor
154
24k
Being A Developer After 40
akosma
86
590k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Building Adaptive Systems
keathley
38
2.3k
GitHub's CSS Performance
jonrohan
1030
460k
Transcript
࣮ફ Server Side Swift Yuki Takei Swift࣮ફೖץߦه೦Πϕϯτ Feb 24 2017
None
Twitter @noppoman722 Ͱͬͯ·͢
• 201512݄ΑΓɺServer Side SwiftͷϥΠϒϥϦ։ൃί ϛϡχςΟͷߩݙΛελʔτ • 20162݄ʹSlimaneΛϦϦʔε(ݱࡏ։ൃStop) • 20163݄͔ΒTokyo Server
Side Swift Meetup։࢝ • 20164݄͔ΒOpen-SwiftɺZewoίϛϡχςΟʹࢀՃ • 201610݄ΑΓServer Work GroupͷεςʔΫϗϧμʔʹ • 20171݄ΑΓSwiftΛόοΫΤϯυαʔϏεͷݴޠͱͯ͠ɺ αʔϏε։ൃΛελʔτ ͱServer Side SwiftͷาΈ
https://tokyo-ss-swift.connpass.com/
• Server Side Swiftͷ͍ॴ • Server Side Swiftͷ։ൃڥΛ͑Δ • SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ
• ࣮ફతͳWebΞϓϦέʔγϣϯΛ࡞Ζ͏ ΞδΣϯμ
Server Side Swiftͷ͍ॴ
SwiftΛwebαʔϏεͰ͏ϝϦοτ • ੩తݴޠͳΒͰͷ҆શੑɺॊೈͳݴޠ༷ • ࣮ߦɺলΤω • VMɺGC͕ͳ͍ • iOSΞϓϦ։ൃʹΘΕ͍ͯΔϥΠϒϥϦ͕ͦͷ ··͑Δέʔε͕༗Δ
• APIαʔόʔ(Node.jsGoͰ࡞͍ͬͯΔͱ͜Ζ) • όονॲཧͳͲͷίϚϯυϥΠϯΞϓϦέʔγϣϯ • ը૾ॖখɺѹॖɺ҉߸ԽͳͲLLͰ࣌ؒͷ͔͔Δ෦ SwiftͷwebαʔϏεͰͷ͍ॴ
Server Side Swiftͷ։ൃڥΛ͑ͯΈΑ͏
• Xcode8.2ΛࣄલʹΠϯετʔϧ͓͖ͯ͠·͠ΐ͏ ڥߏஙͷલʹ….
Server SideϓϩδΣΫτ ίϚϯυϥΠϯΛͬͯ࡞͍͖ͯ͠·͢
Server Side SwiftϓϩδΣΫτͷ࡞ ϓϩδΣΫτ༻ͷσΟϨΫτϦ࡞
εχϖοτ࡞ίϚϯυ Server Side SwiftϓϩδΣΫτͷ࡞
Server Side Swiftͷ جຊతͳϑΝΠϧߏ Server Side SwiftϓϩδΣΫτͷ࡞
main.swiftͷத
ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ ίϯύΠϧ & ϦϯΫ
ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ .build/debugҎԼʹ࣮ߦՄೳͳϥΠϒϥ͕ग़དྷΔ
ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ ࣮ߦ
Swift Package ManagerΛͬͯΈΑ͏
• ͦͷ໊ͷ௨ΓɺSwift༻Package Manager(Appleެࣜ) • ௨শSPM • CocoaPodsCarthageͱಉϨΠϠʔ • ϓϩδΣΫτԼͷPackage.swiftʹઃఆґଘΛॻ͖ ࠐΉ
• ݱ࣌ͰɺiOSMacͳͲͷUIΞϓϦະαϙʔτ Swift Package Managerͱ
هड़ྫ
…………… …………… …………… Ϗϧυ
…………… …………… …………… ґଘϥΠϒϥϦͷclone Ϗϧυ
…………… …………… …………… ίϯύΠϧ & ϦϯΫ Ϗϧυ
XcodeͰ։ൃͯ͠ΈΑ͏
SPMͰ࡞ͬͨϓϩδΣΫτɺ XcodeͰ։ൃ͢Δ͜ͱग़དྷ·͢ɻ
XcodeͰ։ൃͯ͠ΈΑ͏ Package.swift͔ΒઃఆΛಡΈࠐΈɺ.xcodeprojΛ࡞͢Δ
XcodeͰ։ൃͯ͠ΈΑ͏ ίϚϯυҰൃͰxcodeproj͕࡞͞ΕΔ
XcodeͰ։ൃͯ͠ΈΑ͏ xcodeprojΛ։͖·͢
Xcode্Ͱ࣮ߦ
XCTestͪΖΜ͑·͢ʂ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯ Λ࡞ΔϙΠϯτ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ڧྗͳܕ҆શੑΛ׆͔͢
σʔλϕʔεAPIϨεϙϯεͷIO݁ՌAnyܕ͕΄ͱΜͲ JSON MySQL
σʔλϕʔεAPIϨεϙϯεͷIO݁ՌAnyܕ͕΄ͱΜͲ JSON MySQL AnyܕΛͦͷ··ѻ͏ͱ…
σʔλϕʔεAPIϨεϙϯεͷIO݁ՌAnyܕ͕΄ͱΜͲ JSON MySQL unwrapɺόϦσʔγϣϯΛॻ͘Ίʹ…
Server SideؔΘΔਓ͕ଟ͍ͨΊɺ AnyܕΛ;ΜͩΜʹͬͨίʔυϝϯς͕૬େม
DBͷऔಘ݁ՌͳͲ͖ͪΜͱܕΛఆٛ͠·͠ΐ͏
Enum ڧྗͳܕ҆શੑΛ׆͔͢ EnumʹΑΔύλʔϯϚονϯάɺServer SideͰ༗ޮ HTTP Method HTTP Status
Enum ڧྗͳܕ҆શੑΛ׆͔͢ ErrorͷύλʔϯϚονϯάͰɺΤϥʔϨεϙϯεΛॊೈʹ
ܦݧ্ɺwebαʔϏεΛӡ༻͍ͯͯ͠ɺ αʔόʔ͕མͪΔେ͖ͳݪҼͷҰ͕ͭNullࢀর(ψϧϙ)Ͱ͢ɻ Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢
SwiftʹOptionalܕ͕͋ΓɺϓϩάϥϚͦͷ͕nullΛڐ༰ ͢Δ͔Ͳ͏͔Λࢹ֮తʹஅͰ͖·ͨ͠ ʢίϯύΠϥ͕ڭ͑ͯ͘ΕΔʣ Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢
Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢ JavaScriptͰΫϥογϡ͢Δ͔͠Εͳ͍ྫ
Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢ SwiftͰ҆શʹIO݁ՌΛѻ͏
SwiftΛαʔόʔͰ͏ϝϦοτͯ͠ɺ ͜ͷnullͷڐ༰ੑͱͯେ͖ͳཁૉͰ͢ɻ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ΫϥΠΞϯτͰϝδϟʔͳϥΠϒϥϦΛੵۃతʹ͏
• SwiftʹiOSͳͲͰங͔ΕͨɺطଘͷΤίγες Ϝ͕͋Δ • UIΛ࣋ͨͳ͍௨৴ॲཧσʔλγϦΞϥΠζʹ ಛԽͨ͠ͷߴ֬ͰServer SideͰ͑Δ • SPMʹରԠ͍ͯ͠ͳ͍߹ɺগ͠ͷมߋͰ Server
SideͰ͑Δέʔε͕͋Δ SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ΫϥΠΞϯτͰϝδϟʔͳϥΠϒϥϦΛੵۃతʹ͏
• Himotoki • SwiftyJSON • swift-protobuf • RXSwift • JSONWebToken.swift
αʔόʔαΠυͰ͑ΔiOSͷϥΠϒϥϦʢ΄ΜͷҰ෦ʣ
ंྠͷ࠶ൃ໌Λ߇͑ͯɺΤίγεςϜΛੵۃతʹ׆༻͠·͠ΐ͏
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ UbuntuͰಈ͘͜ͱΛ࠷༏ઌʹ
• GoͷΑ͏ͳϙʔλϏϦςΟະͩSwiftʹͳ͍ • SwiftެࣜαϙʔτͷLinuxUbuntu • MacͰ͑ͯUbuntuͰ͑ͳ͍API݁ߏ͋Δ • ςετίʔυΛඞͣUbuntuͰΒͤ·͠ΐ͏ SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ UbuntuͰಈ͘͜ͱΛ࠷༏ઌʹ
Linux൛SwiftͰϋϚͬͨ͜ͱᶃ CF**ͰDNSղܾΛࢼΈ͕ͨ…..
CF**ͰDNSղܾΛࢼΈ͕ͨ….. Linux൛SwiftͰϋϚͬͨ͜ͱᶃ
Linux൛SwiftʹCF**Ͱ͑ͳ͍ͷ͕͋Δ Linux൛SwiftͰϋϚͬͨ͜ͱᶃ
Main thread͔Ͳ͏͔ఆ͠Α͏ͱͯ͠… Linux൛SwiftͰϋϚͬͨ͜ͱᶄ
Main thread͔Ͳ͏͔ఆ͠Α͏ͱͯ͠… Linux൛SwiftͰϋϚͬͨ͜ͱᶄ
Linux൛SwiftͰϋϚͬͨ͜ͱᶄ
࣮͞Εͯͳ͍wwwww
͜ͷΑ͏ʹɺະͩLinux൛SwiftͰτϥοϓ͕ͪΒ΄Β
MacͰಈ͍͔ͨΒͬͯ҆৺͠ͳ͍Α͏ʹʂ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ GCDΛ׆༻ͯ͠ɺαʔόʔϦιʔεΛޮతʹѻ͏
GCDͱɺThreadඇಉظ໋ྩΛ؆୯ʹѻ͏ͨΊͷϥΠϒϥϦ GCDΛ͏·͘ѻ͏͜ͱ͕ɺSwiftͷServer SideΛ߈ུ͢Δ伴
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
Dispatch Source Dispatch Source Socket ࢹ Event Event Event ιέοτϑΝΠϧͷهड़ࢠΛࢹͯ͠ɺ
ॻ͖ࠐΈ͕͋ͬͨλΠϛϯάͰΠϕϯτΛൃՐ selectkqueueͱ͍ͬͨඇಉظܥͷsystem callΛநԽ͍ͯ͠Δ
GCDΛۦͯ͠ɺߴͳαʔόʔΛ࡞Γ·͠ΐ͏
SwiftͰ࣮ફతͳWebΞϓϦέʔγϣϯ Λ࡞ͬͯΈΑ͏
Ϣʔβʔ A Ϣʔβʔ B OAuth OAuth Callback Callback ͜Μͳײ͡ͷwebΞϓϦέʔγϣϯΛ࡞ͬͯΈ·ͨ͠ ChatϧʔϜ
ᶃ ᶃ ᶄ ᶄ ᶅ ᶅ
• GithubͷOAuthΛͬͨϩάΠϯػೳ • JWTʹΑΔϢʔβʔೝূ(ηογϣϯସ) • σʔλશͯMySQLʹอଘ͢Δ • ίʔυଆͰεΩʔϚཧ • ViewSPAͰఏڙ
ͬ͘͟Γͱͨ͠ཁ݅
https://github.com/noppoMan/SwiftJNChatApp
σϞ
https://www.youtube.com/watch?v=WI7BQQHVV_Y&feature=youtu.be ಈըURLͪ͜Β
• 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 ࠓճͬͨϥΠϒϥϦ
noppoMan/Prorsum
noppoMan/Prorosum • HTTP ServerͳͲͷSwiftඪ४Ͱఏڙ͞Ε͍ͯͳ͍ ωοτϫʔΫػೳΛิ͢Δ • LinuxͰಈ͘͜ͱΛલఏʹ։ൃ͞Ε͍ͯΔ • GoݴޠͷgoroutineͱChannelΛΠϯεύΠΞ͠ ͨɺඇಉظ/ಉظॲཧϞσϧ
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
• Ͳ͏ͯ͠ɺCallbackϕʔεͷϑϩʔ੍ޚʹͳͬ ͯ͠·͏ • ConcurrentͰ࣮ߦ͢Δ߹ɺεϨουηʔϑΛҙ ࣝͯ͠ϓϩάϥϛϯάΛ͢Δඞཁ͕͋Δ DispatchQueueͷ
thread Prorsumͷඇಉظ/ಉظॲཧϞσϧ Task1 Task2 Task3 send send send main thread
receive Channel thread thread ProrsumͰݪଇͱͯ͠callbackʹΑΔϑϩʔ੍ޚߦΘͳ͍ɻ Prorosumͷఏڙ͢ΔgoؔͱchannelʹΑΓɺฒྻॲཧͱͦͷಉظΛऔΔ Async Async Async Sync(Blocking)
go()ͱChannelΛ࢛ͬͨଇԋࢉͷྫ
thread લճͷਤʹͯΊΔͱ Task1 Task2 Task3 send(1) send(2) send(3) main thread
Channel thread thread go() go() go() receive(1) receive(2) receive(3)
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
RoutingMiddleware • noppoMan/WebAppKitΛ͍ͬͯΔ • WebAppKitϓϩʔαϜ͚ͷRoutingɺ MiddlewareΛఏڙ͢ΔϑϨʔϜϫʔΫ • ExpressSinatraͷΑ͏ͳͷ • ೝূͳͲ͜ͷMiddlewareͰॲཧ͍ͯ͠Δ
APIͱͷ௨৴ (NS)URLSessionΛͬͯAPIͱ௨৴͍ͯ͠Δɻ ͨͩ͠ɺίʔϧόοΫϕʔεͷϑϩʔ੍ޚʹͨ͘͠ͳ͍ͷͰɺdataTaskResumtSync ͱ͍͏ϝιουΛՃͯ͠ɺpthread_cond_waitͰϒϩοΫ͢ΔΑ͏ʹ͍ͯ͠Δ Routes/GithubRoute.swift
࣮APIKitΛ͍͔ͨͬͨͷͰ͕͢ɺɺ
objc_**͕LinuxͰ͑ͳ͍…
ͥͻLinuxରԠ͍ͨ͠Ͱ͢Ͷʂ
• SwiftyJSONͳͲͷJSONϥΠϒϥϦ͍ͬͯͳ͍ • EntityΛswift-protobufΛͬͯఆ͍ٛͯ͠ΔͨΊɺͦ Ε͕ఏڙ͢ΔJSONγϦΞϥΠζػೳΛ͍ͬͯΔ JSONͷऔѻ
Swift͔ΒMySQLΛѻ͏ ͓खͷSwiftKnexͱ͍͏ϥΠϒϥϦΛ͍ͬͯΔ
• Mysql QueryBuilder • Schema Migration • Pure SwiftͰMySQLϓϩτίϧΛ࣮͍ͯ͠Δ noppoMan/SwiftKnex
None
None
None
ܕ҆શʹselect݁ՌΛѻ͏
ίʔυଆͰεΩʔϚཧ SwiftKnexʹɺεΩʔϚཧ/ϚΠάϨʔγϣϯػೳ͕උΘ͍ͬͯΔ
Migration ClassΛ࡞͢Δ $ ./.build/debug/SwiftKnexMigration create ResourceName ΛίϚϯυϥΠϯ͔Β࣮ߦ͢Δ͜ͱͰɺMigration ClassΛ࡞
Migration/20170218035306_CreateUser.swift Migration Classͷఆٛ
Migrationͷ࣮ߦ/ϩʔϧόοΫ
ΫϥυͷσϓϩΠͳͲɺ ͍ͨ͜͠ͱଞʹࢁ͋ΔͷͰɺͥͻ࠙ձͰʂ
SwiftͰͷαʔόʔαΠυ։ൃເޠͰͳ͘ɺ ͢ͰʹϦΞϧͳͷͰ͋Δͱ͍͏͜ͱ͕ΘΕخ͍͠Ͱ͢ʂ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠