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
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
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
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
130
The Rise of LLMOps
asei
7
1.7k
飲食店データの分析事例とそれを支えるデータ基盤
kimujun
0
160
IBC 2024 動画技術関連レポート / IBC 2024 Report
cyberagentdevelopers
PRO
1
110
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
170
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
540
The Role of Developer Relations in AI Product Success.
giftojabu1
0
140
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
330
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
第1回 国土交通省 データコンペ参加者向け勉強会③- Snowflake x estie編 -
estie
0
130
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
660
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
297
20k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Statistics for Hackers
jakevdp
796
220k
Optimizing for Happiness
mojombo
376
70k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
430
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
GitHub's CSS Performance
jonrohan
1030
460k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
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͕ग़ͨλΠϛϯάͰɺ ΒͷαʔϏεஞ࣍Ҡ২Λߦ͏ͭΓͰ͢ɻ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠