Slide 1

Slide 1 text

Mohit Sarveiya gRPC with Kotlin Coroutines on Android @heyitsmohit

Slide 2

Slide 2 text

gRPC with Kotlin Coroutines ● Using gRPC on Android ● Authentication ● Retries & Hedging ● Debugging tools

Slide 3

Slide 3 text

RPC g RPC (Remote Procedure Call) Framework

Slide 4

Slide 4 text

• Polyglot implementation • Coroutines Support gRPC

Slide 5

Slide 5 text

Server(Go) gRPC Client(Kotlin) Client(Swift)

Slide 6

Slide 6 text

Server(Go) gRPC service Client(Kotlin) Client(Swift)

Slide 7

Slide 7 text

Server(Go) gRPC service service MyService { rpc subscribe( . . ) }

Slide 8

Slide 8 text

• Unary RPC call • Server streaming • Client streaming • Bidirectional streaming RPC Calls

Slide 9

Slide 9 text

gRPC service MyService { rpc subscribe( . . ) } Proto buffer file Protoc Service Class

Slide 10

Slide 10 text

• Generated class • Handles incoming requests • Send back data Service Class

Slide 11

Slide 11 text

Server(Go) gRPC service Client(Kotlin) Client(Swift)

Slide 12

Slide 12 text

Server(Go) gRPC service Client(Kotlin) Client(Swift) service service

Slide 13

Slide 13 text

gRPC service MyService { rpc subscribe( . . ) } Proto buffer file Protoc Client Stub

Slide 14

Slide 14 text

• Generated class. • Make Requests. • Creates response types. Client Stub

Slide 15

Slide 15 text

Server(Go) gRPC service Client(Kotlin) Client(Swift) Client Stub

Slide 16

Slide 16 text

Server(Go) gRPC service Client(Kotlin) Client(Swift) rpc rpc

Slide 17

Slide 17 text

Server(Go) gRPC service Client(Kotlin) Client(Swift) rpc rpc HTTP 2

Slide 18

Slide 18 text

Android gRPC Server

Slide 19

Slide 19 text

grpc/grpc-kotlin Code ! Issues Pull Requests gRPC-Kotlin/JVM - An RPC library and framework grpc-kotlin-stub v0.1.4 protoc-gen-grpc-kotlinstub v0.1.4 grpc-kotlin-stub-lite v0.1.4 Bazel Build passing Gradle Build passing A Kotlin/JVM implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first.

Slide 20

Slide 20 text

Use Case ● Tracks your route.

Slide 21

Slide 21 text

Use Case ● Tracks your route. ● Get a place from location.

Slide 22

Slide 22 text

Use Case ● Tracks your route. ● Get a place from location. ● List Places around location.

Slide 23

Slide 23 text

Use Case ● Tracks your route. ● Get a place from location. ● List Places around location. ● Chat with others at location. Shoreline Golf links 2940 N Shoreline Blvd Mountain View, CA 94043 Start typing

Slide 24

Slide 24 text

Android gRPC Server gRPC

Slide 25

Slide 25 text

gRPC service service MyService { rpc subscribe( . . ) } gRPC Server

Slide 26

Slide 26 text

protocolbuffers/protobuf Code ! Issues Pull Requests Protocol Buffers - Google’s data interchange format Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. Gradle Build passing

Slide 27

Slide 27 text

Service syntax = “proto3”; Protocol Buffer Version

Slide 28

Slide 28 text

Service syntax = “proto3”; service Places { } Declare Service

Slide 29

Slide 29 text

RPC Call Types ● Unary ● Server Streaming ● Client Streaming ● Bidirectional

Slide 30

Slide 30 text

Use Case ● Get a place from location 
 (Latitude, Longitude) Place

Slide 31

Slide 31 text

Unary RPC Call Client Server Location

Slide 32

Slide 32 text

syntax = “proto3”; service Places { rpc GetPlace(Location) returns (Place) {}; } Unary RPC Call keyword

Slide 33

Slide 33 text

Unary RPC Call Name of RPC call service Places { rpc GetPlace() }

Slide 34

Slide 34 text

Unary RPC Call service Places { rpc GetPlace(Location) } Request

Slide 35

Slide 35 text

Unary RPC Call service Places { rpc GetPlace(Location) returns (Place) {}; } Response

Slide 36

Slide 36 text

Unary RPC Call syntax = “proto3”; service Places { rpc GetPlace(Location) returns (Place) {}; } Unary RPC Call

Slide 37

Slide 37 text

Unary RPC Call syntax = “proto3”; service Places { rpc GetPlace(Location) returns (Place) {}; } Define messages

Slide 38

Slide 38 text

Unary RPC Call message Location { double latitude = 1; double longitude = 2; } } Keyword

Slide 39

Slide 39 text

Unary RPC Call service Places { message Location double latitude = 1; double longitude = 2; } } Message name

Slide 40

Slide 40 text

Unary RPC Call message Location { double latitude = 1; double longitude = 2; } Fields

Slide 41

Slide 41 text

message Location { double latitude = 1; double longitude = 2; } Unary RPC Call Type

Slide 42

Slide 42 text

message Location { double latitude = 1; double longitude = 2; } Unary RPC Call .proto Type C++ Java Go double double Double * f l oat64 int32 int32 int *int32 Int64 long int/long *int64 https://developers.google.com/protocol-bu f f ers/docs/overview#scalar

Slide 43

Slide 43 text

message Location { double latitude = 1; double longitude = 2; } Unary RPC Call Field numbers

Slide 44

Slide 44 text

Unary RPC Call syntax = “proto3”; service Places { rpc GetPlace(Location) returns (Place) {}; } Define Place

Slide 45

Slide 45 text

Unary RPC Call message Place { string name = 1; Location location = 2; } Name & Location

Slide 46

Slide 46 text

Unary RPC Call message Place { string name = 1; Location location = 2; 
 PlaceType placeType = 3; } Enum

Slide 47

Slide 47 text

Unary RPC Call message Place { 
 PlaceType placeType = 3; enum PlaceType { Landmark = 0; Driving_Range = 1; Golf_Course = 2; Restaurant = 3; Retail = 4; } Enum

Slide 48

Slide 48 text

Unary RPC Call message Place { string name = 1; Location location = 2; 
 PlaceType placeType = 3; enum PlaceType { … } 
 int64 checkins = 4; int64 comments = 5; } Int Scaler Types

Slide 49

Slide 49 text

Unary RPC Call syntax = “proto3”; service Places { rpc GetPlace(Location) returns (Place) {}; }

Slide 50

Slide 50 text

Use Case ● Check into place 
 Location Empty Response

Slide 51

Slide 51 text

Unary RPC Call syntax = “proto3”; service Places { rpc CheckIn(Location) returns () {}; }

Slide 52

Slide 52 text

Unary RPC Call syntax = “proto3”; service Places { rpc CheckIn(Location) returns () {}; } How do we return an empty?

Slide 53

Slide 53 text

Code ! Issues Pull Requests protocolbuffers/protobuf protobuf/src/google/protobuf/empty.proto message Empty { }

Slide 54

Slide 54 text

Well Known Types ● Empty ● Timestamp ● Duration

Slide 55

Slide 55 text

Unary RPC Call syntax = “proto3”; 
 import "google/protobuf/empty.proto"; service Places { rpc CheckIn(Place) returns () {}; } Import

Slide 56

Slide 56 text

Unary RPC Call syntax = “proto3”; service Places { rpc CheckIn(Location) returns (google.protobuf.Empty) {}; } How do we return an empty?

Slide 57

Slide 57 text

RPC Call Types ● Unary ● Server Streaming ● Client Streaming ● Bidirectional

Slide 58

Slide 58 text

Use Case ● List Places around location. Location(s) List

Slide 59

Slide 59 text

Server Streaming Client Server Location(s)

Slide 60

Slide 60 text

Server Streaming Client Server Stream of Places

Slide 61

Slide 61 text

Server Streaming service Places { rpc ListPlaces(Area) returns (stream Place) {}; } Server Streaming RPC Call

Slide 62

Slide 62 text

Server Streaming service Places { rpc ListPlaces(Area) returns (stream Place) {}; } Cluster of Locations

Slide 63

Slide 63 text

Server Streaming message Area { Location lo = 1; Location hi = 2; 
 } Cluster of Locations

Slide 64

Slide 64 text

Server Streaming service Places { rpc ListPlaces(Area) returns (stream Place) {}; } stream keyword

Slide 65

Slide 65 text

Server Streaming service Places { rpc ListPlaces(Area) returns (stream Place) {}; } Server Streaming RPC Call

Slide 66

Slide 66 text

RPC Call Types ● Unary ● Server Streaming ● Client Streaming ● Bidirectional

Slide 67

Slide 67 text

Use Case ● Tracks your route Location(s) 
 Trip Summary

Slide 68

Slide 68 text

Client Streaming Client Server Location(s)

Slide 69

Slide 69 text

Client Streaming Client Server Trip Summary

Slide 70

Slide 70 text

Client Streaming service Places { rpc RecordTrip(stream Location) returns (TripSummary) {}; }

Slide 71

Slide 71 text

Client Streaming service Places { rpc RecordTrip(stream Location) returns (TripSummary) {}; } Client Stream

Slide 72

Slide 72 text

Client Streaming service Places { rpc RecordTrip(stream Location) returns (TripSummary) {}; } Return single message

Slide 73

Slide 73 text

Client Streaming service Places { rpc RecordTrip(stream Location) returns (TripSummary) {}; }

Slide 74

Slide 74 text

RPC Call Types ● Unary ● Server Streaming ● Client Streaming ● Bidirectional

Slide 75

Slide 75 text

Use Case ● Chat with others at location ● Stream of messages Shoreline Golf links 2940 N Shoreline Blvd Mountain View, CA 94043 Start typing

Slide 76

Slide 76 text

Bidirectional RPC Call Client Server Comments

Slide 77

Slide 77 text

Bidirectional RPC Call Client Server Replies

Slide 78

Slide 78 text

Bidirectional RPC Call service Places { rpc Chat(stream Comment) returns (stream Comment) {}; } Bidirectional RPC Call

Slide 79

Slide 79 text

Bidirectional RPC Call service Places { rpc Chat(stream Comment) returns (stream Comment) {}; } stream keyword

Slide 80

Slide 80 text

RPC Call Types ● Unary ● Server Streaming ● Client Streaming ● Bidirectional

Slide 81

Slide 81 text

RPC Call Types service Places { rpc GetPlace(Location) returns (Place) {}; rpc ListPlaces(Area) returns (stream Place) {}; rpc CheckIn(Place) returns (google.protobuf.Empty) {}; rpc Chat(stream Comment) returns (stream Comment) {}; }

Slide 82

Slide 82 text

gRPC service MyService { rpc subscribe( . . ) } gRPC Server Service

Slide 83

Slide 83 text

Android gRPC Server gRPC Service Service

Slide 84

Slide 84 text

Android gRPC Client Stub

Slide 85

Slide 85 text

Generating Client Stub ● Class for messages ● Coroutine bindings for rpc calls

Slide 86

Slide 86 text

Generating Builders Proto buffer file Protoc Message Builders class Message { static class Builder { Builder set(double value) Location build() } }

Slide 87

Slide 87 text

Creating Messages message Location { double latitude = 1; double longitude = 2; } class Location { static class Builder { Builder setLatitude(double value) Builder setLongitude(double value) Location build() } }

Slide 88

Slide 88 text

Creating Messages message Location { double latitude = 1; double longitude = 2; } Location.newBuilder() .setLatitude(40.9888341) .setLongitude(-73.8502007) .build()

Slide 89

Slide 89 text

protocolbuffers/protobuf Code ! Issues Pull Requests Kotlin support request #3742 ! Open issue opened on Oct 12, 2017 comment on opened on Oct 12, 2017 Please support convert to Kotlin 168

Slide 90

Slide 90 text

Generating Service Proto buffer file Protoc Service class Service { fun rpcMethod1(): Flow 
 fun rpcMethod2(): Flow }

Slide 91

Slide 91 text

Generated Client class CoroutineStub { }

Slide 92

Slide 92 text

Generated Client class CoroutineStub { } rpc GetPlace(Location) returns (Place) {};

Slide 93

Slide 93 text

Generated Client class CoroutineStub { } rpc GetPlace(Location) returns (Place) {}; suspend fun getPlace(request: Location): Place

Slide 94

Slide 94 text

Generated Client class CoroutineStub { } rpc ListPlaces(Area) returns (stream Place) {};

Slide 95

Slide 95 text

Generated Client class CoroutineStub { } rpc ListPlaces(Area) returns (stream Place) {}; fun listPlaces(request: Area): Flow Stream is map to Flow

Slide 96

Slide 96 text

Generated Client class CoroutineStub { } rpc Chat(stream Comment) returns (stream Comment) {};

Slide 97

Slide 97 text

Generated Client class CoroutineStub { } rpc Chat(stream Comment) returns (stream Comment) {}; fun chat(requests: Flow): Flow Stream is map to Flow

Slide 98

Slide 98 text

Generated Client class CoroutineStub { } suspend fun getPlace(request: Location): Place fun recordTrip(requests: Flow): TripSummary fun listPlaces(request: Area): Flow fun chat(requests: Flow): Flow

Slide 99

Slide 99 text

• Create Managed Channel • Specify server url, port • Create Client Stub Using Client

Slide 100

Slide 100 text

Using Client val managedChannel = ManagedChannelBuilder .forAddress(host, port) .useTransportSecurity() .build()

Slide 101

Slide 101 text

Using Client val managedChannel = ManagedChannelBuilder .forAddress(host, port) .useTransportSecurity() .build() Specify host and port

Slide 102

Slide 102 text

Using Client val managedChannel = ManagedChannelBuilder .forAddress(host, port) .useTransportSecurity() for https

Slide 103

Slide 103 text

Using Client val managedChannel = ManagedChannelBuilder .intercept(object : ClientInterceptor { fun interceptCall(method, callOptions, channel) { }) Intercept RPC Calls

Slide 104

Slide 104 text

Using Client val managedChannel = ManagedChannelBuilder .forAddress(host, port) .useTransportSecurity() .build()

Slide 105

Slide 105 text

Using Client val managedChannel = ManagedChannelBuilder .forAddress(host, port) .useTransportSecurity() .build() val client = CoroutineStub(managedChannel)

Slide 106

Slide 106 text

View Model gRPC Client Stub

Slide 107

Slide 107 text

Using Client class GrpcViewModel(val client: CoroutineStub): ViewModel() { }

Slide 108

Slide 108 text

Unary RPC Call class CoroutineStub { } suspend fun getPlace(request: Location): Place

Slide 109

Slide 109 text

Unary RPC Call fun getPlace(location: Location) { viewModelScope.launch { } }

Slide 110

Slide 110 text

Unary RPC Call fun getPlace(location: Location) { viewModelScope.launch { val place = client.getPlace(location) } }

Slide 111

Slide 111 text

Unary RPC Call fun getPlace(location: Location) { viewModelScope.launch { withContext(Dispatchers.IO) { val place = client.getPlace(location) } } }

Slide 112

Slide 112 text

Server Streaming class CoroutineStub { } fun listPlaces(request: Area): Flow

Slide 113

Slide 113 text

Server Streaming fun listPlaces(area: Area) { }

Slide 114

Slide 114 text

Server Streaming fun listPlaces(area: Area) { viewModelScope.launch { } }

Slide 115

Slide 115 text

Server Streaming fun listPlaces(area: Area) { viewModelScope.launch { val places: Flow = client.listPlaces(area) } }

Slide 116

Slide 116 text

Server Streaming fun listPlaces(area: Area) { viewModelScope.launch { val places: Flow = client.listPlaces(area) places.collect { } } }

Slide 117

Slide 117 text

Bidirectional RPC Call class CoroutineStub { } fun chat(requests: Flow): Flow

Slide 118

Slide 118 text

Bidirectional RPC Call class CoroutineStub { } fun chat(requests: Flow): Flow

Slide 119

Slide 119 text

Bidirectional RPC Call Client Stub Comments

Slide 120

Slide 120 text

Bidirectional RPC Call State Flow Comments

Slide 121

Slide 121 text

Bidirectional RPC Call State Flow Comments Comments Client Stub

Slide 122

Slide 122 text

Using Client fun chat(comments: Flow) { } Stream of comments

Slide 123

Slide 123 text

Using Client fun chat(comments: Flow) { viewModelScope.launch { } } } }

Slide 124

Slide 124 text

Using Client fun chat(comments: Flow) { viewModelScope.launch { client.chat(comments) } } } }

Slide 125

Slide 125 text

Using Client fun chat(comments: Flow) { viewModelScope.launch { client.chat(comments) .collect { } } } }

Slide 126

Slide 126 text

Using Client class GrpcViewModel(val client: CoroutineStub): ViewModel() { fun getPlace(location: Location) fun listPlaces(area: Area): Flow fun chat(chat: Flow) }

Slide 127

Slide 127 text

gRPC Client Stub How does it use coroutines?

Slide 128

Slide 128 text

Consumer Coroutine

Slide 129

Slide 129 text

gRPC-java client Start Consumer Coroutine

Slide 130

Slide 130 text

gRPC-java client Request Consumer Coroutine Producer Coroutine Response

Slide 131

Slide 131 text

gRPC-java client Request Consumer Coroutine Producer Coroutine Response Channel (size = 1)

Slide 132

Slide 132 text

gRPC-java client Request Consumer Coroutine Producer Coroutine Response Channel (size = 1)

Slide 133

Slide 133 text

gRPC-java client Request Consumer Coroutine Producer Coroutine Response Channel (size = 1) Flow

Slide 134

Slide 134 text

gRPC Client Stub How does it use coroutines?

Slide 135

Slide 135 text

Android gRPC Client Stub Auth

Slide 136

Slide 136 text

Auth Methods ● JWT Token ● Certificate Auth ● HTTP Basic Auth

Slide 137

Slide 137 text

Auth Client Server RPC Call

Slide 138

Slide 138 text

Auth Client Server Metadata

Slide 139

Slide 139 text

Auth Metadata headers = new Metadata();

Slide 140

Slide 140 text

Auth Metadata headers = new Metadata(); headers.put(“auth_token","token");

Slide 141

Slide 141 text

Auth val client = CoroutineStub(managedChannel)

Slide 142

Slide 142 text

Auth val client = CoroutineStub(managedChannel) .withCallCredentials( . . . )

Slide 143

Slide 143 text

withCallCredentials(object: CallCredentials { override fun applyRequestMetadata() { } } Auth

Slide 144

Slide 144 text

withCallCredentials(object: CallCredentials { override fun applyRequestMetadata(applier: MetadataApplier) { } } Auth

Slide 145

Slide 145 text

withCallCredentials(object: CallCredentials { override fun applyRequestMetadata(applier: MetadataApplier) { Metadata headers = new Metadata(); headers.put(“auth_token","token"); } } Auth

Slide 146

Slide 146 text

withCallCredentials(object: CallCredentials { override fun applyRequestMetadata(applier: MetadataApplier) { Metadata headers = new Metadata(); headers.put(“auth_token","token"); applier.apply(headers); } } Auth

Slide 147

Slide 147 text

Auth Client Server RPC Call

Slide 148

Slide 148 text

Auth Client Server Metadata

Slide 149

Slide 149 text

Errors Client Server RPC Call

Slide 150

Slide 150 text

Errors Client Server Retry

Slide 151

Slide 151 text

Enable Retry val managedChannel = ManagedChannelBuilder .forAddress(host, port) .useTransportSecurity() .enableRetry() .build()

Slide 152

Slide 152 text

Client App gRPC Client Lib Server Application

Slide 153

Slide 153 text

Client App gRPC Client Lib Server Application Make 
 gRPC Call Sends RPC

Slide 154

Slide 154 text

Client App gRPC Client Lib Server Application Failure Make 
 gRPC Call Sends RPC

Slide 155

Slide 155 text

Client App gRPC Client Lib Server Application Failure Make 
 gRPC Call Sends RPC Returns UNAVAILABLE

Slide 156

Slide 156 text

Client App gRPC Client Lib Server Application Failure Make 
 gRPC Call Sends RPC Returns UNAVAILABLE Retried RPC

Slide 157

Slide 157 text

Client App gRPC Client Lib Server Application Failure Make 
 gRPC Call Sends RPC Returns UNAVAILABLE Retried RPC Success Return OK Receive 
 data

Slide 158

Slide 158 text

Enable Retry "retryPolicy": { "maxAttempts": 10, "initialBackoff": "5s", "maxBackoff": "1s", "backoffMultiplier": 2, "retryableStatusCodes": [ "UNAVAILABLE" ] }

Slide 159

Slide 159 text

Hedging Client Server RPC Call(s)

Slide 160

Slide 160 text

Client App gRPC Client Lib Server Application Make 
 gRPC Call Sends RPC

Slide 161

Slide 161 text

Client App gRPC Client Lib Server Application Make 
 gRPC Call Sends RPC Return OK

Slide 162

Slide 162 text

Client App gRPC Client Lib Server Application Make 
 gRPC Call Sends RPC Cancel

Slide 163

Slide 163 text

Hedging Policy "hedgingPolicy": { "maxAttempts": 10, "hedgingDelay": “1.5s", "nonFatalStatusCodes": [ "UNAVAILABLE", "INTERNAL", "ABORTED" ] }

Slide 164

Slide 164 text

uw-labs/bloom-rpc Code ! Issues Pull Requests BloomRPC The missing GUI Client for GRPC services. release v1.5.2

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

gRPC https: / / codingwithmohit.com/grpc/grpc-kotlin-coroutines/

Slide 167

Slide 167 text

Resources ● gRPC with Kotlin Coroutines 
 
 https: / / codingwithmohit.com/grpc/grpc-kotlin-coroutines/ ● Unit Testing Delays, Errors & Retries with Kotlin Flows 
 
 https: / / codingwithmohit.com/coroutines/unit-testing-delays- 
 errors-retries-with-kotlin-flows/ 
 


Slide 168

Slide 168 text

Thank You! www.codingwithmohit.com @heyitsmohit