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
600
クラウドを使う側から、作る側へ / 大吉祥寺.pm 2026前夜祭
fujiwara3
8
2.2k
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
9
6.4k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
430
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
13
14k
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
9
7k
alecthomas/kong はいいぞ
fujiwara3
7
2.7k
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3.9k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
4
5.8k
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.6k
For a Future-Friendly Web
brad_frost
184
11k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Test your architecture with Archunit
thirion
2
2.5k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
970
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
GitHub's CSS Performance
jonrohan
1033
470k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
870
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Statistics for Hackers
jakevdp
799
230k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
290
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 ΑΖ͘͠