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
700
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
Transcript
13 slides
ଳҬ੍ݶͱ 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 ΑΖ͘͠
More Decks by FUJIWARA Shunichiro
See All by FUJIWARA Shunichiro
多摩川(.dev)ランニング入門 / Tamagawa.dev#3
fujiwara3
3
620
クラウドを使う側から、作る側へ / 大吉祥寺.pm 2026前夜祭
fujiwara3
8
2.3k
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
9
6.6k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
440
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
13
14k
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
9
7.1k
alecthomas/kong はいいぞ
fujiwara3
7
2.7k
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3.9k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
4
5.9k
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
450
First, design no harm
axbom
PRO
2
1.4k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
690
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
Designing Experiences People Love
moore
143
24k
A better future with KSS
kneath
240
18k
Technical Leadership for Architectural Decision Making
baasie
4
660
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
290
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Exploring anti-patterns in Rails
aemeredith
4
580
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k