Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
帯域制限とx/time/rate
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
FUJIWARA Shunichiro
September 16, 2016
690
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
帯域制限とx/time/rate
http://connpass.com/event/39794/
FUJIWARA Shunichiro
September 16, 2016
More Decks by FUJIWARA Shunichiro
See All by FUJIWARA Shunichiro
多摩川(.dev)ランニング入門 / Tamagawa.dev#3
fujiwara3
3
530
クラウドを使う側から、作る側へ / 大吉祥寺.pm 2026前夜祭
fujiwara3
8
2.2k
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
9
6.1k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
400
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
13
14k
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
9
6.9k
alecthomas/kong はいいぞ
fujiwara3
7
2.6k
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3.8k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
4
5.6k
Featured
See All Featured
Facilitating Awesome Meetings
lara
57
7.1k
Balancing Empowerment & Direction
lara
6
1.3k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
600
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
610
Product Roadmaps are Hard
iamctodd
55
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Music & Morning Musume
bryan
48
7.4k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
280
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
Transcript
ଳҬ੍ݶͱ x/time/rate ΈΜGoϦϦύˏVOYAGE ajito
౻ݪ ढ़Ұ @fujiwara github.com/fujiwara sfujiwara.hatenablog.com ٕज़෦
3ষΛࣥච͠·ͨ͠ ײΛblogͰ͍͚ͨͩΔͱ େมتͼ·͢!
ಛूΛࣥච͠·ͨ͠ ײΛ(ུ
GoͰଳҬ੍ݶ͕͍ͨ͠ • Ϧιʔε͍͗͢ࢭ • StretcherͰμϯϩʔυଳҬ੍ݶ • ϕϯνϚʔΫ • Fluentd in_tail
ϕϯνϚʔΫͷͨΊͷ go-dummer • (ࣾ)ISUCON ϕϯνϚʔΧʔͰଳҬΤϛϡϨʔγϣϯ • io.Reader, io.Writer ΠϯλʔϑΣʔεͩͱศར
ͭ͘Γ·ͨ͠ (4݄) github.com/fujiwara/shapeio import "github.com/fujiwara/shapeio" r := shapeio.NewReader(reader) // io.Reader
r.SetRateLimit(1024 * 10) // 10KB/sec // ͋ͱී௨ʹ io.Reader ͱͯ͠͏ Writer͋Γ·͢
ͦͷޙ
golang.org/x/time/rate !? ʮgolang.org/x/time/rateͰ੍ݶΛߦ͏ʯ http://qiita.com/lufia/items/29bf1aeb0a0fe69d16f0 ศརͦ͏
x/time/rate Ͱॻ͖͠ before (ଳҬ੍ޚͰͭ෦) func (l *limiter) wait(n int, err
error) (int, error) { if n == 0 || err != nil { return n, err } elapsed := time.Since(l.startedAt) l.bytes += int64(n) rate := float64(l.bytes) / elapsed.Seconds() if rate < l.bytesPerSec { return n, nil } d := time.Duration(float64(l.bytes)/l.bytesPerSec*float64(time.Second) - float64(elapsed)) time.Sleep(d) // reset shaping window l.startedAt = time.Now() l.bytes = 0 return n, nil }
x/time/rate Ͱॻ͖͠ after func (s *Writer) Write(p []byte) (int, error)
{ if s.limiter == nil { return s.w.Write(p) } n, err := s.w.Write(p) if err != nil { return n, err } // ͭͱ͜Ζ͚ͩ͜͜ if err := s.limiter.WaitN(s.ctx, n); err != nil { return n, err } return n, err }
x/time/rate 1ඵؒʹཷ·Δtokenͷͱɺ࠷େͰཷΊΔΛࢦఆ limiter := rate.NewLimiter(rate.Limit(m), burst) nݸͷtokenΛফඅ͢Δ(ཷ·ͬͯͳ͚Εཷ·Δ·Ͱͭ) err := limiter.WaitN(ctx,
n) nݸͷtoken͕ཷ·ͬͯΔ͔֬ೝ(ཷ·ͬͯΕফඅͯ͠true) ok := limiter.AllowN(time.Now(), n)
x/time/rate ಋೖͷ෭࢈ context ͕͑ΔΑ͏ʹ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Seconds) r
:= shapeio.NewReaderWithContext(reader, ctx) shapeio.SetRateLimit(1024 * 1024) io.Copy(w, r) ... ଳҬ੍ݶͭͭ͠λΠϜΞτΩϟϯηϧ
·ͱΊ x/time/rate ศརʂ shapeio ΑΖ͘͠