Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
AWS SDK for GoのContextパターン / aws sdk for go context pattern
Shuichi Ohsawa
May 28, 2018
Programming
3
440
AWS SDK for GoのContextパターン / aws sdk for go context pattern
Shuichi Ohsawa
May 28, 2018
Tweet
Share
More Decks by Shuichi Ohsawa
See All by Shuichi Ohsawa
ohsawa0515
0
520
ohsawa0515
0
1.8k
ohsawa0515
0
410
ohsawa0515
1
2.4k
ohsawa0515
9
2.5k
ohsawa0515
0
980
ohsawa0515
0
660
ohsawa0515
0
840
ohsawa0515
0
220
Other Decks in Programming
See All in Programming
christianliebel
PRO
0
130
osyo
0
250
takahi5
1
370
neripark
3
550
doyaaaaaken
0
770
grapecity_dev
0
170
danilop
1
720
hassaku63
1
700
bkuhlmann
4
270
manfredsteyer
PRO
0
120
hackz_inc
0
310
legalforce
PRO
0
600
Featured
See All Featured
pedronauck
652
110k
kastner
54
1.9k
maggiecrowley
9
490
roundedbygravity
84
7.8k
colly
188
14k
afnizarnur
176
14k
marcelosomers
220
15k
holman
288
130k
lemiorhan
627
43k
shpigford
369
42k
geoffreycrofte
21
910
tenderlove
53
3.5k
Transcript
AWS SDK for Goͷ Contextύλʔϯ Gopherಓ #1 LT Shuichi Ohsawa
(@ohsawa0515) 1/13
ࣗݾհ • େᖒलҰ(Shuichi Ohsawa) • GitHub, Twitter: @ohsawa0515 • Blog:
http://blog.jicoman.info/ • Sansanגࣜձࣾ - ໊ཧΫϥυαʔϏε • ໊σʔλԽγεςϜͷΠϯϑϥߏஙɾӡ༻ɾվળ 2/13
AWS SDK for Go ͷ υΩϡϝϯτΛ ݟ͍ͯͯؾ͍ͮͨ 3/13
4/13
5/13
ContextΛΔલ WithContextͬͯͳΜͩΖ͏ŋŋŋ ❓ GoݴޠͷContextΛ͏Έ͍͚ͨͩͲɺαϯϓϧίʔυݟͯ ͬͯͳͦ͞͏ͩ͠ผʹ͍͍ " 6/13
ContextΛͬͨޙ Ωϟϯηϧͱ͔λΠϜΞτ͕Ͱ͖Δ͔ΒศརΜ 7/13
S3ΞοϓϩʔυʹλΠϜΞτΛઃ ͚Δ ctx, cancel := context.WithTimeout(context.Background(), time.Duration(30)*time.Second) defer cancel() svc
:= s3.New(sess) if _, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ Body: f, Bucket: aws.String(s3Bucket), Key: aws.String(file), }); err != nil { log.Fatal(err) } 8/13
PutObjectWithContext() PutObject() ʹ ContextΛηοτ͍ͯ͠Δ func (c *S3) PutObjectWithContext(ctx aws.Context, input
*PutObjectInput, opts ...request.Option) (*PutObjectOutput, error) { req, out := c.PutObjectRequest(input) req.SetContext(ctx) req.ApplyOptions(opts...) return out, req.Send() } 9/13
PutObjectWithContext() PutObject() ʹͳ͍Ҿ͕ŋŋŋ func (c *S3) PutObjectWithContext(ctx aws.Context, input *PutObjectInput,
opts ...request.Option) (*PutObjectOutput, error) { req, out := c.PutObjectRequest(input) req.SetContext(ctx) req.ApplyOptions(opts...) return out, req.Send() } 10/13
ϦΫΤετϋϯυϥΛڬΊΔ PutObjectʹ͔͔ͬͨ࣌ؒΛදࣔ͢Δ ctx, cancel := context.WithTimeout(context.Background(), time.Duration(30)*time.Second) defer cancel() svc
:= s3.New(sess) if _, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ Body: f, Bucket: aws.String(s3Bucket), Key: aws.String(file), }, func(r *request.Request) { start := time.Now() r.Handlers.Complete.PushBack(func(req *request.Request) { fmt.Printf("request %s took %s to complete\n", req.RequestID, time.Since(start)) }) }); err != nil { log.Fatal(err) } 11/13
·ͱΊ • AWS SDK for GoͷContextύλʔϯΛ͑APIίʔϧͷ ΩϟϯηϧλΠϜΞτΛ؆୯ʹઃఆͰ͖Δ • SDKଆͰΩϟϯηϧॲཧΛͤΔ͜ͱ͕Ͱ͖ΔͷͰɺContext ͱ΄΅࣮͕มΘΒͣ͑Δ
• ϦΫΤετϋϯυϥΛڬΊΔͷͰଞʹ͍Ζ͍ΖͰ͖ͦ͏ 12/13
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝ ·ͨ͠ 13/13