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
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
140
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
330
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
180
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
430
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
500
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
Lexical Analysis
shigashiyama
1
150
AGIについてChatGPTに聞いてみた
blueb
0
130
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Music & Morning Musume
bryan
46
6.2k
Site-Speed That Sticks
csswizardry
0
28
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Raft: Consensus for Rubyists
vanstee
136
6.6k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Making Projects Easy
brettharned
115
5.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
A Tale of Four Properties
chriscoyier
156
23k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
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ͰͷαʔόʔαΠυ։ൃເޠͰͳ͘ɺ ͢ͰʹϦΞϧͳͷͰ͋Δͱ͍͏͜ͱ͕ΘΕخ͍͠Ͱ͢ʂ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠