Slide 1

Slide 1 text

iOSΤϯδχΞͷҝͷgrpc-swiftೖ໳ iOSDC Japan 2018 Koichi Tanaka

Slide 2

Slide 2 text

Koichi Tanaka Origami inc.

Slide 3

Slide 3 text

Agenda • gRPCͱ͸ • gRPCͷϝϦοτ • gRPC-Swiftʹ͍ͭͯ

Slide 4

Slide 4 text

gRPCͱ͸ʁ

Slide 5

Slide 5 text

Google͕։ൃͨ͠RPCϑϨʔϜϫʔΫ

Slide 6

Slide 6 text

ͦ΋ͦ΋RPCͱ͸ʁ

Slide 7

Slide 7 text

Remote Procedure Call αʔόʔͷؔ਺ΛϩʔΧϧͷؔ਺ͷΑ͏ʹݺͼग़ٕ͢ज़

Slide 8

Slide 8 text

REST HTTPϝιουͱURIΛࢦఆͯ͠αʔόʔͷػೳΛݺͼग़͢

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

gRPC࠾༻ࣄྫ

Slide 16

Slide 16 text

gRPC࠾༻ࣄྫ • Firebase Firestore

Slide 17

Slide 17 text

gRPC࠾༻ࣄྫ • Firebase Firestore • Google Cloud Speech

Slide 18

Slide 18 text

gRPC࠾༻ࣄྫ • Firebase Firestore • Google Cloud Speech • Lyft, Netflix, Square..etc

Slide 19

Slide 19 text

gRPC࠾༻ࣄྫ • Firebase Firestore • Google Cloud Speech • Lyft, Netflix, Square..etc • Cyber Agent, Mercari, 10x, Origami..etc

Slide 20

Slide 20 text

gRPCͷϝϦοτ

Slide 21

Slide 21 text

APIͷ࢓༷ͱ࣮૷ͷဃ཭͕ݮΓ·͢ ྫ͑͹࣍ͷΑ͏ͳ໰୊Λආ͚ΒΕ·͢ɻ

Slide 22

Slide 22 text

APIͷ࢓༷ͱ࣮૷ͷဃ཭͕ݮΓ·͢ ྫ͑͹࣍ͷΑ͏ͳ໰୊Λආ͚ΒΕ·͢ɻ • ʮͦͷAPI͸υΩϡϝϯτʹ͋Δ͚Ͳ΋͏ଘࡏ͠ͳ͍ΜͩΑͶ ! ʯ

Slide 23

Slide 23 text

APIͷ࢓༷ͱ࣮૷ͷဃ཭͕ݮΓ·͢ ྫ͑͹࣍ͷΑ͏ͳ໰୊Λආ͚ΒΕ·͢ɻ • ʮͦͷAPI͸υΩϡϝϯτʹ͋Δ͚Ͳ΋͏ଘࡏ͠ͳ͍ΜͩΑͶ ! ʯ • ʮͪΌΜͱnameͰϨεϙϯεฦͯ͠Δʁ͝ΊΜnemeͰλΠϙ ͯͨ͠ " ʯ

Slide 24

Slide 24 text

APIͷ࢓༷ͱ࣮૷ͷဃ཭͕ݮΓ·͢ ྫ͑͹࣍ͷΑ͏ͳ໰୊Λආ͚ΒΕ·͢ɻ • ʮͦͷAPI͸υΩϡϝϯτʹ͋Δ͚Ͳ΋͏ଘࡏ͠ͳ͍ΜͩΑͶ ! ʯ • ʮͪΌΜͱnameͰϨεϙϯεฦͯ͠Δʁ͝ΊΜnemeͰλΠϙ ͯͨ͠ " ʯ • ʮϨεϙϯε͸ͲΜͳߏ଄͚ͩͬͨͬʁ ʯ

Slide 25

Slide 25 text

ڧ͍ܕ෇͚ɺHTTP/2ɺProtobuf

Slide 26

Slide 26 text

ڧ͍ܕ෇͚ɺHTTP/2ɺProtobuf • ։ൃ࣌ͷܕͷϛε͸શͯίϯύΠϧ࣌ʹݕग़Ͱ͖Δ

Slide 27

Slide 27 text

ڧ͍ܕ෇͚ɺHTTP/2ɺProtobuf • ։ൃ࣌ͷܕͷϛε͸શͯίϯύΠϧ࣌ʹݕग़Ͱ͖Δ • ϙʔϦϯάΛαʔόʔαΠυϓογϡʹஔ͖׵͑ͯ௨৴ྔ࡟ݮ

Slide 28

Slide 28 text

ڧ͍ܕ෇͚ɺHTTP/2ɺProtobuf • ։ൃ࣌ͷܕͷϛε͸શͯίϯύΠϧ࣌ʹݕग़Ͱ͖Δ • ϙʔϦϯάΛαʔόʔαΠυϓογϡʹஔ͖׵͑ͯ௨৴ྔ࡟ݮ • Protocol BufferͰJSONΑΓϖΠϩʔυαΠζΛ࡟ݮ

Slide 29

Slide 29 text

Protocol Buffers

Slide 30

Slide 30 text

Protocol Buffers • Google͕։ൃͨ͠IDLʢΠϯλʔϑΣʔεهड़ݴޠʣ

Slide 31

Slide 31 text

Protocol Buffers • Google͕։ൃͨ͠IDLʢΠϯλʔϑΣʔεهड़ݴޠʣ • JSONಉ༷௨৴΍ӬଓԽʹ࢖͑ΔγϦΞϥΠζϑΥʔϚοτ

Slide 32

Slide 32 text

Protocol Buffers • Google͕։ൃͨ͠IDLʢΠϯλʔϑΣʔεهड़ݴޠʣ • JSONಉ༷௨৴΍ӬଓԽʹ࢖͑ΔγϦΞϥΠζϑΥʔϚοτ • protoϑΝΠϧʹAPIΛܕ෇͖Ͱఆٛ

Slide 33

Slide 33 text

Protocol Buffersʢলུʣ service Greeter { // αʔϏεఆٛ rpc SayHello (HelloRequest) returns (HelloResponse) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string text = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloResponse { // Ϩεϙϯεϝοηʔδఆٛ string text = 1; }

Slide 34

Slide 34 text

Protocol Buffers class Greeter_GreeterServiceClient { func sayHello(){} } struct Greeter_HelloRequest {ɹ var text: String = String() } struct Greeter_HelloResponse { var text: String = String() }

Slide 35

Slide 35 text

4छྨͷετϦʔϛϯά௨৴ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳAPIɺϚΠΫ ϩαʔϏεؒ௨৴ Server streaming 1 ෳ਺ ϑΟʔυ഑৴ɺαʔ όʔPush Client streaming ෳ਺ 1 ෳ਺σʔλૹ৴ Bidirectional streaming ෳ਺ ෳ਺ speech-to-textɺνϟ οτ

Slide 36

Slide 36 text

͜͜·Ͱͷ·ͱΊ • RPC͸RESTͱಉྻͷ௨৴खஈͷ̍ͭ • gRPCͷߏ੒ཁૉɺઃܭɺProtocol BuffersɺHTTP/2ɺ11ݴޠʹ ެࣜରԠ • Protocol BuffersͰAPIͷܕΛදݱ • HTTP/2্Ͱ4छྨͷετϦʔϛϯά௨৴Λ࣮ݱ

Slide 37

Slide 37 text

gRPC-Swiftʹ͍ͭͯ

Slide 38

Slide 38 text

gRPC-Swiftʹ͍ͭͯ • gRPC-Swiftͷߏ੒

Slide 39

Slide 39 text

gRPC-Swiftʹ͍ͭͯ • gRPC-Swiftͷߏ੒ • ඞཁͳಓ۩ͷ४උ

Slide 40

Slide 40 text

gRPC-Swiftʹ͍ͭͯ • gRPC-Swiftͷߏ੒ • ඞཁͳಓ۩ͷ४උ • γϯϓϧͳAPIͷ࣮૷

Slide 41

Slide 41 text

gRPC-Swiftͷߏ੒ • gRPC-C-Core͕ϕʔε

Slide 42

Slide 42 text

gRPC-Swiftͷߏ੒ • ExperimentalͰݱࡏͷόʔδϣϯ͸0.5.1

Slide 43

Slide 43 text

gRPC-Swift with SwiftNIO? • VaporͰ΋࢖ΘΕ͍ͯΔSwiftNIOͰͷ࣮ݧతͳ࣮૷

Slide 44

Slide 44 text

gRPC-SwiftΛ࢖͏ͨΊʹඞཁͳಓ۩ ಓ۩ આ໌ protoc ϓϩτίϧίϯύΠϥɺ͜ΕͰίʔυ Λࣗಈੜ੒͢Δ protoc-gen-swift Swift༻ͷprotocϓϥάΠϯ protoc-gen-swiftgrpc Swift+gRPC༻ͷprotocϓϥάΠϯ SwiftGRPC = grpc-swiftɺswift-protobuf౳ʹґଘ)

Slide 45

Slide 45 text

CocoaPodsʹΑΔಋೖํ๏ • grpc-swiftΛ࢖͏४උΛ੔͑Δ: Qiita

Slide 46

Slide 46 text

gRPCʹΑΔ։ൃͷྲྀΕ

Slide 47

Slide 47 text

gRPCʹΑΔ։ൃͷྲྀΕ • protoϑΝΠϧʹAPIΛఆٛ͢Δ

Slide 48

Slide 48 text

gRPCʹΑΔ։ൃͷྲྀΕ • protoϑΝΠϧʹAPIΛఆٛ͢Δ • protocͰίʔυΛࣗಈੜ੒͢Δ

Slide 49

Slide 49 text

gRPCʹΑΔ։ൃͷྲྀΕ • protoϑΝΠϧʹAPIΛఆٛ͢Δ • protocͰίʔυΛࣗಈੜ੒͢Δ • αʔόʔͱΫϥΠΞϯτΛ࣮૷͢Δ

Slide 50

Slide 50 text

γϯϓϧͳAPIͷ࣮૷ • ΫϥΠΞϯτ͔ΒϝοηʔδΛૹΔͱɺϝοηʔδͷ຤ඌʹจ ࣈྻΛՃ͑ͯฦͯ͘͠ΕΔAPIΛߟ͑ͯΈΔ

Slide 51

Slide 51 text

γϯϓϧͳAPIͷ࣮૷ʢprotoͷఆٛʣ syntax = "proto3"; package greeter; service Greeter { rpc SayHello (HelloRequest) returns (HelloResponse) {} } message HelloRequest { string text = 1; } message HelloResponse { string text = 1; }

Slide 52

Slide 52 text

protocͰίʔυΛࣗಈੜ੒ $ protoc helloworld.proto \ --swift_out=. \ --swiftgrpc_out=Client=true,Server=false,TestStubs=true:.

Slide 53

Slide 53 text

ࣗಈੜ੒ίʔυʢൈਮʣhelloworld.pb.swift struct Greeter_HelloRequest { var text: String = String() }

Slide 54

Slide 54 text

ࣗಈੜ੒ίʔυʢൈਮʣhelloworld.grpc.swift internal final class Greeter_GreeterServiceClient: ServiceClientBase, Greeter_GreeterService { /// Synchronous. Unary. internal func sayHello(_ request: Greeter_HelloRequest) throws -> Greeter_HelloResponse { return try Greeter_GreeterSayHelloCallBase(channel) .run(request: request, metadata: metadata) } /// Asynchronous. Unary. internal func sayHello(_ request: Greeter_HelloRequest, completion: @escaping (Greeter_HelloResponse?, CallResult) -> Void) throws -> Greeter_GreeterSayHelloCall { return try Greeter_GreeterSayHelloCallBase(channel) .start(request: request, metadata: metadata, completion: completion) } }

Slide 55

Slide 55 text

gRPCΫϥΠΞϯτʢUnary Blocking) let client = Greeter_GreeterServiceClient.init(address: "127.0.0.1:50051", secure: false) class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() blockingSayHello() } func blockingSayHello() { var request = Greeter_HelloRequest() request.text = "hello" let response = try? self.client.sayHello(request) print("gRPC Server returns " + response!.text) } }

Slide 56

Slide 56 text

gRPCΫϥΠΞϯτʢUnary Non Blocking) // ඇಉظॲཧ func nonBlockingSayHello() { var request = Greeter_HelloRequest() request.text = "hello" let _ = try? self.client.sayHello(request, completion: { (response, result) in if result.success, result.statusCode == .ok { print("gRPC Server returns " + response!.text) } else { print("gRPC Server returns error") } }) }

Slide 57

Slide 57 text

ଞͷετϦʔϛϯά௨৴Λѻ͏ʹ͸ service Echo { //Unary rpc Get(EchoRequest) returns (EchoResponse) {} //Server streaming rpc Expand(EchoRequest) returns (stream EchoResponse) {} //Client streaming rpc Collect(stream EchoRequest) returns (EchoResponse) {} //Bidirectional streaming rpc Update(stream EchoRequest) returns (stream EchoResponse) {} }

Slide 58

Slide 58 text

σϞ

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

·ͱΊ gRPCͱProtocol BuffersΛ૊Έ߹ΘͤΔͱ • API࢓༷ʹؔ͢Δίϛϡχέʔγϣϯίετ͕Լ͕Δ • ࢓༷มߋʹ΋ίʔυࣗಈੜ੒Ͱૉૣ͘ରԠՄೳ

Slide 61

Slide 61 text

gRPCΛϞόΠϧΞϓϦ͔Β࢖͏্Ͱͷ՝୊ • ϞόΠϧΞϓϦ͔Βར༻͍ͯ͠Δࣄྫ͕·ͩগͳ͍ • αʔόʔͷෛՙ෼ࢄͷ໰୊ ! • TLS઀ଓΛߦ͏্Ͱ͸ͻͱखؒඞཁ

Slide 62

Slide 62 text

Next Step!! • grpc-swiftΛ࢖ͬͯiOSΞϓϦͰ΋շదͳgRPC௨৴Λߦ͏ • Plasma - gRPC streamΛར༻ͨ͠ϦΞϧλΠϜͳϢʔβʔମݧ • grpc.io: Official Site • grpc-swift: Github • swift-protobuf: Github • gRPC & iOS at Lyft: YouTube

Slide 63

Slide 63 text

Thanks!! Questions? Koichi Tanaka @tikidunpon