feat: add river-backed async task queue
This commit is contained in:
@@ -2,13 +2,49 @@ package runner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/easyai/easyai-ai-gateway/apps/api/internal/auth"
|
||||
"github.com/easyai/easyai-ai-gateway/apps/api/internal/clients"
|
||||
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
|
||||
)
|
||||
|
||||
type localRateLimitError struct {
|
||||
clientErr *clients.ClientError
|
||||
cause error
|
||||
retryAfter time.Duration
|
||||
}
|
||||
|
||||
func (e *localRateLimitError) Error() string {
|
||||
if e == nil || e.clientErr == nil {
|
||||
return store.ErrRateLimited.Error()
|
||||
}
|
||||
return e.clientErr.Error()
|
||||
}
|
||||
|
||||
func (e *localRateLimitError) Unwrap() []error {
|
||||
if e == nil || e.clientErr == nil {
|
||||
if e != nil && e.cause != nil {
|
||||
return []error{e.cause}
|
||||
}
|
||||
return []error{store.ErrRateLimited}
|
||||
}
|
||||
if e.cause != nil {
|
||||
return []error{e.clientErr, e.cause}
|
||||
}
|
||||
return []error{e.clientErr, store.ErrRateLimited}
|
||||
}
|
||||
|
||||
func localRateLimitRetryAfter(err error) time.Duration {
|
||||
var limitErr *localRateLimitError
|
||||
if errors.As(err, &limitErr) && limitErr.retryAfter > 0 {
|
||||
return limitErr.retryAfter
|
||||
}
|
||||
return store.RateLimitRetryAfter(err)
|
||||
}
|
||||
|
||||
func (s *Service) rateLimitReservations(ctx context.Context, user *auth.User, candidate store.RuntimeModelCandidate, body map[string]any) []store.RateLimitReservation {
|
||||
out := make([]store.RateLimitReservation, 0)
|
||||
out = append(out, reservationsFromPolicy("platform_model", candidate.PlatformModelID, effectiveRateLimitPolicy(candidate), body)...)
|
||||
|
||||
Reference in New Issue
Block a user