Adds `client.GuessSessionIDFromPayload(client, r, payload)` for reverse-proxy callers. Generated by Coder Agents on behalf of @pawbana.
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// NewStreamingTransport returns an HTTP transport for long-lived provider
|
|
// responses. It intentionally omits both dial and response-header timeouts so
|
|
// slow connection establishment and first-token latency are not cut off here.
|
|
func NewStreamingTransport() *http.Transport {
|
|
return &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: 100,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: time.Second,
|
|
}
|
|
}
|
|
|
|
// NewJSONErrorResponse builds an *http.Response with a JSON body
|
|
// and optional Retry-After header. Used to synthesize bridge-side
|
|
// error responses (e.g. key-pool exhaustion, marshaling
|
|
// fallbacks). Retry-After is set to whole seconds (rounded up)
|
|
// when retryAfter is positive, and omitted otherwise.
|
|
func NewJSONErrorResponse(status int, retryAfter time.Duration, body []byte) *http.Response {
|
|
h := http.Header{}
|
|
h.Set("Content-Type", "application/json")
|
|
if retryAfter > 0 {
|
|
h.Set("Retry-After", strconv.Itoa(int(math.Ceil(retryAfter.Seconds()))))
|
|
}
|
|
return &http.Response{
|
|
Status: fmt.Sprintf("%d %s", status, http.StatusText(status)),
|
|
StatusCode: status,
|
|
Proto: "HTTP/1.1",
|
|
ProtoMajor: 1,
|
|
ProtoMinor: 1,
|
|
Header: h,
|
|
Body: io.NopCloser(bytes.NewReader(body)),
|
|
ContentLength: int64(len(body)),
|
|
}
|
|
}
|