Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/appthrust/appserver-go/apphealth"
"github.com/appthrust/appserver-go/appserver/health"
"github.com/appthrust/appserver-go/appserver/ollamasupervisor"
"github.com/appthrust/appserver-go/appserver/requestlog"
)

type config struct {
Expand Down Expand Up @@ -91,11 +93,8 @@ func serve(ctx context.Context, listen string, cfg config) error {
defer close(childDone)
s.Run(childCtx)
}()
server := &http.Server{
Handler: newHandler(cfg.model, cfg.upstream, func() bool { return !draining.Load() && s.Ready() }, &outcomes),
ReadHeaderTimeout: 10 * time.Second, ReadTimeout: requestTimeout,
WriteTimeout: requestTimeout + 5*time.Second, IdleTimeout: 60 * time.Second,
}
handler := newHandler(cfg.model, cfg.upstream, func() bool { return !draining.Load() && s.Ready() }, &outcomes)
server := newServer(handler, os.Stderr)
served := make(chan error, 1)
go func() { served <- server.Serve(listener) }()
log.Printf("embedding-server listening on %s (project %s)", listener.Addr(), cfg.projectID)
Expand All @@ -117,3 +116,11 @@ func serve(ctx context.Context, listen string, cfg config) error {
}
return err
}

func newServer(handler http.Handler, logOutput io.Writer) *http.Server {
return &http.Server{
Handler: requestlog.Wrap(handler, requestlog.Options{Service: "embedding", Output: logOutput}),
ReadHeaderTimeout: 10 * time.Second, ReadTimeout: requestTimeout,
WriteTimeout: requestTimeout + 5*time.Second, IdleTimeout: 60 * time.Second,
}
}
10 changes: 9 additions & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/appthrust/appserver-go/appserver/health"
"github.com/appthrust/appserver-go/appserver/httpx"
"github.com/appthrust/appserver-go/appserver/releaseidentity"
"github.com/appthrust/appserver-go/appserver/requestlog"
)

const (
Expand Down Expand Up @@ -130,7 +131,14 @@ func newHandler(model string, upstream *url.URL, ready func() bool, outcomes *he
if response.StatusCode == http.StatusTooManyRequests {
class = apphealth.ReasonResourceExhausted
}
outcomes.Record(copyErr == nil && response.StatusCode >= 200 && response.StatusCode < 300, class)
upstreamOK := response.StatusCode >= 200 && response.StatusCode < 300
switch {
case copyErr != nil:
requestlog.SetOutcome(r, "stream_interrupted", "response_copy_failed")
case !upstreamOK:
requestlog.SetOutcome(r, "upstream_error", "upstream_non_2xx")
}
outcomes.Record(copyErr == nil && upstreamOK, class)
})
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) {
httpx.WriteJSON(w, http.StatusOK, map[string]string{"status": "ok"})
Expand Down
26 changes: 26 additions & 0 deletions cmd/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"encoding/json"
"io"
Expand Down Expand Up @@ -142,6 +143,31 @@ func TestProxyRejectsFifthInFlightRequest(t *testing.T) {
require.Equal(t, http.StatusOK, requestForTest(h, http.MethodPost, "/v1/embeddings", `{"input":"after release"}`).Code)
}

func TestRequestLogForInterruptedEmbeddingResponse(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Length", "1024")
_, _ = io.WriteString(w, "private-partial-body")
}))
defer upstream.Close()
var logs bytes.Buffer
h := newServer(handlerForTest(t, upstream.URL, func() bool { return true }), &logs).Handler
w := requestForTest(h, http.MethodPost, "/v1/embeddings?private-query", `{"input":"private-input"}`)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "private-partial-body", w.Body.String())

require.Equal(t, 1, bytes.Count(logs.Bytes(), []byte("\n")))
require.NotContains(t, logs.String(), "private-")
var entry struct {
Service, Outcome, Reason string
Status int
}
require.NoError(t, json.Unmarshal(logs.Bytes(), &entry))
require.Equal(t, "embedding", entry.Service)
require.Equal(t, http.StatusOK, entry.Status)
require.Equal(t, "stream_interrupted", entry.Outcome)
require.Equal(t, "response_copy_failed", entry.Reason)
}

func TestProxyDoesNotFollowRedirectsAndPreservesUpstreamErrors(t *testing.T) {
var calls atomic.Int32
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/appthrust/embedding
go 1.26.0

require (
github.com/appthrust/appserver-go v0.3.0
github.com/appthrust/appserver-go v0.7.0
github.com/stretchr/testify v1.11.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/appthrust/appserver-go v0.3.0 h1:zxOz0hkspZSXjLq3KWGDLkZIz42Jag6wqRujZ8lTru0=
github.com/appthrust/appserver-go v0.3.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo=
github.com/appthrust/appserver-go v0.7.0 h1:Fd1G+l/Ys0aAAiI2ZmMKqAz4qY6Dv7WwsF/rZtUgPUA=
github.com/appthrust/appserver-go v0.7.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
Expand Down