MCPcopy
hub / github.com/caddyserver/certmagic / loop

Method loop

ratelimiter.go:69–109  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

67}
68
69func (r *RingBufferRateLimiter) loop() {
70 defer func() {
71 if err := recover(); err != nil {
72 buf := make([]byte, stackTraceBufferSize)
73 buf = buf[:runtime.Stack(buf, false)]
74 log.Printf("panic: ring buffer rate limiter: %v\n%s", err, buf)
75 }
76 }()
77
78 for {
79 // if we've been stopped, return
80 select {
81 case <-r.stopped:
82 return
83 default:
84 }
85
86 if len(r.ring) == 0 {
87 if r.window == 0 {
88 // rate limiting is disabled; always allow immediately
89 r.permit()
90 continue
91 }
92 panic("invalid configuration: maxEvents = 0 and window != 0 does not allow any events")
93 }
94
95 // wait until next slot is available or until we've been stopped
96 r.mu.Lock()
97 then := r.ring[r.cursor].Add(r.window)
98 r.mu.Unlock()
99 waitDuration := time.Until(then)
100 waitTimer := time.NewTimer(waitDuration)
101 select {
102 case <-waitTimer.C:
103 r.permit()
104 case <-r.stopped:
105 waitTimer.Stop()
106 return
107 }
108 }
109}
110
111// Allow returns true if the event is allowed to
112// happen right now. It does not wait. If the event

Callers 1

NewRateLimiterFunction · 0.95

Calls 4

permitMethod · 0.95
LockMethod · 0.65
UnlockMethod · 0.65
StopMethod · 0.45

Tested by

no test coverage detected