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
270
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
420
Server Side Framework Slimaneの紹介
noppoman
1
1.1k
Other Decks in Technology
See All in Technology
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
180
なぜCodeceptJSを選んだか
goataka
0
160
アップデート紹介:AWS Data Transfer Terminal
stknohg
PRO
0
180
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
マイクロサービスにおける容易なトランザクション管理に向けて
scalar
0
110
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
380
フロントエンド設計にモブ設計を導入してみた / 20241212_cloudsign_TechFrontMeetup
bengo4com
0
1.9k
Storage Browser for Amazon S3
miu_crescent
1
140
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
UI State設計とテスト方針
rmakiyama
2
420
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
160
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
160
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Visualization
eitanlees
146
15k
Done Done
chrislema
181
16k
Scaling GitHub
holman
458
140k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
A Tale of Four Properties
chriscoyier
157
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Building Adaptive Systems
keathley
38
2.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
4 Signs Your Business is Dying
shpigford
181
21k
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͕ग़ͨλΠϛϯάͰɺ ΒͷαʔϏεஞ࣍Ҡ২Λߦ͏ͭΓͰ͢ɻ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠