Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
帯域制限と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
680
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
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
9
5.2k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
350
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
13
13k
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
9
6.7k
alecthomas/kong はいいぞ
fujiwara3
7
2.5k
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3.7k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
3
4.7k
監視のこれまでとこれから/sakura monitoring seminar 2025
fujiwara3
12
5.9k
k6による負荷試験 入門から日常的な実践まで/Re:TechTalk #01
fujiwara3
2
620
Featured
See All Featured
Embracing the Ebb and Flow
colly
88
5.1k
A Tale of Four Properties
chriscoyier
163
24k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Design in an AI World
tapps
1
260
Why Our Code Smells
bkeepers
PRO
340
58k
We Have a Design System, Now What?
morganepeng
55
8.2k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Skip the Path - Find Your Career Trail
mkilby
1
160
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
The Pragmatic Product Professional
lauravandoore
37
7.4k
30 Presentation Tips
portentint
PRO
1
340
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 ΑΖ͘͠