summaryrefslogtreecommitdiff
path: root/internal/limits/limits.go
blob: 6e0bf74f3b466d3be6e3c03bba41242d7b130337 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package limits

import (
	"context"
	"encoding/json"
	"errors"
	"log/slog"
	"math"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"aigw/internal/domain"

	"github.com/redis/go-redis/v9"
)

var (
	ErrRequestsExceeded = errors.New("requests per minute limit exceeded")
	ErrTokensExceeded   = errors.New("tokens per minute limit exceeded")
	ErrConcurrencyLimit = errors.New("concurrent request limit exceeded")
)

type Lease interface{ Release() }

type Limiter struct {
	redis            *redis.Client
	prefix           string
	defaultMaxOutput int64
	logger           *slog.Logger
	policies         atomic.Pointer[policySnapshot]
	localMu          sync.Mutex
	local            map[string]*localWindow
	redisUp          atomic.Bool
	redisFailureSeen atomic.Bool
	redisRetryAt     atomic.Int64
}

type policySnapshot struct{ policies map[string]domain.LimitPolicy }

type localWindow struct {
	minute     int64
	requests   int64
	tokens     int64
	concurrent int64
}

type localLease struct {
	limiter  *Limiter
	project  string
	released atomic.Bool
}
type redisLease struct {
	limiter  *Limiter
	key      string
	released atomic.Bool
}

const (
	redisCommandTimeout = 250 * time.Millisecond
	redisRetryCooldown  = 5 * time.Second
)

const acquireScript = `
local req = tonumber(ARGV[1])
local tok = tonumber(ARGV[2])
local conc = tonumber(ARGV[3])
local estimate = tonumber(ARGV[4])
local ttl = tonumber(ARGV[5])
local r = 0
local t = 0
local c = 0
if req > 0 then r = redis.call('INCR', KEYS[1]); if r == 1 then redis.call('PEXPIRE', KEYS[1], ttl) end end
if tok > 0 then t = redis.call('INCRBY', KEYS[2], estimate); if t == estimate then redis.call('PEXPIRE', KEYS[2], ttl) end end
if conc > 0 then c = redis.call('INCR', KEYS[3]); redis.call('PEXPIRE', KEYS[3], 3600000) end
if (req > 0 and r > req) then if req > 0 then redis.call('DECR', KEYS[1]) end; if tok > 0 then redis.call('DECRBY', KEYS[2], estimate) end; if conc > 0 then redis.call('DECR', KEYS[3]) end; return {0,1} end
if (tok > 0 and t > tok) then if req > 0 then redis.call('DECR', KEYS[1]) end; if tok > 0 then redis.call('DECRBY', KEYS[2], estimate) end; if conc > 0 then redis.call('DECR', KEYS[3]) end; return {0,2} end
if (conc > 0 and c > conc) then if req > 0 then redis.call('DECR', KEYS[1]) end; if tok > 0 then redis.call('DECRBY', KEYS[2], estimate) end; if conc > 0 then redis.call('DECR', KEYS[3]) end; return {0,3} end
return {1,0}
`

const releaseScript = `
local value = tonumber(redis.call('GET', KEYS[1]) or '0')
if value > 0 then redis.call('DECR', KEYS[1]) end
return value
`

func New(redisURL, prefix string, defaultMaxOutput int64, logger *slog.Logger) *Limiter {
	if logger == nil {
		logger = slog.Default()
	}
	if prefix == "" {
		prefix = "aigw:limits"
	}
	l := &Limiter{prefix: strings.TrimRight(prefix, ":"), defaultMaxOutput: defaultMaxOutput, logger: logger, local: make(map[string]*localWindow)}
	if strings.TrimSpace(redisURL) != "" {
		if options, err := redis.ParseURL(redisURL); err == nil {
			options.MaxRetries = -1
			options.DialerRetries = 1
			options.DialTimeout = redisCommandTimeout
			options.ReadTimeout = redisCommandTimeout
			options.WriteTimeout = redisCommandTimeout
			options.PoolTimeout = redisCommandTimeout
			l.redis = redis.NewClient(options)
		} else {
			logger.Warn("limits_redis_config_invalid", "error", err, "fallback", "local")
		}
	}
	l.ReplacePolicies(nil)
	return l
}

func (l *Limiter) Close() error {
	if l.redis == nil {
		return nil
	}
	return l.redis.Close()
}

func (l *Limiter) ReplacePolicies(policies []domain.LimitPolicy) {
	copyMap := make(map[string]domain.LimitPolicy, len(policies))
	for _, policy := range policies {
		if policy.ProjectID != "" {
			copyMap[policy.ProjectID] = policy
		}
	}
	l.policies.Store(&policySnapshot{policies: copyMap})
}

func (l *Limiter) Policy(projectID string) (domain.LimitPolicy, bool) {
	snapshot := l.policies.Load()
	if snapshot == nil {
		return domain.LimitPolicy{}, false
	}
	policy, ok := snapshot.policies[projectID]
	return policy, ok
}

func (l *Limiter) Acquire(ctx context.Context, principal domain.Principal, body []byte) (Lease, error) {
	policy, ok := l.Policy(principal.ProjectID)
	if !ok || (policy.RequestsPerMinute == 0 && policy.TokensPerMinute == 0 && policy.Concurrent == 0) {
		return noopLease{}, nil
	}
	estimate := EstimateTokens(body, l.defaultMaxOutput)
	minute := time.Now().Unix() / 60
	if l.redis != nil && time.Now().UnixNano() >= l.redisRetryAt.Load() {
		redisContext, cancel := context.WithTimeout(ctx, redisCommandTimeout)
		lease, err := l.acquireRedis(redisContext, principal.ProjectID, minute, policy, estimate)
		cancel()
		if err == nil {
			return lease, nil
		}
		if errors.Is(err, ErrRequestsExceeded) || errors.Is(err, ErrTokensExceeded) || errors.Is(err, ErrConcurrencyLimit) {
			l.markRedis(true, nil)
			return nil, err
		}
		l.markRedis(false, err)
	}
	return l.acquireLocal(principal.ProjectID, minute, policy, estimate)
}

func EstimateTokens(body []byte, defaultMaxOutput int64) int64 {
	input := int64((len(body) + 3) / 4)
	if input < 1 {
		input = 1
	}
	maxOutput := defaultMaxOutput
	var limits struct {
		MaxTokens           int64 `json:"max_tokens"`
		MaxCompletionTokens int64 `json:"max_completion_tokens"`
		MaxOutputTokens     int64 `json:"max_output_tokens"`
	}
	if json.Unmarshal(body, &limits) == nil {
		explicit := int64(0)
		for _, value := range []int64{limits.MaxTokens, limits.MaxCompletionTokens, limits.MaxOutputTokens} {
			if value > explicit {
				explicit = value
			}
		}
		if explicit > 0 {
			maxOutput = explicit
		}
	}
	if maxOutput < 0 {
		maxOutput = 0
	}
	if input > math.MaxInt64-maxOutput {
		return math.MaxInt64
	}
	return input + maxOutput
}

func (l *Limiter) acquireRedis(ctx context.Context, project string, minute int64, policy domain.LimitPolicy, estimate int64) (Lease, error) {
	base := l.prefix + ":" + project + ":" + strconv.FormatInt(minute, 10)
	keys := []string{base + ":requests", base + ":tokens", l.prefix + ":" + project + ":concurrent"}
	values, err := l.redis.Eval(ctx, acquireScript, keys, policy.RequestsPerMinute, policy.TokensPerMinute, policy.Concurrent, estimate, 125000).Result()
	if err != nil {
		return nil, err
	}
	items, ok := values.([]any)
	if !ok || len(items) < 2 {
		return nil, errors.New("invalid limits Redis response")
	}
	allowed, _ := toInt64(items[0])
	reason, _ := toInt64(items[1])
	if allowed == 0 {
		switch reason {
		case 1:
			return nil, ErrRequestsExceeded
		case 2:
			return nil, ErrTokensExceeded
		default:
			return nil, ErrConcurrencyLimit
		}
	}
	l.markRedis(true, nil)
	return &redisLease{limiter: l, key: keys[2]}, nil
}

func (l *Limiter) acquireLocal(project string, minute int64, policy domain.LimitPolicy, estimate int64) (Lease, error) {
	l.localMu.Lock()
	defer l.localMu.Unlock()
	for key, value := range l.local {
		if value.minute < minute-2 && value.concurrent == 0 {
			delete(l.local, key)
		}
	}
	window := l.local[project]
	if window == nil {
		window = &localWindow{minute: minute}
		l.local[project] = window
	} else if window.minute != minute {
		window.minute = minute
		window.requests = 0
		window.tokens = 0
	}
	if policy.RequestsPerMinute > 0 && window.requests >= policy.RequestsPerMinute {
		return nil, ErrRequestsExceeded
	}
	if policy.TokensPerMinute > 0 && (estimate > policy.TokensPerMinute || window.tokens > policy.TokensPerMinute-estimate) {
		return nil, ErrTokensExceeded
	}
	if policy.Concurrent > 0 && window.concurrent >= policy.Concurrent {
		return nil, ErrConcurrencyLimit
	}
	window.requests++
	window.tokens += estimate
	window.concurrent++
	return &localLease{limiter: l, project: project}, nil
}

func (l *Limiter) releaseLocal(project string) {
	l.localMu.Lock()
	defer l.localMu.Unlock()
	if value := l.local[project]; value != nil && value.concurrent > 0 {
		value.concurrent--
	}
}

func (l *Limiter) releaseRedis(ctx context.Context, key string) {
	if l.redis == nil {
		return
	}
	if _, err := l.redis.Eval(ctx, releaseScript, []string{key}).Result(); err != nil {
		l.markRedis(false, err)
	}
}

func (x *localLease) Release() {
	if x.released.CompareAndSwap(false, true) {
		x.limiter.releaseLocal(x.project)
	}
}
func (x *redisLease) Release() {
	if x.released.CompareAndSwap(false, true) {
		ctx, cancel := context.WithTimeout(context.Background(), redisCommandTimeout)
		defer cancel()
		x.limiter.releaseRedis(ctx, x.key)
	}
}

type noopLease struct{}

func (noopLease) Release() {}

func (l *Limiter) markRedis(healthy bool, err error) {
	l.redisUp.Store(healthy)
	if healthy {
		l.redisRetryAt.Store(0)
		if l.redisFailureSeen.Swap(false) {
			l.logger.Info("limits_redis_recovered")
		}
		return
	}
	l.redisRetryAt.Store(time.Now().Add(redisRetryCooldown).UnixNano())
	if l.redisFailureSeen.CompareAndSwap(false, true) {
		l.logger.Warn("limits_redis_unavailable", "error", err, "fallback", "local", "retry_after", redisRetryCooldown)
	}
}

func toInt64(value any) (int64, bool) {
	switch v := value.(type) {
	case int64:
		return v, true
	case string:
		n, e := strconv.ParseInt(v, 10, 64)
		return n, e == nil
	case []byte:
		n, e := strconv.ParseInt(string(v), 10, 64)
		return n, e == nil
	default:
		return 0, false
	}
}