feat: enforce durable queue resource limits and backpressure

This commit is contained in:
2026-09-17 17:25:56 +08:00
parent 3ce7d2c104
commit 5c8825e4bc
38 changed files with 1231 additions and 30 deletions

View File

@@ -100,6 +100,23 @@ func registerTasks(mux *http.ServeMux, s *service.Service) {
out, err := s.GetQueue(r.Context(), t, core.ID(r.PathValue("id")))
return out, 200, err
}))
mux.HandleFunc("POST /v1/queues/{command}", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
command := r.PathValue("command")
if !strings.HasSuffix(command, ":configure") {
return nil, 0, fault.NotFound
}
var in service.QueueConfigInput
if err := decodeBody(r, &in); err != nil {
return nil, 0, err
}
// 完整替换命令要求 paused 显式给出,避免遗漏被解释为解除暂停。
key, err := idempotencyKey(r)
if err != nil {
return nil, 0, err
}
out, err := s.ConfigureQueue(r.Context(), t, core.ID(strings.TrimSuffix(command, ":configure")), key, in)
return out, 200, err
}))
}
func createdStatus(duplicate bool) int {
if duplicate {
@@ -172,6 +189,10 @@ func writeError(w http.ResponseWriter, id string, err error) {
if status == 0 {
status = 503
}
if known.Code == "QUEUE_FULL" {
status = http.StatusTooManyRequests
w.Header().Set("Retry-After", "1")
}
if status == 401 {
w.Header().Set("WWW-Authenticate", `Bearer realm="orbit"`)
}
@@ -243,6 +264,11 @@ func exactFields(raw []byte, t reflect.Type) error {
name := strings.Split(field.Tag.Get("json"), ",")[0]
if name != "" && name != "-" {
names[name] = field.Type
if field.Tag.Get("required") == "true" {
if _, ok := object[name]; !ok {
return errors.New("missing required field")
}
}
}
}
for name, value := range object {