Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Server Side Swiftを使った サービス開発とその運用を考える
Search
Yuki Takei
January 28, 2017
Technology
4
1.3k
Server Side Swiftを使った サービス開発とその運用を考える
Tokyo Server Side Swift meetup#6 27, Jan, 2017
Yuki Takei
January 28, 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
470
実践Server Side Swift
noppoman
13
4.1k
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
Entra ID の多要素認証(Japan Microsoft 365 コミュニティ カンファレンス 2024 )
murachiakira
0
1.3k
Amazon ECSとCloud Runの相互理解で広げるクラウドネイティブの景色 / Mutually understanding Amazon ECS and Cloud Run
iselegant
17
2.1k
乗っ取れKubernetes!!~リスクから学ぶKubernetesセキュリティの考え方~/k8s-risk-and-security
mochizuki875
3
360
Storybook との上手な向き合い方を考える
re_taro
5
4.4k
セキュリティベンダー/ユーザー双方の視点で語る、 Attack Surface Managementの実践 - Finatextパート / cloudnative-architecture-of-asm
stajima
0
2.3k
Next.jsとNuxtが混在? iframeでなんとかする!
ypresto
3
2.3k
LLMアプリケーションの評価と継続的改善
pharma_x_tech
2
150
【CNDW2024】SIerで200人クラウドネイティブのファンを増やした話
yuta1979
0
190
PFN Company Deck
pfn
PRO
2
130
GDGoC開発体験談 - Gemini生成AI活用ハッカソン / GASとFirebaseで挑むパン屋のフードロス解決 -
hotekagi
1
270
LY Accessibility Guidelines @fukuoka_a11yconf_前夜祭
lycorptech_jp
PRO
1
110
徹底解説!Microsoft 365 Copilot の拡張機能 / Complete guide to Microsoft 365 Copilot extensions
karamem0
1
1.2k
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
A Philosophy of Restraint
colly
203
16k
Testing 201, or: Great Expectations
jmmastey
39
7.1k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Why Our Code Smells
bkeepers
PRO
334
57k
Side Projects
sachag
452
42k
Practical Orchestrator
shlominoach
186
10k
A better future with KSS
kneath
238
17k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Gamification - CAS2011
davidbonilla
80
5k
Transcript
Server Side SwiftΛͬͨ αʔϏε։ൃͱͦͷӡ༻Λߟ͑Δ Yuki Takei@TSSS meetup #6 27 Jan,
2017
ࣗݾհ
Serve-Side-SwiftͰෳͷOSSΛެ։͍ͯ͠·͢ʂ • Slimane: Web Framework + Web Server • Prorsum:
Go Style Concurrent System and Networking Library • SwiftKnex: QueryBuilder for Mysql
͜ͷɺෆ࿑ॴಘΛಘ͍ͨʂͱ͍͏͜ͱͰɺ αʔϏεΛҰຊग़͢͜ͱʹ͠·ͨ͠ɻʢসʣ
ϓϥΠϕʔτΧϯύχʔͰ࡞ΔͨΊɺ ͋ΔఔԿΛͯ͠OK…ͱ͍͏͜ͱͰ
Server Side SwiftͰ։ൃ͢Δ͜ͱʹ͠·ͨ͠ʂ
ɾ࣮ӡ༻Λલఏͱͯ͠ͲͷΑ͏ʹػೳ։ൃΛ͍͔ͯ͘͠ʁ ɾ࣮ࡍͷӡ༻ΛͲ͏͢Δ͔ʁ ࠓճɺServer Side SwiftͰ ͱ͍͕ͬͨͰ͖Εͱࢥ͍·͢ɻ
۩ମతͳαʔϏε༰ʹ৮Εͣɺ ٕज़తͳ؍Ͱൃද͍͖ͯ͠·͢ɻ
• εΫϨΠϐϯάͰwebίϯςϯπΛूΊΔ • ίϯςϯπΛղੳ͠ɺΫϥελϦϯάɺΧςΰϥ Πζ • ղੳޙͷίϯςϯπΛͬͨwebαʔϏεΛ։ ൃ͢Δ • ϢʔβʔߦಈΛղੳͤ͞ɺϢʔβʔʹఏڙ͢Δί
ϯςϯπΛֶशɺมԽͤ͞Δ αʔϏεཁ݅
• εΫϨΠϐϯάͰwebίϯςϯπΛूΊΔ • ίϯςϯπΛղੳ͠ɺΫϥελϦϯάɺΧςΰϥ Πζ • ղੳޙͷίϯςϯπΛͬͨwebαʔϏεΛ։ ൃ͢Δ • ϢʔβʔߦಈΛղੳͤ͞ɺϢʔβʔʹఏڙ͢Δί
ϯςϯπΛֶशɺมԽͤ͞Δ αʔϏεཁ݅ ͜͜PythonͰΔͱͯ͠
• εΫϨΠϐϯάͰwebίϯςϯπΛूΊΔ • ίϯςϯπΛղੳ͠ɺΫϥελϦϯάɺΧςΰϥ Πζ • ղੳޙͷίϯςϯπΛͬͨwebαʔϏεΛ։ ൃ͢Δ • ϢʔβʔߦಈΛղੳ͠ɺϢʔβʔʹఏڙ͢Δίϯ
ςϯπΛֶशɺมԽͤ͞Δ αʔϏεཁ݅ ͜͜ΛSwiftͰΔ͜ͱʹ͠·ͨ͠
• ΫϥυαʔϏεͲΕΛ͏ʁ • ϑϨʔϜϫʔΫͲ͏͢Δʁ • σʔλϕʔεͲ͏͢Δʁ • ΞϓϦWebͳͲͷΫϥΠΞϯτͱͲ͏௨৴͢ Δʁ •
Swiftαʔόʔͷӡ༻ͱσϓϩΠͲ͏͢Δʁ SwiftͰWebαʔϏεΛ࡞Δͱ͍ͬͯʁ
ΫϥυαʔϏεԿΛ͏ʁ • SwiftόΠφϦͳͷͰVM͕ͳ͍ɻͦͷͨΊޓ ੑͷ͋ΔOSͰ͋ΕͳΜͰOK • Ubuntu্ͰόΠφϦͷ࣮ߦɺσʔϞϯԽ͕ग़དྷ ΔͷͰ͋ΕOK • ͋ͱɺ͓ۚ࣍ୈ
• SwiftͷWebϑϨʔϜϫʔΫଟ͘ଘࡏ͢Δ • ༗ྗͳͷɺKitura or vapor͔ʢຊ൪ӡ༻ͷ࣮͕ ෆ໌ͳ্ɺރΕ͍ͯΔͱݴ͑ͳ͍ʣ • ίϛϡχςΟͷنͱ։ൃεϐʔυΛॏࢹ •
ݴޠͷؚΊɺ1ޙʹશ͘ҧ͏ͷʹͳͬͯ ͍ΔՄೳੑ͕ߴ͍ɻࠜؾڧ͘ϝϯς͢Δ֮ޛ͕ඞཁ • ։ൃ͕͍͖ͳΓStop͢Δةݥੑ(Slimane͕ͦ͏…) ϑϨʔϜϫʔΫͲ͏͢Δʁ
• ͋·ΓϓϩάϥϜݴޠؔͳ͍ • αʔϏεཁ݅࣍ୈͰԿΛ͏ͷࣗ༝ • ͨͩ͠ɺSwiftͰDBʹΑͬͯυϥΠό͕ଘࡏ ͠ͳ͍ or ރΕ͍ͯͳ͍ͨΊࣗ࡞͢Δඞཁ͕͋Δ ߹
σʔλϕʔεͲ͏͢Δʁ
• HTTP/1.1ΛͬͨRest API+JSON͕Ұൠత͔ • apple͕ެࣜͰSwift൛protocol buffersΛαϙʔτ ͍ͯͯ͠ɺਓؾ͕ٸ্ঢத • γϯάϧϖʔδωΠςΟϒͰͳ͍߹ɺαʔ όʔαΠυϨϯμϦϯάͰOKʢSwiftͷςϯ
ϓϨʔτΤϯδϯෳݸଘࡏ͢Δʣ ΞϓϦWebͳͲͷΫϥΠΞϯτͱͲ͏௨৴͢Δʁ
• SwiftαʔόʔΛී௨ʹཱͪ͋͛ͯӡ༻͢Δ߹ɺ super visorͰϓϩηεΛࢹ͢Δͷ͕Ұൠత • Docker(Ubuntu)ͳ͑͘Δ • VMݴޠͰͳ͍ͷͰɺӡ༻͢ΔOSͰϏϧυ͢Δඞཁ ͕͋Δ •
CI Tool(Ubuntu)ͰϏϧυͯͦ͠ͷόΠφϦΛσϓϩΠ ͢Δ͔ɺDocker ContainerΛ࡞ͬͯσϓϩΠ͕Ұൠత Swiftαʔόʔͷӡ༻ͱσϓϩΠͲ͏͢Δʁ
Ҏ্ͷ͜ͱΛߟྀ͢ΕɺSwiftͰαʔόʔ։ൃ͕ՄೳͰ͢
·ͨɺ͜Ε·Ͱͷ·ͱΊͱͯ͠
ϝδϟʔͳαʔόʔݴޠͱൺֱͯ͠ɺ ಉ༷ͳɾҟͳΔগ͠ݟ͍͖ͯ·͠ΐ͏
ଞͷϝδϟʔͳWeb։ൃݴޠͱಉ༷ͳ • ΫϥυαʔϏε • ϛυϧΣΞDB • جຊతͳWebαʔϏεͷߏ • جຊతͳαʔόʔΞʔΩςΫνϟ(preforkɺevent driven
etc..) • ΫϥΠΞϯτͱͷ௨৴पΓ • CIɺςετڥ
• ͦͦຊ൪ӡ༻࣮͕ஶ͘͠গͳ͍ • ެࣜαϙʔτͷOS͕·ͩ·ͩଟ͘ͳ͍(LinuxͰDestribution ʹΑͬͯಈ͔ͳ͍͜ͱ) • ݴޠͱϑϨʔϜϫʔΫͷ͕ૣ͍ͨΊɺࠜؾڧ͍ϝϯς͕ ඞཁ • SPMͰ͑ΔϥΠϒϥϦ·ͩ·ͩগͳ͍ͨΊɺࣗ࡞͢Δ
ͷ͕ଟ͍ (AWS-SDKͱ͔ͱ͔…) • ΫϥΠΞϯτ(iOSɺMac)ͱαʔόʔؒͰιʔείʔυͷڞ༗ ͕Ͱ͖Δ ଞͷϝδϟʔͳWeb։ൃݴޠͱҟͳΔ
Ͱɺ͜ΕΒΛ౿·͑
ࠓճͷαʔϏεͰɺ ͲͷΑ͏ʹ։ൃɾӡ༻͍͔ͯ͘͠Λൃද͍͖ͯ͠·͢
• ΫϥυαʔϏεAWS • GRPCΛ࠾༻͠ɺWebϑϨʔϜϫʔΫΘͳ͍ • ௨৴ͷγϦΞϥΠζϑΥʔϚοτʹprotocol buffersΛ ར༻ʢwebJSONʹϑΥʔϧόοΫʣ • σʔλϕʔεʹMysql
5.7Λ࠾༻ • ΞϓϦReactNativeͰ࡞Δ͔ • Circle CI͔ΒϏϧυͱσϓϩΠΛߦ͏ ߏͱେ
αʔόʔߏਤ εΫϨΠϐϯά Lambda ղੳ JSON HTTP/1.1 ALB ECS GRPC Service
ECS JSON <-> PB Proxy Service JSON HTTP/2.0
• Swiftαʔόʔӡ༻ͳ͘ग़དྷΔ • αʔόʔϨεӡ༻͕͍͢͠ (εΫϨΠϐϯά lambdaΛఆظ࣮ߦ • ECS͕ศར͗͢Δ • http/2.0͕ALBͰ͑Δ
QɿΫϥυαʔϏεʁ AɿΫϥυαʔϏεAWS
• ࠓճͷαʔϏεͰɺෳࡶͳRoutingɺαʔ όʔαΠυϨϯμϦϯά͕ෆཁ • ਖ਼ɺGraphQL͔ͳΓؾʹͳΔ • GRPCͷଞɺ͓खͷWebαʔόʔΛΞϓϦέʔ γϣϯαʔόʔͱͯ͠͏ʢޙड़ʣ • Model෦ͷΈɺ͓खͷQueryBuilderΛ༻͍Δ
ʢޙड़ʣ QɿϑϨʔϜϫʔΫʁ AɿGRPCΛ࠾༻͠ɺϑϨʔϜϫʔΫΘͳ͍
GRPCͱʁ www.grpc.io • googleͷRPCϑϨʔϜϫʔΫ • సૹϑΥʔϚοτ͕protocol buffers • ௨৴ؚ·ΕΔ •
HTTP/2.0ඞਢ • ༷ʑͳݴޠ༻ʹplugin͕͋Δ • ϚΠΫϩαʔϏεؒͷ௨৴ʹgoogleͰ͍ͬͯΔΒ͍͠
Swift༻ͷϓϥάΠϯ͋Γ·͢
• Amazon AuroraͷҠ২Λߟྀ • ϊϯεΩʔϚͳεΫϨΠϐϯάσʔλJSONܕͰ อଘ͢Δ(MongoESͷӡ༻͠ΜͲ͗͢) • ͋Δ͍ɺεΫϨΠϐϯάσʔλΛS3ʹอଘ͠ Amazon AthenaͰݕࡧͰ͖ͨΒເͷΑ͏
• ͓खͷMYSQLυϥΠόͰ௨৴͢Δ Qɿσʔλϕʔεʁ A : Mysql5.7Λ࠾༻
• ωΠςΟϒΫϥΠΞϯτͱɺϚΠΫϩαʔϏεؒͷ ௨৴protocol-buffers • webΫϥΠΞϯτͷ௨৴JSONʹϑΥʔϧόοΫ • Androidઐଐͷ։ൃऀ͕͍ͳ͍ͷͰɺωΠςΟϒΫ ϥΠΞϯτReactNativeͰΔՄೳੑ͕ߴ͍ Q :
ΫϥΠΞϯτͱͷ௨৴ʁ AɿGRPC(protocol-buffers)ͱHTTP/1.1+JSON
• swift buildޙʹdocker containerΛCircleCI্Ͱ࡞ • CircleCI͔ΒECSͷΦʔέετϨʔγϣϯΛ࣮ߦ • Blue/GreenσϓϩΠϝϯτ •
ΑͬͯɺSwiftઐ༻ͷσϓϩΠπʔϧෆཁ Q : σϓϩΠͲ͏͢Δʁ A : Circle CIͰϏϧυͱσϓϩΠΛߦ͏
σϓϩΠϑϩʔਤ git push hook ɾϚΠάϨʔγϣϯ ɾσϓϩΠ ɾdockerίϯςφͷΓସ͑ ɾswift build ɾςετ
ɾ֤छdockerίϯςφͷ࡞
·ͣɺ͜ͷߏͰαʔϏε։ൃͱӡ༻ Λߦ͍ͬͯ͜͏ͱࢥ͍·͢ɻ
ͨͩ͠ɺϑϨʔϜϫʔΫΛΘͳ͍ͨΊɺ ͜ͷ··͙͢ʹ։ൃʹೖΔͷͪΐͬͱେมͰ͢ɻ
• Webܥͷػೳ͕શવͳ͍(HTTPαʔόʔɺ WebSocketͳͲ) • LinuxͰಈ͔ͳ͍API͕·ͩ·ͩ͋Δ • ඇಉظॲཧDispatchQueueΛ͏͜ͱʹͳΔ ͕ɺcallbackϕʔεʹͳΔ • GCDthreadϕʔεͳͷͰɺϨʔείϯσΟγϣ
ϯ͕ා͍ Swiftඪ४Ͱ…
ͦ͜Ͱ
None
• SwiftͷωοτϫʔΫϓϩάϥϛϯάڥ (LinuxͰͪΌΜͱಈ͘) • ίʔϧόοΫϔϧΛओ؟ʹɺGOελΠϧͳฒྻॲཧ/ಉظػೳΛ ࣋ͭʢCoroutineͳ͍ʣ • TCPɺDNSͳͲجຊతͳωοτϫʔΫػೳΛCF**ΛΘͣʹఏڙ • HTTP(S)/1.1ɺWebSocketͷαʔόʔɺΫϥΠΞϯτΛఏڙ
• TLSʹlibresslΛ࠾༻ • NonBlocking I/OͱBlocking I/O+GCDΛΈ߹ΘͤͨϋΠϒϦου ϞσϧͳαʔόʔΞʔΩςΫνϟ(KituraΑΓߴ) Prorsum
SwiftͰඇಉظॲཧͷయܕྫ
͜ɺ͜Ε…
None
CallBack Hellʂ
·ͨɺThread ϓϩάϥϛϯάʹΑ͋͘Δͷ͕… IPA ηΩϡϦςΟηϯλʔ ϨʔείϯσΟγϣϯͷҰൠతରࡦΑΓ
·ͨɺThread ϓϩάϥϛϯάʹΑ͋͘Δͷ͕… IPA ηΩϡϦςΟηϯλʔ ϨʔείϯσΟγϣϯͷҰൠతରࡦΑΓ ϨʔείϯσΟγϣϯσουϩοΫ
Swift͜ΕΒͷΛજࡏతʹሃΉ…
ͨͩ͠
͜ΕΒΛ͏·͘ղܾͨ͠ݴޠ͕͋Γ·ͨ͠
None
ͦ͏ɺΈΜͳେ͖GoݴޠͰ͢
GoݴޠɺGoroutineͰฒྻ/ฒߦॲཧΛߦ͍ɺ ChannelͰಉظΛͱ͍ͬͯ·ͨ͠ɻ
Goroutine GoroutineෳThread্ʹଟॏԽ͞ΕͨCoroutineͷΑ͏ͳಈ࡞Λ͢Δɻ Ұͭͷgoroutine͕͍࣌ؒϒϩοΫ͢Α͏ͳؔͰɺผͷgoroutineʹhand off͠ ͯॲཧΛଓߦ͢ΔͨΊɺCPUΛ༨ΒͤͣʹޮతʹϓϩάϥϜΛ࣮ߦͰ͖Δ G1 G2 G3 G4 G5
sleep(1) G6 G4 G5 sleep(1) G6 G8 G4 G7 G9 thread1 thread2 thread3
Channel GorotuineThreadؒΛ·͍ͨͰ࣮ߦ͞ΕΔͨΊɺGoroutineಉ࢜ͰҰͭͷมΞΫ ηε͢ΔͱRace Condition͕ൃੜͯ͠͠·͏ɻ ͦΕΛ͙ͨΊʹɺGoͰChannelͱ͍͏ػೳ͕ఏڙ͞Ε͓ͯΓɺͦΕΛͬͯ Λૹड৴͢Δ G1 G2 G3 send
send send main thread receive Channel
ProrsumͰɺgoɺchannelɺselectͳͲΛSwiftʹҠ২ (ͨͩ͠ɺProrosumͷgoϢʔβʔϥϯυͷίϯςΩετεΠονͳ͍)
SwiftͰgoɺchannelɺselectΛ
HTTPαʔόʔNode.jsͷΑ͏ʹΧδϡΞϧʹ͑·͢
None
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
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
͜ͷHTTPαʔόʔΛࠓճͷΞϓϦέʔγϣϯαʔόʔͱͯ͠ Քಇͤ͞ɺಈతͳHTMLͷ৴ɺgrpc <-> http1.1ͷϓϩΩγ αʔόͱͯ͠ར༻͠·͢ɻ
https://speakerdeck.com/noppoman/swiftnishi-sitasabaakitekutiyawozai- kao-siteshi-zhuang-madesitemiru TSSS meetup #5ࢿྉ https://medium.com/@yukitakei/rethink-appropriate-server-architecture-for- swift-7c8513944db8#.ds9jhnmk8 Medium Article ͞Βʹৄ͘͠Γ͍ͨํ….
ProrsumΛ͏͜ͱͰɺओཁͳωοτϫʔΫػೳͷ΄͔ ΑΓ؆୯ʹඇಉظ/ฒྻϓϩάϥϛϯά͕ՄೳʹͳΓ·͢ɻ
࣍ʹɺσʔλϕʔεૢ࡞ɺ ϚΠάϨʔγϣϯͲ͏͠·͠ΐ͏
• SwiftͷMysqlυϥΠόԿΛͬͨΒ͍͍ͷ͔ • Ͱ͖ΕMigration·Ͱαϙʔτͯ͠΄͍͠ • ϕλͰSQLΛॻ͘ͷϝϯς͕ޙʑେม • DispatchQueueॏ͍ΫΤϦͩͱthread͕ϒϩοΫ ͞ΕΔͨΊɺඇಉظI/O͕͑Δͱخ͍͠έʔε
ͯ͢ͷཁ݅ʹ͋ͯ·ΔυϥΠό͋Γ·ͤΜͰͨ͠ (´ʀωʀʆ)
ͳͷͰɺ࡞Γ·ͨ͠ʢসʣ
None
SwiftKenxͱ • Pure Swift Mysql Client + Query Builder •
libmysqlclientʹґଘ͠ͳ͍ • TCPϨΠϠʔProrsum • εΩʔϚϚΠάϨʔγϣϯػೳΛαϙʔτ • SwiftݴޠͰදݱྗͷߴ͍SQL͕ॻ͚Δ • ܕηʔϑͳfetchɺinsert • ඇಉظI/OɺଟॏԽͷαϙʔτʢ͜Ε͔Βʣ
None
None
None
None
None
Migration
MigrationΫϥε
migration࣮ߦϑΝΠϧ(Migration/main.swift)
None
None
αʔϏε։ൃΛ͠ͳ͕ΒඞཁͳػೳΛॱ͍͖࣍ͯ͠·͢
͞Βʹৄ͘͠ɺnoppoMan/SwiftKnex
࠷ޙʹ
None
appleެࣜͷαʔόʔAPIϓϩδΣΫτ͕࢝ಈ͍ͯ͠·͢
• RFC 7230-7235(HTTP/1.1) • RFC 6455 (WebSocket) • RFC 7540(HTTP/2.0)
• TCP/IP(v6,v4), UDP including I/O • TLS/Encryption ରҎԼ
ެࣜAPI͕ग़ͨλΠϛϯάͰɺ ΒͷαʔϏεஞ࣍Ҡ২Λߦ͏ͭΓͰ͢ɻ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠