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

AWS SDK for GoのContextパターン / aws sdk for go context pattern

AWS SDK for GoのContextパターン / aws sdk for go context pattern

E8ac626646da35420ffba5da02f4787d?s=128

Shuichi Ohsawa

May 28, 2018
Tweet

Transcript

  1. AWS SDK for Goͷ Contextύλʔϯ Gopherಓ৔ #1 LT Shuichi Ohsawa

    (@ohsawa0515) 1/13
  2. ࣗݾ঺հ • େᖒलҰ(Shuichi Ohsawa) • GitHub, Twitter: @ohsawa0515 • Blog:

    http://blog.jicoman.info/ • Sansanגࣜձࣾ - ໊ࢗ؅ཧΫϥ΢υαʔϏε • ໊ࢗσʔλԽγεςϜͷΠϯϑϥߏஙɾӡ༻ɾվળ 2/13
  3. AWS SDK for Go ͷ υΩϡϝϯτΛ ݟ͍ͯͯؾ͍ͮͨ 3/13

  4. 4/13

  5. 5/13

  6. ContextΛ஌Δલ WithContextͬͯͳΜͩΖ͏ŋŋŋ ❓ GoݴޠͷContextΛ࢖͏Έ͍͚ͨͩͲɺαϯϓϧίʔυݟͯ΋ ࢖ͬͯͳͦ͞͏ͩ͠ผʹ͍͍΍ " 6/13

  7. ContextΛ஌ͬͨޙ Ωϟϯηϧͱ͔λΠϜΞ΢τ͕Ͱ͖Δ͔Βศར΍Μ 7/13

  8. 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
  9. 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
  10. 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
  11. ϦΫΤετϋϯυϥΛڬΊΔ 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
  12. ·ͱΊ • AWS SDK for GoͷContextύλʔϯΛ࢖͑͹APIίʔϧͷ Ωϟϯηϧ΍λΠϜΞ΢τΛ؆୯ʹઃఆͰ͖Δ • SDKଆͰΩϟϯηϧॲཧΛ೚ͤΔ͜ͱ͕Ͱ͖ΔͷͰɺContext ͱ΄΅࣮૷͕มΘΒͣ࢖͑Δ

    • ϦΫΤετϋϯυϥΛڬΊΔͷͰଞʹ΋͍Ζ͍ΖͰ͖ͦ͏ 12/13
  13. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝ ·ͨ͠ 13/13