Add HTTP whole-run deadlines and context outcomes
This commit is contained in:
@@ -5,8 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
@@ -80,6 +82,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "transient_retries must be greater than or equal to 0")
|
||||
return
|
||||
}
|
||||
if req.RunTimeoutSeconds != nil {
|
||||
if *req.RunTimeoutSeconds < 0 || *req.RunTimeoutSeconds > math.MaxInt64/int64(time.Second) {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "run_timeout_seconds is out of range")
|
||||
return
|
||||
}
|
||||
}
|
||||
var mappedInputs map[string]promptkit.ArtifactRef
|
||||
if len(req.Inputs) > 0 {
|
||||
mappedInputs = make(map[string]promptkit.ArtifactRef, len(req.Inputs))
|
||||
@@ -97,7 +105,13 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
model = executionTargetOverrideFromModelOverrideDTO(req.Model)
|
||||
}
|
||||
|
||||
res, err := h.runner.Run(r.Context(), promptkit.RunRequest{
|
||||
runContext := r.Context()
|
||||
if req.RunTimeoutSeconds != nil && *req.RunTimeoutSeconds > 0 {
|
||||
var cancel context.CancelFunc
|
||||
runContext, cancel = context.WithTimeout(runContext, time.Duration(*req.RunTimeoutSeconds)*time.Second)
|
||||
defer cancel()
|
||||
}
|
||||
res, err := h.runner.Run(runContext, promptkit.RunRequest{
|
||||
PromptID: req.PromptID,
|
||||
PromptVersion: req.PromptVersion,
|
||||
ProfileID: req.ProfileID,
|
||||
@@ -108,6 +122,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
TransientRetries: req.TransientRetries,
|
||||
})
|
||||
if err != nil {
|
||||
switch runContext.Err() {
|
||||
case context.DeadlineExceeded:
|
||||
writeError(w, http.StatusGatewayTimeout, "deadline_exceeded", "run execution deadline exceeded")
|
||||
return
|
||||
case context.Canceled:
|
||||
writeError(w, http.StatusRequestTimeout, "request_canceled", "run request canceled")
|
||||
return
|
||||
}
|
||||
status, code, message := mapRunError(err)
|
||||
writeError(w, status, code, message)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user