Perl + Go実運用の話

B5582ce2d9959dfcff0384a07003e188?s=47 mackee
December 16, 2015

Perl + Go実運用の話

Gotanda.pm #7 vs Yokohama.pm #13

B5582ce2d9959dfcff0384a07003e188?s=128

mackee

December 16, 2015
Tweet

Transcript

  1. Perl + Go࣮ӡ༻ͷ࿩ @mackee_w a.k.a Ϛίϐʔ ໘ന๏ਓΧϠοΫ 2015-12-16 Gotanda.pm #7

    vs Yokohama.pm #13
  2. ͜Μ͹ΜΘ

  3. ୭ʁ • Yokohama.pm෭ Ϧʔμʔ • twitter:@mackee_w • github:mackee • ໘ന๏ਓΧϠοΫ

    • ιʔγϟϧήʔϜͷ ։ൃͱӡ༻PG
  4. None
  5. Perl + Go࣮ӡ༻ͷ࿩ @mackee_w a.k.a Ϛίϐʔ ໘ന๏ਓΧϠοΫ 2015-12-16 Gotanda.pm #7

    vs Yokohama.pm #13
  6. ෭୊

  7. ·ͨ͸ զʑ͸

  8. ೗Կʹͯ͠Perl ͚ͩΛ࢖͏͜ ͱΛࢭΊͯ https://flic.kr/p/q6cRh

  9. GoͱPerlͷΩ ϝϥΛӡ༻͢ ΔΑ͏ʹͳͬ ͔ͨ https://twitter.com/bradfitz/status/335213285815226369

  10. Section.0 Perl + Goͷ ߏ੒

  11. Unity੡ͷΫϥΠΞϯτ (iOS/Android)

  12. ELBͰड͚ͯPerlͷཱͬͯΔ AppαʔόʹϦόʔεϓϩΩγ

  13. MySQLͷRDSʹAppαʔό͕ ܨ͛ͯΔ

  14. ී௨ͳײ͡ Ͱ͸ʁʁʁ

  15. ͜ͷߏ੒͕ෳ਺͋ͬͯ

  16. Goͷαʔόʹ಺෦APIͰୟ͘

  17. None
  18. Section.1 Goͷ࠾༻

  19. ༩͑ΒΕͨ৚݅ • ૝ఆDAUʹ଱͑ΒΕΔ͜ͱ • γϯάϧϚελʔͩͱͭΒͦ͏ͳײ͕͡աڈ ͷ࣮੷͔Βͯ͠૝ఆ͞ΕΔ • શੈքʹಉ͡ΞϓϦΛ഑৴͢Δ • ϚϧνϦʔδϣϯ/DC΋ࢹ໺ʹ

  20. γϟʔσΟϯά͸ʁʁʁ • DBIx::Classͷຐज़ʹΑΓطଘͷίʔυʹ͸ JOIN͕͍ͬͺ͍ • ͦͷ··΍ΔͳΒMySQL Clusterͱ͔Spider Engineͱ͔͚ͩΕͲฐࣾͰ࣮੷ͳ͍͠RDS ࢖͍͍ͨ͠

  21. ήʔϜ಺ͷσʔλͷྲྀΕΛݟΔ • શϢʔβ͸ϥϯΫͱݺ͹ΕΔख़࿅౓͝ͱͷ֊ ૚ʹ෼͔ΕͯͦͷதͰϦʔάʹ෼͔ΕΔ • ࢼ߹(ͱݺ͹ΕΔఆ࣌ʹߦΘΕΔGvG)͸Ϧʔά ಺ͷάϧʔϓ͝ͱʹߦΘΕΔ • γʔζϯ࠷ޙʹϦʔά಺ͷ੒੷্ҐऀͰτʔ φϝϯτΛߦ͏

  22. Αͬ͠ΌϦʔάͰDB෼͚Α͏ • ͪΐ͏ͲϦʔάΛ஍Ҭผʹ͢Δ࿩͕͋ͬͨͷ ͰͦΕʹͷ͔ͬͬͨ(=>ϚϧνϦʔδϣϯ) • Ͱ΋ݟͨ໨ϫϯϫʔϧυΛ࣮ݱ͢Δ(ϑϨϯυ ΍ΫϥϒҠ੶ͳͲ)ͨΊʹ͸தԝαʔό͕ඞཁ • ͦͯ͠๯಄ͷߏ੒͕ग़དྷ্͕ͬͨ

  23. ͜ͷ࣌఺Ͱ๻͸ GoΛ࠾༻͢Δ ཧ༝Λ୳͍ͯͨ͠

  24. http://www.kayac.com/news/2014/07/golang

  25. ͦΕ͔ΒԿϲ݄͔ܦͬͯ • झຯͰॻ͍ͨΓଞͷαʔϏεͰϛυϧ΢ΣΞ ͱͯ͠ಈ͍͍ͯΔౕΛݟͨΓ͍ͯͯ͠ಛੑత ͳ΋ͷ͸ݟ͖͑ͯͨ • ιγϟήʔΛ·Δ͖ͬΓϦϓϨʔε͢Δͷ͸ େมͩͱࢥ͏͕ɺػೳ͕ߜΒΕͯͨΒ͍͍ײ ͡ʹͳΔؾ͕͢Δ

  26. Ϊδϡπతͳཧ༝෇͚ • Perlଆͷαʔόʹ͸1୆ʹ͖ͭଟ਺ͷϓϩηεཱ͕ͭ͜ͱ ʹͳΔ • ͦͷଟ਺ͷϓϩηε͔ΒͷHTTPϦΫΤετΛ͞͹͘ͷͨ Ίʹಉ͡ϓϩηε਺෼ͷPerlΛཱͯͯͨΒҙຯ෼͔Μͳ͍ ײ͡ʹͳΔͷͰ͸(΋ͪΖΜ಺෦APIͷ࢖͍ํʹΑͬͯ͸ͦ ͏ͳΒͳ͍ؾ΋͢Δ) •

    ϦιʔεΛޮ཰తʹ࢖͑ΔݴޠͰॻ͘ͷ͕͍͍ؾ͕͢Δʂ
  27. Section.2 GoΛॻ͘

  28. ࣾ಺ͷPerlͷίʔυ

  29. ܕͰΨνΨνʹݻΊ͍ͯΔ ͷ͕΄ͱΜͲ

  30. ܕΛຖճॻ͍͍ͯ ΔͳΒͦΕΛ࢖ͬ ͯίϯύΠϧ࣌ʹ ղܾͨ͠΄͏͕Α ͘Ͷʁ

  31. ํ਑ • Ͱ͖Δ͚ͩ Go way Λ໨ࢦ͢ • net/httpΛͦͷ··࢖͏ • Ͱ͖Δ͚ͩPerlΛॻ͍͍ͯͨ࣌ͱಉ͡ߟ͑Ͱॻ

    ͚ΔΑ͏ʹ͢Δ • Goʹͳͬͯศརʹͳͬͨ͜ͱ͸ར༻͢Δ
  32. IDLͷ࿩ • IDL = Interface Definition Language • ྫ: JSON

    Schema, Swagger, Protobuf • RPCͳͲͷΠϯλʔϑΣΠεΛઐ༻ݴޠͰఆٛ ͯͦ͠ΕΛར༻ͯ͠ίʔυΛੜ੒ͨ͠Γ͢Δ
  33. ฐࣾͷIDLͷ࿩ • ΋ͱ΋ͱBaalͱ͍͏ಠࣗIDL͕ࣾ಺Ͱ࢖ΘΕ͍ͯ ͨ • BaalͰServiceͱεΩʔϚΛఆٛ͠ɺC#(Unity)ͷ APIୟ͘ίʔυΛੜ੒͢Δ • PerlͰίϯτϩʔϥͷςετΛॻ͘ͱ͖ʹBaalͰ ܕ΍εΩʔϚ͕͍͋ͬͯΔ͔ΛνΣοΫ͢Δ

  34. go generateͷػӡ • BaalͰΫϥΠΞϯτ͕ੜ੒Ͱ͖ΔͳΒαʔό (ͷίϯτϩʔϥ)΋ੜ੒Ͱ͖Δ͸ͣ • ΍ͬͯΈͨ -> Ͱ͖ͨ •

    ϝϦοτ: ϦΫΤετΛύʔεͯ͠Goͷܕʹม ׵͢ΔͷͰิ׬ʹग़ͯ͘Δ࠷ߴ
  35. ࣮ࡍͷBaalͷྫ ϑϨϯυਃ੥ namespace Data.Request { entity FriendRequestRequest { /# ਃ੥͢ΔϢʔβ

    #/ UserId: !long; /# ਃ੥͞ΕΔϢʔβ #/ TargetUserId: !long; } } namespace Data.Response { entity FriendRequestResponse { /# 1: ਃ੥த 2: ਃ੥͞Εத 3: ϑϨϯυొ࿥ࡁΈ #/ Status: !integer; } }
  36. ࣮ࡍͷBaalͷྫ ϑϨϯυਃ੥ namespace Service { service FriendService { /# <short>༑ͩͪਃ੥</short>

    <url>/api/friend/request</url> <request_method>POST</request_method> #/ Request: <= !Data.Request.FriendRequestRequest => !Data.Response.FriendRequestResponse; } }
  37. ੜ੒͞Εͨίʔυ

  38. ੜ੒͞Εͨίʔυ

  39. ͜ͷίʔυΛ࢖͏ͱ͜Ζ

  40. ͜ͷίʔυΛ࢖͏ͱ͜Ζ ิ׬ʹग़ͯ͘Δʂʂʂ

  41. Go͔ΒDBΛѻ͏࿩ • ORMΛ࢖ΘͣʹΫΤϦϏϧμʔΛ࢖͏Α͏ʹͨ͠ • ϥΠϒϥϦ࠾༻ͷํ਑ͱͯ͠ʮࣗ෼͕ॻ͚ͦ͏ɺ ࣗ෼͕ϝϯςͰ͖ͦ͏ʯͳ΋ͷͱ͍͏ͷ͕͋Γ ·ͨ͠ • Ҏલgorp࢖ͬͨ͜ͱ͕͋ͬͯͦΜͳʹ޷͖͡Όͳ ͔ͬͨ

  42. Go͔ΒDBΛѻ͏࿩ • ͱ͸͍͑DDLͱGoͷstructͱಉ͜͡ͱΛ2౓ॻ ͘ͷ͸ਏ͍ • ͷͰGoͷstructΛλά෇͖Ͱॻ͍ͨΒDDLʹม ׵͢ΔͷΛॻ͍ͨ -> ࢖͑ͨ࠷ߴ •

    ੾Γग़ͨ͠ -> github.com/mackee/go-genddl
  43. ͜Ε͕

  44. ͜͏ͳΔ

  45. Section.3 Perl͔ΒGo

  46. Perl͔ΒGoʹͭͳ͙ • GoͷAPIαʔό͸εςʔτϨεͳRESTful JSON API • ೝূͳ͠ɺηογϣϯͳ͠ • FurlͰͭͳ͍ͰϦΫΤετΛJSONʹύʔε͢ ΔΫϥεΛPerlଆʹ༻ҙͯͦ͠ΕΛ࢖͏

  47. Goଆ͕མͪͯΔ࣌͸ʁ • Goଆ͕མͪͯશαʔόμ΢ϯ͸ආ͚͍ͨ • ํ਑1: ಺෦APIΛ༻͍ΔAPIΛݶఆ͢Δ • ํ਑2: མ͍ͪͯͯ΋ͳΜͱ͔Ͱ͖Δ৔߹͸ͳΜͱ͔͢ Δ

    • ํ਑3: མ͍ͪͯͯ΋ͳΜͱ͔Ͱ͖ͳ͍৔߹͸ૉ௚ʹࢮ ΜͰΤϥʔΛग़͢
  48. ͳΜͱ͔Ͱ͖Δ৔߹͸ͳΜͱ͔ ͢Δ • ྫ: ϩάΠϯޙʹදࣔ͞ΕΔϚΠϖʔδͷ͓஌ ΒͤAPI(࣮͸͓஌Βͤ͸தԝαʔόʹ಺෦API Ͱ໰͍߹Θͤͯ഑৴͍ͯ͠Δʂʂʂ) • ࢮΜͰ͍Δ৔߹ ->

    200Ҏ֎͕ؼ͖ͬͯͨ࣌ͷ ྫ֎ΛΩϟονۭͯͬ͠ΆΛฦ͢ -> શମͱ͠ ͯ͸ੜ͔͢
  49. τϥϯβΫγϣϯ͕࢖͑ͳ͍໰ ୊ • ผαʔόผDBͳͷͰ౰વͦͷؒͰී௨ͷτϥϯβ Ϋγϣϯ͸࢖͑ͳ͍ • ͳͷͰGoଆAPIΛႈ౳(ಉ͡ϦΫΤετΛԿ౓ୟ͍ ͯ΋ಉ݁͡ՌʹͳΔ)ʹͳΔΑ͏ʹ഑ྀ͢Δ • ྫ:

    ϑϨϯυਃ੥Λ2౓ୟ͍ͯ΋ʮϑϨϯυਃ੥͠ ͨΑʔʯͱฦ͢ɻDBͷঢ়ଶ͸2౓໨͸ͦͷ··
  50. Section.4 Ωϝϥͷӡ ༻

  51. σϓϩΠํ๏ • consulͷϚϧνDCͰͦΕͧΕߏ੒͞Ε͍ͯΔ • ͦΕͧΕͷDCʹconsul eventΛൃߦ͢Δ -> stretcher ͰS3ʹ্͛ͨtarballΛpull͖ͯͯ͠σϓϩΠ •

    σϓϩΠ͢Δ΋ͷ͸ಉ͡tarballʂʂ̍̍ • daemontoolsͰཱͬͯΔαʔϏε͕ҧ͏
  52. GoͷϏϧυํ๏ • σϓϩΠεΫϦϓτ಺ͰϏϧυ͍ͯ͠Δ • ґଘύοέʔδ΋ͦ͜Ͱͱ͍ͬͯΔ • ςετ͍ͯ͠Δ΋ͷͱࠩ෼͕ग़ΔՄೳੑ͕͋Δ…… ͪΐͬͱා͍ • ґଘύοέʔδ͸ࣗ෼ͷσΟϨΫτϦҎԼʹִ཭͍ͯ͠

    Δ(vendoringͬΆ͍͚ΕͲόʔδϣϯ؅ཧ͸͍ͯ͠ͳ͍)
  53. None
  54. ࣮ࡍʹӡ༻ʹೖͬͯ • GoଆىҼͷόά΍τϥϒϧ͸ͳ͠(!) • ຊ౰ʹؾΛ͚ͭ·ͬͯ͘ॻ͍͍ͯΔͷͰͦΕ͕ޭ Λ૗͍ͯ͠Δؾ͕͠ͳ͍Ͱ΋ͳ͍ • ͋ͱCPU΁ͷෛՙ͕ΊͪΌগͳͯ͘ຊ౰ʹಈ͍ͯ ͍Δͷ͔৺഑ʹͳΔϨϕϧ(ൺֱର৅͸DBIx::Class ͱDateTimeΛΨϯΨϯ࢖͍ͬͯΔPerlଆͷαʔό)

  55. ೰Έ • νʔϜ಺ʹGoͷίʔυʹखΛग़ͤΔਓ͕গͳ ͍ • ͦΜͳʹػձ͕ଟ͘ͳ͍ͷ΋͋Δ • ؾ߹ೖΕͯॻ͔ͳ͍ͱ͍͚ͳ͍͠ɺԿΑΓจ Խݍ/ߟ͑ํ͕ҧ͏ͷͰֶͼ௚͠ʹͳΔ

  56. ·ͱΊ • GoͰϑϨϯυͱ͔࣮૷͢Δͷ͕ඇৗʹ಄Λ͜Ͷ͘Γճͯ͠๐͸ ͖ͳ͕Β΍͚ͬͨΕͲษڧʹͳͬͨؾ͕͢Δ • ϚΠΫϩαʔϏεతʹαʔό෼͚Δͷ͸ΞϦ͔΋͠Εͳ͍(Ψνϟ ͱ͔ڞ௨Ͱ͠ΐ) • ͕ɺτϥϯβΫγϣϯ͕΍͸Γ೉఺ •

    GoͰϑϧεϖοΫͷιγϟήʔΛॻ͘ͷ͸ࠓͷஈ֊Ͱ͸ແཧʂɹ ͚ͩΕͲϥΠϒϥϦ(ಛʹORM)ͷ஌ݟ͕ஷ·Ε͹ߦ͚Δؾ͕͢Δ