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
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
240
Server Side Swift の個人的な利用シーン
noppoman
2
580
Serverless Server Side Swift
noppoman
2
1.6k
Practical Hexaville(ja)
noppoman
0
460
実践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
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
WINTICKETアプリで実現した高可用性と高速リリースを支えるエコシステム / winticket-eco-system
cyberagentdevelopers
PRO
1
190
GitHub Universe: Evaluating RAG apps in GitHub Actions
pamelafox
0
170
【若手エンジニア応援LT会】AWSで繋がり、共に成長! ~コミュニティ活動と新人教育への挑戦~
kazushi_ohata
0
180
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
49k
プロダクトエンジニアが活躍する環境を作りたくて 事業責任者になった話 ~プロダクトエンジニアの行き着く先~
gimupop
1
460
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
30万人が利用するチャットをFirebase Realtime DatabaseからActionCableへ移行する方法
ryosk7
5
330
とあるユーザー企業におけるリスクベースで考えるセキュリティ業務のお話し
4su_para
3
320
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
君は隠しイベントを見つけれるか?
mujyun
0
280
Shift-from-React-to-Vue
calm1205
3
1.3k
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
95
5.2k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
328
21k
Building Your Own Lightsaber
phodgson
102
6k
Faster Mobile Websites
deanohume
304
30k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Docker and Python
trallard
40
3.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
Designing Experiences People Love
moore
138
23k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
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͕ग़ͨλΠϛϯάͰɺ ΒͷαʔϏεஞ࣍Ҡ২Λߦ͏ͭΓͰ͢ɻ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠