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
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
550
クラウドを使う側から、作る側へ / 大吉祥寺.pm 2026前夜祭
fujiwara3
8
2.2k
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
9
6.2k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
410
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.7k
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
699
190k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Into the Great Unknown - MozCon
thekraken
41
2.7k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
910
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
250
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Fireside Chat
paigeccino
43
4k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
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 ΑΖ͘͠