Slide 1

Slide 1 text

GolangͰϦΫΤετ͝ͱͷ ΫΤϦϩά͕Έ͍ͨʂʂ 2018-05-25 kamakura.go #4 LT ໘ന๏ਓΧϠοΫ ۔઒Ѯ @_serinuntius

Slide 2

Slide 2 text

஫ҙ
 ⚠ܕ੒෼গͳΊͰ͢⚠

Slide 3

Slide 3 text

Ξϯέʔτ

Slide 4

Slide 4 text

ISUCON ஌ͬͯΒͬ͠ΌΔํʙ

Slide 5

Slide 5 text

ISUCON ग़ͨ͜ͱ͋ΔΑͬͯํʙ

Slide 6

Slide 6 text

ISUCONͱ͸ʁ

Slide 7

Slide 7 text

͓୊ͱͳΔWebαʔϏεΛ
 ܾΊΒΕͨϨΪϡϨʔγϣϯͷதͰ
 ݶք·Ͱߴ଎ԽΛਤΔ
 νϡʔχϯάότϧɺ
 ͦΕ͕ISUCONͰ͢ɻ ISUCONެࣜBlogͷdescriptionΑΓ

Slide 8

Slide 8 text

ͪͳΈʹ๻͸ ࣾ಺ISUCON͔͠ ग़ͨ͜ͱ͋Γ·ͤΜ

Slide 9

Slide 9 text

ࣾ಺ISUCONͰݟ͑ͨ՝୊ • ΫΤϦͷൃߦճ਺Λߟྀ͠ͳ͚Ε͹͍͚ͳ͍
 (ൃߦճ਺͕গͳ͍ɺ஗͍͚ͩͷΫΤϦ΋͋Δ) w w • Ͳ͜ͰΫΤϦ͕ൃߦ͞Ε͍ͯΔ͔΋஌Γ͍ͨ • ΋ͬͱݴ͑͹ͲͷϦΫΤετͰ
 ͲΕ͚ͩݺ͹ΕͯΔ͔΋஌Γ͍ͨ • GoͰग़͍ͨ(ܕ͕͋ͬͨํ͕ΞϓϦͷϦϑΝΫλ͕͠қ͍)

Slide 10

Slide 10 text

ࣾ಺ISUCONͰͷ ൓লΛ׆͔ͨ͢Ίʹ

Slide 11

Slide 11 text

ΞΫηεϩΨʔ
 ͱ ΫΤϦϩΨʔΛ࡞ͬͨ

Slide 12

Slide 12 text

github.com/serinuntius/graqt
 ര஀

Slide 13

Slide 13 text

࢖͍ํ εςοϓ1~import~ • 2ͭͷύοέʔδΛimport͢Δ

Slide 14

Slide 14 text

࢖͍ํ εςοϓ2
 ~driverΛ؀ڥม਺Ͱ੾Γସ͑Δ~ • driver੾Γସ͑ͱtraceΛΦϯΦϑ͢Δ

Slide 15

Slide 15 text

࢖͍ํ εςοϓ3
 ~middleware௥Ճ~ • aliceΛ࢖͏

Slide 16

Slide 16 text

࢖͍ํ εςοϓ4
 ~SQLݺͼग़͠ݩͷcontextରԠ~ • http.Request͔ΒcontextऔΓग़ͯ͠ɺ
 ݺͼग़͢ͱ͖ʹ౉͢

Slide 17

Slide 17 text

ٕज़తͳ͜ͱ

Slide 18

Slide 18 text

ϦΫΤετʹUUIDΛຒΊࠐΉ • middleware.go UUID=>ϦΫΤετͱΫΤϦΛඥ෇͚ΔͨΊ

Slide 19

Slide 19 text

ϦΫΤετʹUUIDΛຒΊࠐΉ • middleware.go UUID=>ϦΫΤετͱΫΤϦΛඥ෇͚ΔͨΊ ίϯςΩετʹೖΕΔཧ༝
 =>ޙͰΫΤϦͱUUIDΛඥ෇͚ΔͨΊ

Slide 20

Slide 20 text

ϋΠύϑΥʔϚϯεͳ ߏ଄ԽϩΨʔ • uber-go/zap • RLogger͕request༻ͷzapͷLogger ࠓճͷਃ͠༁ఔ౓ͷܕཁૉ

Slide 21

Slide 21 text

ϋΠύϑΥʔϚϯεͳ ߏ଄ԽϩΨʔ {"level":"info", "ts":1525082389.1337767, "caller":"graqt/middleware.go:25", "msg":"",
 "time":0.011566198, "request_id":"5914e629-6746-42c5- z342-702f224f48e1", "path":"/user", "method":"POST" } • ܕΛࢦఆͯ͠Δ͔Β଎͍

Slide 22

Slide 22 text

ΫΤϦΛτϨʔε͢Δ • shogo82148/go-sql-proxy • database/sql͸ͲΜͳDBʹ΋ରԠͰ͖ΔΑ͏ʹͳͬͯ ͍Δ • database/sql/driverʹ͋ΔinterfaceΛຬͨͯ͠Ε͹OK • ϓϩΩγ࡞ͬͯͦͷ··౉ͤ͹ϩάΛు͚ΔͷͰ͸ʁ • ͱ͍͏ൃ૝Ͱ࡞ΒΕͨΒ͍͠Ͱ͢*1 *1GoݴޠͰSQLͷτϨʔεΛ͢Δ
 https://shogo82148.github.io/blog/2015/05/13/golang-sql-proxy/

Slide 23

Slide 23 text

௒φΠεͳinterfaceͩʂʂ
 (਒͑੠) ࠓճͷืूཁ߲ˠ

Slide 24

Slide 24 text

ΫΤϦΛτϨʔε͢Δ • go-sql-proxyʹ͸ඪ४ͰΫΤϦϩΨʔ͕͋Δ • ࠓճ͸UUIDΛϩάʹు͖͍ͨؔ܎Ͱ
 ࣗલͰఆٛ

Slide 25

Slide 25 text

ΫΤϦτϨʔαʔͷ࣮૷ • tracer.go

Slide 26

Slide 26 text

ΫΤϦτϨʔαʔͷ࣮૷ • preܥͰݱࡏ࣌ࠁΛฦ͢ • cͷcontextʹrequestͷuuid͕ೖ͍ͬͯΔ • ctxʹpreܥͷݱࡏ࣌ࠁ͕ೖ͍ͬͯΔ

Slide 27

Slide 27 text

࣮ࡍʹు͔ΕΔϩά • request.log
 {"level":"info","ts":1525082389.1337767,"caller":"graqt/middleware.go: 25","msg":"","time":0.011566198,"request_id":"5914e629-6746-42c5- b342-702f224f48e1","path":"/user","method":"POST"} • query.log
 {"level":"info","ts":1525082389.1336403,"caller":"graqt/tracer.go: 20","msg":"Exec","query":"INSERT INTO `user` (email,age) VALUES (?, ?)","args":[{"Name":"","Ordinal": 1,"Value":"[email protected]"}, {"Name":"","Ordinal":2,"Value":57}],"time": 0.001157627,"request_id":"5914e629-6746-42c5-b342-702f224f48e1"}

Slide 28

Slide 28 text

͑ɺϩάు͚ͩ͘ʁ • ͪΐͬͱ଴ͬͯͯ • alpΈ͍ͨͳϩάΛूܭ͢ΔCLIΛ੡࡞த • ϦΫΤετ͝ͱʹΫΤϦ΋ݟΕΔΑ͏ʹ͠·͢ • tigΛΊͬͪΌγϯϓϧʹͨ͠ૢ࡞ײͷΠϝʔδ

Slide 29

Slide 29 text

ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί)

Slide 30

Slide 30 text

ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒

Slide 31

Slide 31 text

ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒ ISUCON༧બ௨աͯ͠
 ϒϩάͰ঺հ͢Δ

Slide 32

Slide 32 text

ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒ ISUCON༧બ௨աͯ͠
 ϒϩάͰ঺հ͢Δ (୭͔͕graqt࢖ͬͯ) 
 ISUCON༏উ͢Δ

Slide 33

Slide 33 text

ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒ ISUCON༧બ௨աͯ͠
 ϒϩάͰ঺հ͢Δ (୭͔͕graqt࢖ͬͯ) 
 ISUCON༏উ͢Δ 100ສͰ๻ʹ
 ম೑ᇋͬͯ͘ΕΔ

Slide 34

Slide 34 text

·ͱΊ • serinuntius/graqtΛ࢖͑͹ɺ
 ISUCONʹศརͦ͏ͳϩά͕ు͚Δ • ISUCONҎ֎ʹ΋΋ͪΖΜ࢖͑Δ • OSSͳͷͰποίϛͱ͔ؾܰʹͲ͏ͧ • ISUCON༏উͯ͠ম೑ᇋͬͯ͘Ε