GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — workflow/flow_bucket.go at main · ubgo/workflow Expand file tree/ Copy pathflow_bucket.goMore file actions 204 lines (186 loc) · 7.49 KB / Copy pathflow_bucket.goFile metadata and controls204 lines (186 loc) · 7.49 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 package workflow import ( "context" "errors" "fmt" "math" "sync" "time" "github.com/ubgo/workflow/clock" ) // ErrThrottled means an upstream's rate limit has no room for the call // now. UpstreamLimit.Take returns it marked with RetryAfter, so a step // that returns it is retried once the bucket has room on a workflow whose // RetryPolicy.DelayFor is RetryAfterDelay, and on its normal backoff // otherwise. // //ds:def id=errthrottled-ys5p97sd owner=@khanakia stability=stable var ErrThrottled = errors.New("workflow: upstream rate limit reached") // BucketSpec is a leaky bucket: it holds at most Capacity units of cost // and drains LeakPerSecond units every second. A call of cost c fits when // the level plus c is at most Capacity. Shopify's REST limit is Capacity // 40, LeakPerSecond 2 with cost 1 per call; its GraphQL limit is cost // based (Capacity 1000, LeakPerSecond 50, cost per query). // //ds:def id=bucketspec-wgghvwb6 owner=@khanakia stability=stable type BucketSpec struct { Capacity float64 LeakPerSecond float64 } //ds:def id=bucketspec-validate-59g43222 owner=@khanakia stability=stable func (s BucketSpec) validate() error { if !(s.Capacity > 0) || !(s.LeakPerSecond > 0) || math.IsInf(s.Capacity, 0) || math.IsInf(s.LeakPerSecond, 0) { return invalidf("bucket needs a positive, finite Capacity and LeakPerSecond (got %v, %v)", s.Capacity, s.LeakPerSecond) } return nil } // BucketStore holds bucket levels per key (one key per credential or // shop). MemoryBuckets keeps them in this process; a shared store (Redis, // SQL) makes several workers draw from one bucket, which is what an // upstream limit per credential needs once more than one process calls // it. Implementations apply the leak themselves, from the now they are // given, and must be safe for concurrent use; Take must check and add in // one atomic step. // //ds:def id=bucketstore-pphpjy7n owner=@khanakia stability=stable type BucketStore interface { // Take adds cost to key's level and returns zero when it fits; // otherwise it adds nothing and returns how long until it would fit. Take(ctx context.Context, key string, spec BucketSpec, cost float64, now time.Time) (wait time.Duration, err error) // Observe sets key's level to what the upstream reported (Shopify's // "32/40" call-limit header, a GraphQL throttle status), so the // bucket follows the upstream's own count, including calls made by // other apps on the same credential. Observe(ctx context.Context, key string, spec BucketSpec, level float64, now time.Time) error } // MemoryBuckets is an in-process BucketStore. The zero value is ready to // use. // //ds:def id=memorybuckets-kmxbjjmg owner=@khanakia stability=stable type MemoryBuckets struct { mu sync.Mutex buckets map[string]bucketLevel } type bucketLevel struct { level float64 at time.Time } // drained returns b's level at now after leaking since b.at. func (b bucketLevel) drained(spec BucketSpec, now time.Time) float64 { if elapsed := now.Sub(b.at).Seconds(); elapsed > 0 { return max(b.level-elapsed*spec.LeakPerSecond, 0) } return b.level } // Take implements BucketStore. // //ds:def id=memorybuckets-take-cb2xmqgu owner=@khanakia stability=stable func (m *MemoryBuckets) Take(_ context.Context, key string, spec BucketSpec, cost float64, now time.Time) (time.Duration, error) { m.mu.Lock() defer m.mu.Unlock() if m.buckets == nil { m.buckets = map[string]bucketLevel{} } level := m.buckets[key].drained(spec, now) if over := level + cost - spec.Capacity; over > 0 { m.buckets[key] = bucketLevel{level: level, at: now} return time.Duration(math.Ceil(over / spec.LeakPerSecond * float64(time.Second))), nil } m.buckets[key] = bucketLevel{level: level + cost, at: now} return 0, nil } // Observe implements BucketStore. // //ds:def id=memorybuckets-observe-ykyktgtm owner=@khanakia stability=stable func (m *MemoryBuckets) Observe(_ context.Context, key string, spec BucketSpec, level float64, now time.Time) error { m.mu.Lock() defer m.mu.Unlock() if m.buckets == nil { m.buckets = map[string]bucketLevel{} } m.buckets[key] = bucketLevel{level: min(max(level, 0), spec.Capacity), at: now} return nil } // UpstreamLimit is a leaky-bucket, cost-based rate limit on calls a step // makes to an outside API (PS-02), one bucket per key. Unlike // Workflow.RateLimits, which counts run starts, it counts what the // upstream counts: the cost of each call, against a bucket that drains // over time. Build it with NewUpstreamLimit. // //ds:def id=upstreamlimit-a7vr28ey owner=@khanakia stability=stable type UpstreamLimit struct { store BucketStore spec BucketSpec clk clock.Clock } // NewUpstreamLimit returns a limit over store with spec, reading time // from clk. A nil store means a new MemoryBuckets; a nil clk means // clock.Real. It returns an error wrapping ErrInvalid when spec is not // positive and finite. // //ds:def id=newupstreamlimit-gm36feys owner=@khanakia stability=stable func NewUpstreamLimit(store BucketStore, spec BucketSpec, clk clock.Clock) (*UpstreamLimit, error) { if err := spec.validate(); err != nil { return nil, err } if store == nil { store = &MemoryBuckets{} } if clk == nil { clk = clock.Real{} } return &UpstreamLimit{store: store, spec: spec, clk: clk}, nil } // Take reserves cost units in key's bucket. It returns nil when they fit; // otherwise RetryAfter(ErrThrottled, wait), which a step returns as it is // so the attempt is retried once the bucket has room (see ErrThrottled). // A cost above the bucket's capacity can never fit and is a Permanent // error wrapping ErrInvalid; a negative cost is refused the same way. // //ds:def id=upstreamlimit-take-gk7eushb owner=@khanakia stability=stable func (u *UpstreamLimit) Take(ctx context.Context, key string, cost float64) error { wait, err := u.Wait(ctx, key, cost) if err != nil { return err } if wait > 0 { return RetryAfter(fmt.Errorf("%w: key %q, room in %s", ErrThrottled, key, wait), wait) } return nil } // TakeOrSnooze is Take for a driver with CapSnooze: when the bucket has // no room it returns Snooze(wait), which puts the step back without // spending an attempt, since a spent upstream limit is "not yet", not a // failure. On a driver without CapSnooze a snooze is an ordinary error and // spends an attempt; use Take there. // //ds:def id=upstreamlimit-takeorsnooze-ftsxazjz owner=@khanakia stability=stable func (u *UpstreamLimit) TakeOrSnooze(ctx context.Context, key string, cost float64) error { wait, err := u.Wait(ctx, key, cost) if err != nil { return err } if wait > 0 { return Snooze(wait) } return nil } // Wait is Take without the error: it reserves cost and returns zero, or // reserves nothing and returns how long until cost fits, for a caller // that would rather sleep in the step than retry it. // //ds:def id=upstreamlimit-wait-436yru9t owner=@khanakia stability=stable func (u *UpstreamLimit) Wait(ctx context.Context, key string, cost float64) (time.Duration, error) { if cost < 0 || cost > u.spec.Capacity || math.IsNaN(cost) { return 0, Permanent(invalidf("upstream limit: cost %v is outside 0..%v", cost, u.spec.Capacity)) } return u.store.Take(ctx, key, u.spec, cost, u.clk.Now()) } // Observe records the level the upstream reported for key, so the next // Take decides from the upstream's count. Call it with each response's // rate-limit header. // //ds:def id=upstreamlimit-observe-qgmmcj65 owner=@khanakia stability=stable func (u *UpstreamLimit) Observe(ctx context.Context, key string, level float64) error { return u.store.Observe(ctx, key, u.spec, level, u.clk.Now()) } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |