Slide 1

Slide 1 text

gRPC @MoyuruAizawa

Slide 2

Slide 2 text

Ѫᖒ๖ (Moyuru Aizawa) MoyuruAizawa Contract Developer Pairs Div. Eureka, Inc. FRESH! Div. CyberAgent, Inc.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

‣ Live Streaming ‣ Upcoming ‣ Live ‣ Pause ‣ … FRESH!

Slide 5

Slide 5 text

FRESH!

Slide 6

Slide 6 text

Polling

Slide 7

Slide 7 text

Polling Client Server Live Pause

Slide 8

Slide 8 text

Polling Client Server Live Pause

Slide 9

Slide 9 text

Polling Client Server Live Pause

Slide 10

Slide 10 text

Polling Client Server Live Pause

Slide 11

Slide 11 text

Polling Client Server Live Pause

Slide 12

Slide 12 text

Polling Client Server Live Pause

Slide 13

Slide 13 text

Polling Client Server Live Pause

Slide 14

Slide 14 text

Polling Client Server Live Pause

Slide 15

Slide 15 text

Polling Client Server Live Pause ແବ!

Slide 16

Slide 16 text

Polling Client Server Live Pause ஗͍!!

Slide 17

Slide 17 text

gRPC

Slide 18

Slide 18 text

‣ Google ‣ Remote Procedure Call ‣ HTTP/2 ‣ ѹॖϔομʔ ‣ αʔόʔϓογϡ ‣ Protocol Buffers ‣ ετϦʔϜ ‣ … gRPC

Slide 19

Slide 19 text

gRPC

Slide 20

Slide 20 text

gRPC Channel(OkHttp2) Binary data

Slide 21

Slide 21 text

gRPC Serialize/Deserialize
 … FooServiceStub

Slide 22

Slide 22 text

Plasma

Slide 23

Slide 23 text

‣ FRESH!։ൃ ‣ gRPC Stream, Server Sent EventΛ༻͍ͨ൚༻తͳServer Pushϛυϧ ΢ΣΞ ‣ ΫϥΠΞϯτ͸Plasmaʹରͯ͠subscribe͍ͨ͠EventΛૹΔ ‣ Πϕϯτൃੜ࣌ʹPlasma͕ΫϥΠΞϯτʹରͯ͠৘ใΛૹΔ ‣ https://blog.stormcat.io/post/entry/openfresh-plasma/ Plasma

Slide 24

Slide 24 text

gRPC Client Plasma

Slide 25

Slide 25 text

gRPC Client Plasma Connect to server

Slide 26

Slide 26 text

gRPC Client Plasma Connect to server subscribe Event1

Slide 27

Slide 27 text

gRPC Client Plasma Connect to server subscribe Event1 Event1

Slide 28

Slide 28 text

gRPC Client Plasma Connect to server subscribe Event1 Event1

Slide 29

Slide 29 text

gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe Event2

Slide 30

Slide 30 text

gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe Event2 Event2

Slide 31

Slide 31 text

gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe Event2 Event2

Slide 32

Slide 32 text

gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe Event2 Event1 Event2

Slide 33

Slide 33 text

gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe Event2 Event1 Event2

Slide 34

Slide 34 text

gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe Event2 Event1 Event2 ແବͳϦΫΤετͷ࡟ݮ ϦΞϧλΠϜ

Slide 35

Slide 35 text

TIPS

Slide 36

Slide 36 text

‣ Protobuf ‣ ProtoϑΝΠϧͷσʔλఆٛΛݩʹSerialize/Deserializeίʔυͷੜ ੒ɺσʔλΫϥεͷੜ੒ ‣ ProtoϑΝΠϧͷαʔϏεఆٛΛݩʹStubΫϥεͷੜ੒ ‣ Channel ‣ αʔόʔͱͷίωΫγϣϯ ‣ Stub ‣ αʔϏε gRPC

Slide 37

Slide 37 text

Kotlin and Protobuf

Slide 38

Slide 38 text

Kotlin and Protobuf

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Channel and Lifecycle

Slide 41

Slide 41 text

ManagedChannel and Lifecycle onCreate

Slide 42

Slide 42 text

ManagedChannel and Lifecycle onCreate new ManagedChannel()

Slide 43

Slide 43 text

ManagedChannel and Lifecycle onCreate new ManagedChannel() FooServiceStubGrpc.newStub(channel)

Slide 44

Slide 44 text

ManagedChannel and Lifecycle onCreate new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()

Slide 45

Slide 45 text

ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()

Slide 46

Slide 46 text

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()

Slide 47

Slide 47 text

ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar() αʔόʔʹແବͳίωΫγϣϯ͕ཷ·͍ͬͯ͘ ΞϓϦ͕όοΫάϥ΢ϯυͰ΋௨৴ͯ͠͠·͏

Slide 48

Slide 48 text

ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar() onStart onStop

Slide 49

Slide 49 text

‣ onStart-onStopͰίωΫγϣϯΛுΔ ‣ ίωΫγϣϯ͕੾ΕͨStub͸࢖͑ͳ͘ͳΔͷͰɺStub΋ຖ౓ੜ੒͠௚ ͢ ‣ FRESH!Ͱ͸gRPCͰ௨৴͢Δ༻ͷΫϥεΛ࡞੒ɻαʔόʔͱͷ઀ଓ ͱɺϦΫΤετΛ·ͱΊͨɻ ManagedChannel and Lifecycle

Slide 50

Slide 50 text

Reconnection

Slide 51

Slide 51 text

‣ Channel: αʔόʔͱͷίωΫγϣϯ ‣ ManagedChannel:αʔόʔ͕μ΢ϯ͢Δɺ୺຤͕ΦϑϥΠϯʹͳΔͳ ͲʹΑͬͯίωΫγϣϯ͕੾Εͯ΋ɺ࠶઀ଓΛߦͬͯ͘ΕΔɻ Reconnection

Slide 52

Slide 52 text

Reconnection ͍͔ͭΒManagedChannel͕ૉૣ͘ ࠶઀ଓͯ͘͠ΕΔͱࡨ͍֮ͯͨ͠?

Slide 53

Slide 53 text

‣ ͔ͨ͠ʹɺ࠶઀ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺ΍ͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶઀ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧ΢ΣΞͳͷͰɺ઀ଓ͕੾Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ Reconnection

Slide 54

Slide 54 text

‣ ͔ͨ͠ʹɺ࠶઀ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺ΍ͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶઀ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧ΢ΣΞͳͷͰɺ઀ଓ͕੾Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ ‣ ઀ଓঢ়ଶͷมߋΛ؂ࢹ͍ͨ͠ͳ͊…. Reconnection

Slide 55

Slide 55 text

‣ ͔ͨ͠ʹɺ࠶઀ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺ΍ͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶઀ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧ΢ΣΞͳͷͰɺ઀ଓ͕੾Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ ‣ ઀ଓঢ়ଶͷมߋΛ؂ࢹ͍ͨ͠ͳ͊…. Reconnection

Slide 56

Slide 56 text

TLS 1.2

Slide 57

Slide 57 text

OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS) .build() Build ManagedChannel

Slide 58

Slide 58 text

OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS) .build() Build ManagedChannel

Slide 59

Slide 59 text

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͕ར༻Ͱ͖ͳ͍

Slide 60

Slide 60 text

class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun getDefaultCipherSuites(): Array = 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 = 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

Slide 61

Slide 61 text

class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun getDefaultCipherSuites(): Array = 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 = 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

Slide 62

Slide 62 text

class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun getDefaultCipherSuites(): Array = 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 = 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

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

FRESH!

Slide 65

Slide 65 text

FRESH!

Slide 66

Slide 66 text

‣ ଈ࠲ʹαʔόʔ͔Β৘ใ͕ඈΜͰ͖ͯ࠷ߴ ‣ ίωΫγϣϯʹؾΛ഑Δඞཁ͕͋Δ ‣ HTTP/1.1ʹ͸ແ͔ͬͨ֓೦ͳͷͰ׳ΕΔ·Ͱେม ‣ AndroidͰϓϩμΫγϣϯʹಋೖͨ͠࿩͕গͳ͍ ‣ ࢼߦࡨޡͷ࿈ଓ Conclusion

Slide 67

Slide 67 text

Thank you