-
Notifications
You must be signed in to change notification settings - Fork 27
feat: response body mode framework for streaming support #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5f2053c
541110e
c5b83af
1194b3a
63991fd
d793ccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ type RequestContext struct { | |
| RequestSentTimestamp time.Time | ||
| ResponseFirstChunkTimestamp time.Time | ||
| ResponseCompleteTimestamp time.Time | ||
| ResponseHeadersSent bool | ||
| Profile *requesthandling.Profile | ||
| CycleState *plugin.CycleState | ||
| Request *requesthandling.InferenceRequest | ||
|
|
@@ -176,15 +177,25 @@ func (s *Server) Process(srv extProcPb.ExternalProcessor_ProcessServer) error { | |
| if reqCtx.ResponseFirstChunkTimestamp.IsZero() { | ||
| reqCtx.ResponseFirstChunkTimestamp = time.Now() | ||
| } | ||
| responseBody = append(responseBody, v.ResponseBody.Body...) | ||
| if !v.ResponseBody.EndOfStream { | ||
| continue | ||
|
|
||
| if reqCtx.Profile.NeedsResponseBuffering { | ||
| responseBody = append(responseBody, v.ResponseBody.Body...) | ||
| if !v.ResponseBody.EndOfStream { | ||
| // Keep accumulating — don't send responses or record metrics yet. | ||
| break | ||
| } | ||
| responses, err = s.HandleResponseBody(ctx, reqCtx, responseBody) | ||
| loggerVerbose.Info("processing response body complete") | ||
| } else { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now this looks nice and clean :) |
||
| responses, err = s.HandleResponseChunk(ctx, reqCtx, v.ResponseBody.Body, v.ResponseBody.EndOfStream) | ||
| loggerVerbose.Info("response chunk processing complete") | ||
| } | ||
|
|
||
| if v.ResponseBody.EndOfStream { | ||
| reqCtx.ResponseCompleteTimestamp = time.Now() | ||
| model, _ := reqCtx.Request.Body["model"].(string) | ||
| metrics.RecordRequestTTFT(model, reqCtx.ResponseFirstChunkTimestamp.Sub(reqCtx.RequestReceivedTimestamp)) | ||
| } | ||
| reqCtx.ResponseCompleteTimestamp = time.Now() | ||
| model, _ := reqCtx.Request.Body["model"].(string) | ||
| metrics.RecordRequestTTFT(model, reqCtx.ResponseFirstChunkTimestamp.Sub(reqCtx.RequestReceivedTimestamp)) | ||
| responses, err = s.HandleResponseBody(ctx, reqCtx, responseBody) | ||
| loggerVerbose.Info("processing response body complete") | ||
| case *extProcPb.ProcessingRequest_ResponseTrailers: | ||
| responses, err = s.HandleResponseTrailers(v.ResponseTrailers) | ||
| default: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,6 +123,7 @@ func TestHandleResponseBody_Streaming(t *testing.T) { | |
| wantFullBody := []byte(`{"choices":[{"text":"Hello!"}]}`) | ||
|
|
||
| profiles := newTestProfiles() | ||
| profiles[testProfileName].NeedsResponseBuffering = true | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add in a follow up unit tests to cover the new funcionality?
not a blocker for this PR (can be pushed in follow up).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed — will add in a follow-up PR. |
||
| ref := newServerForTest(profiles) | ||
| want, err := ref.HandleResponseBody(ctx, newTestRequestContext(profiles), wantFullBody) | ||
| if err != nil { | ||
|
|
@@ -164,6 +165,7 @@ func TestHandleResponseBody_Streaming(t *testing.T) { | |
| t.Run(tc.name, func(t *testing.T) { | ||
| streamCtx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background())) | ||
| profiles := newTestProfiles() | ||
| profiles[testProfileName].NeedsResponseBuffering = true | ||
| srv := newServerForTest(profiles) | ||
| testListener, errChan := utils.SetupTestStreamingServer(t, streamCtx, srv) | ||
| process, conn := utils.GetStreamingServerClient(streamCtx, t) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something is still wrong in this function..
chunk processor plugins cannot mutate the chunk content, and even if they do it's ignored in L151.
also, I've noticed that BodyMutation is always used even if chunk hasn't changed, which is an expensive operation in envoy..
we should also probably distinguish between the case when chunk was mutated and the case when it was passed through.
we can improve that last point on a follow up PR, leaving mutation always works for now.
but the first point is functionality wise wrong.