Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails から Protobuf を使いたくなった話

9ec2ceee3f782e64fd67fa7d82b6c1b4?s=47 Hazumi Ichijo
September 20, 2018

Rails から Protobuf を使いたくなった話

9ec2ceee3f782e64fd67fa7d82b6c1b4?s=128

Hazumi Ichijo

September 20, 2018
Tweet

Transcript

  1. ©2018 Wantedly, Inc. Rails͔ΒProtobufΛ ࢖͍ͨ͘ͳͬͨ࿩ Cloud Pub/Sub ͰͷεΩʔϚ؅ཧ 2018.9.20 -

    @rerost/@hazumirr
  2. ©2018 Wantedly, Inc. ࣗݾ঺հ Ұᑍ୺੅!IB[VNJSS!SFSPTU 8FC"QQMJDBUJPO&OHJOFFS 3BJMT (P 3FBDUʜ ۴࿏ߴઐஜ೾େֶ8BOUFEMZ

  3. ©2018 Wantedly, Inc. ۴࿏ߴઐ

  4. ©2018 Wantedly, Inc. 8BOUFEMZ1FPQMF  ೥ϦϦʔε  ϚΠΫϩαʔϏεʢ(P 3VCZ ʜʣ

    8BOUFEMZ7JTJU  ೥ϦϦʔε  ϞϊϦγοΫͳ3BJMTΞϓϦέʔγϣϯ
  5. ©2018 Wantedly, Inc. ืू ϑΟʔυ εΧ΢τ

  6. ©2018 Wantedly, Inc.

  7. ©2018 Wantedly, Inc. ຖ೔શͯͷεΧ΢τ༻ͷϢʔβʔσʔλΛೖΕ௚͍ͯ͠Δ ࠩ෼ͷσʔλ͚ͩཉ͍͠ WJTJUࣗମͷ։ൃͱผαΠΫϧͰճ͍ͨ͠ σʔλ෼ੳ෦෼ #JH2VFSZͳͲΛѻ͏

  8. ©2018 Wantedly, Inc. վળํ๏  ͪΐͬͱͣͭվળ͍ͯ͘͠  ϚΠΫϩαʔϏεʹ੾Γग़͢

  9. ©2018 Wantedly, Inc. εΧ΢τͷػೳ  ߜΓࠐΈ  ਪન  Ϣʔβʔ৘ใͷදࣔ

    ϚΠΫϩαʔϏεʹ੾Γग़ͯ͠ ੔ཧ͍ͨ͠
  10. ©2018 Wantedly, Inc. εΧ΢τͷσʔλ෼ੳ͢Δਓ

  11. ©2018 Wantedly, Inc. ΋͠ϚΠΫϩαʔϏεԽ͕ਐΜͩΒ

  12. ©2018 Wantedly, Inc. 7JTJUͷ੹຿͕΍͹͍

  13. ©2018 Wantedly, Inc. ΋͠ϚΠΫϩαʔϏεԽ͕ਐΜͩΒ ௨஌ͯ͘͠ΕΔԿ͔

  14. ©2018 Wantedly, Inc. $MPVE1VC4VC

  15. ©2018 Wantedly, Inc. https://cloud.google.com/pubsub/docs/overview

  16. ©2018 Wantedly, Inc. ΋͠ϚΠΫϩαʔϏεԽ͕ਐΜͩΒ

  17. ©2018 Wantedly, Inc. ϝϦοτ  αʔϏε͕ؒૄ݁߹ʹͳΔ σϝϦοτ  ૹΔσʔλεΩʔϚͷ؅ཧ͕೉͍͠

  18. ©2018 Wantedly, Inc. ΋͠ϚΠΫϩαʔϏεԽ͕ਐΜͩΒ

  19. ©2018 Wantedly, Inc. ͦ͜Ͱ1SPUPDPM#V⒎FS  γϦΞϥΠβʔɾσγϦΞϥΠβʔࣗಈੜ੒  ޓ׵ੑ͕͋Δఔ౓อͨΕΔ  ੩తܕ෇͖ݴޠͰ΋͏·͘ѻ͑Δ

     ࣾ಺ͷଞͷνʔϜ͕࢖͍ͬͯͯ஌ݟ͕͋Δ
  20. ©2018 Wantedly, Inc. ΋͠ϚΠΫϩαʔϏεԽ͕ਐΜͩΒ 1SPUPCVG 1SPUPCVG

  21. ©2018 Wantedly, Inc. Protocol Buffer

  22. ©2018 Wantedly, Inc. syntax = "proto3"; option ruby_package = "Protos::SamplePB";

    message User { // ֶྺ message Education { string school_name = 1; } // ৬ྺ message Experience { string company_name = 1; } string user_name = 1; string introduction = 2; int64 age = 3; repeated Education educations = 4; repeated Experience experiences = 5; } TBNQMFQSPUP
  23. ©2018 Wantedly, Inc. # Generated by the protocol buffer compiler.

    DO NOT EDIT! # source: protos/sample.proto require 'google/protobuf' Google::Protobuf::DescriptorPool.generated_pool.build do add_message "User" do optional :user_name, :string, 1 optional :introduction, :string, 2 optional :age, :int64, 3 repeated :educations, :message, 4, "User.Education" repeated :experiences, :message, 5, "User.Experience" end add_message "User.Education" do optional :school_name, :string, 1 end add_message "User.Experience" do optional :company_name, :string, 1 end end module Protos::SamplePB User = Google::Protobuf::DescriptorPool.generated_pool.lookup("User").msgclass User::Education = Google::Protobuf::DescriptorPool.generated_pool.lookup("User.Education").msgclass User::Experience = Google::Protobuf::DescriptorPool.generated_pool.lookup("User.Experience").msgclass end TBNQMFQCSC
  24. ©2018 Wantedly, Inc. def new_sample educations = [ Protos::SamplePB::User::Education.new( school_name:

    "۴࿏ߴઐ" ), Protos::SamplePB::User::Education.new( school_name: "ஜ೾େֶ" ), ] experiences = [ Protos::SamplePB::User::Experience.new( company_name: "΢ΥϯςουϦʔגࣜձࣾ" ), ] Protos::SamplePB::User.new( user_name: 'rerost', introduction: 'δϟεί͔Β110km', educations: educations, experiences: experiences, ) end sample = new_sample puts Protos::SamplePB::User.encode_json(sample) { "userName": "rerost", "introduction": "δϟεί͔Β110km", "educations": [ { "schoolName": "۴࿏ߴઐ" }, { "schoolName": "ஜ೾େֶ" } ], "experiences": [ { "companyName": "΢ΥϯςουϦʔגࣜձࣾ" } ] }
  25. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new( user_name: 1, # Τϥʔ

    user_nameͷܕ͕ҧ͏ͷͰ age: 23, introduction: 'δϟεί͔Β110km', educations: [], experiences: [], ) end sample = new_sample ҧ͏ܕΛೖΕͨ৔߹ʹ͍ͭͯ
  26. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new() end sample = new_sample

    puts sample.age # 0 ஋ΛೖΕͳ͔ͬͨ৔߹ʹ͍ͭͯ
  27. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new( age: 23, ) end

    sample = new_sample encoded = Protos::SamplePB::User.encode(sample) puts encoded.class # String decoded = Protos::SamplePB::User.decode(encoded) puts decoded.age # 23 CJOBSZ΁ͷγϦΞϥΠζσγϦΞϥΠζ
  28. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new( age: 23, ) end

    sample = new_sample encoded = Protos::SamplePB::User.encode_json(sample) puts encoded.class # String puts encoded # {"age":23,"educations":[],"experiences":[]} decoded = Protos::SamplePB::User.decode_json(encoded) puts decoded.age # 23 +40/΁ͷγϦΞϥΠζσγϦΞϥΠζ
  29. ©2018 Wantedly, Inc. https://github.com/rerost/sample-protobuf

  30. ©2018 Wantedly, Inc. ޓ׵ੑʹ͍ͭͯ ࠷௿ݶཉ͍͠ޓ׵ੑɹ  ΧϥϜͷ௥Ճ  ໊લͷมߋ

  31. ©2018 Wantedly, Inc. ޓ׵ੑʹ͍ͭͯ ࠷௿ݶཉ͍͠ޓ׵ੑɹ  ΧϥϜͷ௥Ճ  ໊લͷมߋ 1SPUPCVGͳΒɾɾɾ

     σγϦΞϥΠζෆՄͳΧϥϜ͸ແࢹ  λάφϯόʔͰࣝผ όΠφϦͳΒ
  32. ©2018 Wantedly, Inc. <࠶ܝ>΋͠ϚΠΫϩαʔϏεԽ͕ਐΜͩΒ 1SPUPCVG 1SPUPCVG

  33. ©2018 Wantedly, Inc. 1SPUPCVGΛ࢖͏͜ͱͰղܾͰ͖ͨ໰୊ɹ  σʔλΛ҆ఆͯ͠΍ΓऔΓͰ͖Δ  ੩తܕ෇͖ݴޠͱ΋͏·͘΍ΓͱΓ͕Ͱ͖Δ  γϦΞϥΠβʔσγϦΞϥΠβʔ͕ࣗಈੜ੒Ͱ͖Δ

  34. ©2018 Wantedly, Inc. 1SPUPCVGPO1VC4VCͰγϯϓϧʹ  ଞͷαʔϏεͱͷίϛϡχέʔγϣϯָ͕ʹ  ੹຿͕ඞཁ࠷খݶʹ