iOSエンジニアの為のgrpc-swift入門

 iOSエンジニアの為のgrpc-swift入門

gRPCはGoogleが開発したハイパフォーマンスなRPC(Remote Procedure Call)プロトコルです。
Firebase SDKにも組み込まれていて、間接的に使った方も多いと思います。
トークでは、アプリからの通信の選択肢として、RESTやGraphQLに加えてgRPCも選べるように、
gRPC自体の解説、grpc-swiftの開発状況や現状の課題をデモを交えお伝えします。

E1cf27d9fa565b2867d171f21a39759c?s=128

tikidunpon

August 30, 2018
Tweet

Transcript

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

  2. Koichi Tanaka Origami inc.

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

  4. gRPCͱ͸ʁ

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

  6. ͦ΋ͦ΋RPCͱ͸ʁ

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

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

  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. gRPC࠾༻ࣄྫ

  16. gRPC࠾༻ࣄྫ • Firebase Firestore

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

  18. gRPC࠾༻ࣄྫ • Firebase Firestore • Google Cloud Speech • Lyft,

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

    Netflix, Square..etc • Cyber Agent, Mercari, 10x, Origami..etc
  20. gRPCͷϝϦοτ

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

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

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

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

    ʯ • ʮϨεϙϯε͸ͲΜͳߏ଄͚ͩͬͨͬʁ ʯ
  25. ڧ͍ܕ෇͚ɺHTTP/2ɺProtobuf

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

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

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

  29. Protocol Buffers

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

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

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

  33. Protocol Buffersʢলུʣ service Greeter { // αʔϏεఆٛ rpc SayHello (HelloRequest)

    returns (HelloResponse) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string text = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloResponse { // Ϩεϙϯεϝοηʔδఆٛ string text = 1; }
  34. Protocol Buffers class Greeter_GreeterServiceClient { func sayHello(){} } struct Greeter_HelloRequest

    {ɹ var text: String = String() } struct Greeter_HelloResponse { var text: String = String() }
  35. 4छྨͷετϦʔϛϯά௨৴ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳAPIɺϚΠΫ ϩαʔϏεؒ௨৴

    Server streaming 1 ෳ਺ ϑΟʔυ഑৴ɺαʔ όʔPush Client streaming ෳ਺ 1 ෳ਺σʔλૹ৴ Bidirectional streaming ෳ਺ ෳ਺ speech-to-textɺνϟ οτ
  36. ͜͜·Ͱͷ·ͱΊ • RPC͸RESTͱಉྻͷ௨৴खஈͷ̍ͭ • gRPCͷߏ੒ཁૉɺઃܭɺProtocol BuffersɺHTTP/2ɺ11ݴޠʹ ެࣜରԠ • Protocol BuffersͰAPIͷܕΛදݱ

    • HTTP/2্Ͱ4छྨͷετϦʔϛϯά௨৴Λ࣮ݱ
  37. gRPC-Swiftʹ͍ͭͯ

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

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

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

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

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

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

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

    SwiftGRPC = grpc-swiftɺswift-protobuf౳ʹґଘ)
  45. CocoaPodsʹΑΔಋೖํ๏ • grpc-swiftΛ࢖͏४උΛ੔͑Δ: Qiita

  46. gRPCʹΑΔ։ൃͷྲྀΕ

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

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

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

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

  51. γϯϓϧͳAPIͷ࣮૷ʢprotoͷఆٛʣ syntax = "proto3"; package greeter; service Greeter { rpc

    SayHello (HelloRequest) returns (HelloResponse) {} } message HelloRequest { string text = 1; } message HelloResponse { string text = 1; }
  52. protocͰίʔυΛࣗಈੜ੒ $ protoc helloworld.proto \ --swift_out=. \ --swiftgrpc_out=Client=true,Server=false,TestStubs=true:.

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

  54. ࣗಈੜ੒ίʔυʢൈਮʣ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) } }
  55. 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) } }
  56. 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") } }) }
  57. ଞͷετϦʔϛϯά௨৴Λѻ͏ʹ͸ 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) {} }
  58. σϞ

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

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

  62. Next Step!! • grpc-swiftΛ࢖ͬͯiOSΞϓϦͰ΋շదͳgRPC௨৴Λߦ͏ • Plasma - gRPC streamΛར༻ͨ͠ϦΞϧλΠϜͳϢʔβʔମݧ •

    grpc.io: Official Site • grpc-swift: Github • swift-protobuf: Github • gRPC & iOS at Lyft: YouTube
  63. Thanks!! Questions? Koichi Tanaka @tikidunpon