Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
gRPC
Moyuru Aizawa
September 15, 2017
Programming
2
860
gRPC
Moyuru Aizawa
September 15, 2017
Tweet
Share
More Decks by Moyuru Aizawa
See All by Moyuru Aizawa
lvla
1
170
lvla
1
51
lvla
2
1.7k
lvla
5
370
lvla
1
67
lvla
2
220
lvla
3
310
lvla
2
180
lvla
3
3.4k
Other Decks in Programming
See All in Programming
decoc
1
350
aratayokoyama
0
220
takahi5
0
240
pyama86
1
100
kazaman97
0
190
nanimonodemonai
2
1.4k
nbkouhou
0
980
line_developers_tw2
0
770
zsmb
2
110
rishitdagli
0
180
line_developers_tw
0
1.4k
line_developers_tw
0
550
Featured
See All Featured
phodgson
87
3.9k
addyosmani
1348
190k
bryan
100
11k
danielanewman
200
20k
orderedlist
PRO
329
35k
smashingmag
229
18k
keathley
18
650
aarron
258
36k
geoffreycrofte
19
800
ddemaree
274
31k
pedronauck
652
110k
ammeep
656
54k
Transcript
gRPC @MoyuruAizawa
Ѫᖒ๖ (Moyuru Aizawa) MoyuruAizawa Contract Developer Pairs Div. Eureka, Inc.
FRESH! Div. CyberAgent, Inc.
None
‣ Live Streaming ‣ Upcoming ‣ Live ‣ Pause ‣
… FRESH!
FRESH!
Polling
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause
Polling Client Server Live Pause ແବ!
Polling Client Server Live Pause ͍!!
gRPC
‣ Google ‣ Remote Procedure Call ‣ HTTP/2 ‣ ѹॖϔομʔ
‣ αʔόʔϓογϡ ‣ Protocol Buffers ‣ ετϦʔϜ ‣ … gRPC
gRPC
gRPC Channel(OkHttp2) Binary data
gRPC Serialize/Deserialize … FooServiceStub
Plasma
‣ FRESH!։ൃ ‣ gRPC Stream, Server Sent EventΛ༻͍ͨ൚༻తͳServer Pushϛυϧ ΣΞ
‣ ΫϥΠΞϯτPlasmaʹରͯ͠subscribe͍ͨ͠EventΛૹΔ ‣ Πϕϯτൃੜ࣌ʹPlasma͕ΫϥΠΞϯτʹରͯ͠ใΛૹΔ ‣ https://blog.stormcat.io/post/entry/openfresh-plasma/ Plasma
gRPC Client Plasma
gRPC Client Plasma Connect to server
gRPC Client Plasma Connect to server subscribe Event1
gRPC Client Plasma Connect to server subscribe Event1 Event1
gRPC Client Plasma Connect to server subscribe Event1 Event1
gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe
Event2
gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe
Event2 Event2
gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe
Event2 Event2
gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe
Event2 Event1 Event2
gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe
Event2 Event1 Event2
gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe
Event2 Event1 Event2 ແବͳϦΫΤετͷݮ ϦΞϧλΠϜ
TIPS
‣ Protobuf ‣ ProtoϑΝΠϧͷσʔλఆٛΛݩʹSerialize/Deserializeίʔυͷੜ ɺσʔλΫϥεͷੜ ‣ ProtoϑΝΠϧͷαʔϏεఆٛΛݩʹStubΫϥεͷੜ ‣ Channel ‣
αʔόʔͱͷίωΫγϣϯ ‣ Stub ‣ αʔϏε gRPC
Kotlin and Protobuf
Kotlin and Protobuf
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'com.google.protobuf' apply
plugin: 'com.android.application' apply plugin: 'com.google.protobuf' apply plugin: 'kotlin-android' Kotlin and Protobuf
Channel and Lifecycle
ManagedChannel and Lifecycle onCreate
ManagedChannel and Lifecycle onCreate new ManagedChannel()
ManagedChannel and Lifecycle onCreate new ManagedChannel() FooServiceStubGrpc.newStub(channel)
ManagedChannel and Lifecycle onCreate new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()
ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()
ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() the system will simply
kill the activity's hosting process without calling onDestroy new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()
ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()
αʔόʔʹແବͳίωΫγϣϯ͕ཷ·͍ͬͯ͘ ΞϓϦ͕όοΫάϥϯυͰ௨৴ͯ͠͠·͏
ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()
onStart onStop
‣ onStart-onStopͰίωΫγϣϯΛுΔ ‣ ίωΫγϣϯ͕ΕͨStub͑ͳ͘ͳΔͷͰɺStubຖੜ͠ ͢ ‣ FRESH!ͰgRPCͰ௨৴͢Δ༻ͷΫϥεΛ࡞ɻαʔόʔͱͷଓ ͱɺϦΫΤετΛ·ͱΊͨɻ ManagedChannel and
Lifecycle
Reconnection
‣ Channel: αʔόʔͱͷίωΫγϣϯ ‣ ManagedChannel:αʔόʔ͕μϯ͢Δɺ͕ΦϑϥΠϯʹͳΔͳ ͲʹΑͬͯίωΫγϣϯ͕Εͯɺ࠶ଓΛߦͬͯ͘ΕΔɻ Reconnection
Reconnection ͍͔ͭΒManagedChannel͕ૉૣ͘ ࠶ଓͯ͘͠ΕΔͱࡨ͍֮ͯͨ͠?
‣ ͔ͨ͠ʹɺ࠶ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧΣΞͳͷͰɺଓ͕Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ Reconnection
‣ ͔ͨ͠ʹɺ࠶ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧΣΞͳͷͰɺଓ͕Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ ‣ ଓঢ়ଶͷมߋΛࢹ͍ͨ͠ͳ͊….
Reconnection
‣ ͔ͨ͠ʹɺ࠶ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧΣΞͳͷͰɺଓ͕Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ ‣ ଓঢ়ଶͷมߋΛࢹ͍ͨ͠ͳ͊….
Reconnection
TLS 1.2
OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS)
.build() Build ManagedChannel
OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS)
.build() Build ManagedChannel
OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS)
.build() Build ManagedChannel Android 4.1-4.4Ͱ TLS 1.2͕ར༻Ͱ͖ͳ͍
class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun
getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites override fun createSocket(s: Socket?, host: String?, port: Int, autoClose: Boolean): Socket = delegate.createSocket(s, host, port, autoClose).let(this::patch) override fun createSocket(host: String?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(host: String?, port: Int, localHost: InetAddress?, localPort: Int): Socket = delegate.createSocket(host, port, localHost, localPort).let(this::patch) override fun createSocket(host: InetAddress?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(address: InetAddress?, port: Int, localAddress: InetAddress?, localPort: Int): Socket = delegate.createSocket(address, port, localAddress, localPort).let(this::patch) override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites private fun patch(s: Socket): Socket = s.apply { if (this is SSLSocket) enabledProtocols = arrayOf("TLSv1.2") } } Enable TLS 1.2 https://github.com/square/okhttp/issues/2372
class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun
getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites override fun createSocket(s: Socket?, host: String?, port: Int, autoClose: Boolean): Socket = delegate.createSocket(s, host, port, autoClose).let(this::patch) override fun createSocket(host: String?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(host: String?, port: Int, localHost: InetAddress?, localPort: Int): Socket = delegate.createSocket(host, port, localHost, localPort).let(this::patch) override fun createSocket(host: InetAddress?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(address: InetAddress?, port: Int, localAddress: InetAddress?, localPort: Int): Socket = delegate.createSocket(address, port, localAddress, localPort).let(this::patch) override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites private fun patch(s: Socket): Socket = s.apply { if (this is SSLSocket) enabledProtocols = arrayOf("TLSv1.2") } } Enable TLS 1.2 https://github.com/square/okhttp/issues/2372
class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun
getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites override fun createSocket(s: Socket?, host: String?, port: Int, autoClose: Boolean): Socket = delegate.createSocket(s, host, port, autoClose).let(this::patch) override fun createSocket(host: String?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(host: String?, port: Int, localHost: InetAddress?, localPort: Int): Socket = delegate.createSocket(host, port, localHost, localPort).let(this::patch) override fun createSocket(host: InetAddress?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(address: InetAddress?, port: Int, localAddress: InetAddress?, localPort: Int): Socket = delegate.createSocket(address, port, localAddress, localPort).let(this::patch) override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites private fun patch(s: Socket): Socket = s.apply { if (this is SSLSocket) enabledProtocols = arrayOf("TLSv1.2") } } Enable TLS 1.2 SSLSocketFactoryΛϥοϓͯ͠TLS 1.2Λ༗ޮʹ͢Δ https://github.com/square/okhttp/issues/2372
None
FRESH!
FRESH!
‣ ଈ࠲ʹαʔόʔ͔Βใ͕ඈΜͰ͖ͯ࠷ߴ ‣ ίωΫγϣϯʹؾΛΔඞཁ͕͋Δ ‣ HTTP/1.1ʹແ͔ͬͨ֓೦ͳͷͰ׳ΕΔ·Ͱେม ‣ AndroidͰϓϩμΫγϣϯʹಋೖ͕ͨ͠গͳ͍ ‣ ࢼߦࡨޡͷ࿈ଓ
Conclusion
Thank you