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
Platform Engineering for Software Developers and Architects
syntasso
1
510
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
370
AGIについてChatGPTに聞いてみた
blueb
0
130
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.6k
B2B SaaS × AI機能開発 〜テナント分離のパターン解説〜 / B2B SaaS x AI function development - Explanation of tenant separation pattern
oztick139
2
220
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
The Rise of LLMOps
asei
5
1.3k
いざ、BSC討伐の旅
nikinusu
2
780
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
190
ドメイン名の終活について - JPAAWG 7th -
mikit
33
20k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
5
590
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Writing Fast Ruby
sferik
627
61k
Done Done
chrislema
181
16k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
860
Embracing the Ebb and Flow
colly
84
4.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Optimizing for Happiness
mojombo
376
70k
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͕ग़ͨλΠϛϯάͰɺ ΒͷαʔϏεஞ࣍Ҡ২Λߦ͏ͭΓͰ͢ɻ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠