Skip to content
Open
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
14 changes: 0 additions & 14 deletions api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ func sendStreamRequest[T responseStream[R], R any](ctx context.Context, ac *apiC
return err
}

// Handle context timeout.
// The request's context deadline is set using [HTTPOptions.Timeout].
// [ClientConfig.HTTPClient.Timeout] does not affect the context deadline for the request.
// [ClientConfig.HTTPClient.Timeout] is used along with `x-server-timeout` header in order to
// get the end-to-end timeout value for logging.
requestContext := ctx
timeout := httpOptions.Timeout
var cancel context.CancelFunc
if timeout != nil && *timeout > 0*time.Second && isTimeoutBeforeDeadline(ctx, *timeout) {
requestContext, cancel = context.WithTimeout(ctx, *timeout)
defer cancel()
}
req = req.WithContext(requestContext)

resp, err := doRequest(ac, req)
if err != nil {
return err
Expand Down
98 changes: 0 additions & 98 deletions api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,104 +449,6 @@ func TestSendStreamRequest(t *testing.T) {
wantErr: true,
wantErrorMessage: "doRequest: error sending request: Post \"invalid-url/test\": unsupported protocol scheme",
},
{
name: "With client timeout",
method: "POST",
path: "test",
body: map[string]any{"key": "value"},
mockResponse: "data:{\"key1\":\"value1\"}\n\ndata:{\"key2\":\"value2\"}\n\n",
mockStatusCode: http.StatusOK,
wantResponse: []map[string]any{
{"key1": "value1"},
{"key2": "value2"},
},
clientTimeout: Ptr(100 * time.Millisecond),
serverLatency: 150 * time.Millisecond,
wantErr: true,
wantErrorMessage: "context deadline exceeded",
},
{
name: "With request timeout",
method: "POST",
path: "test",
body: map[string]any{"key": "value"},
mockResponse: "data:{\"key1\":\"value1\"}\n\ndata:{\"key2\":\"value2\"}\n\n",
mockStatusCode: http.StatusOK,
wantResponse: []map[string]any{
{"key1": "value1"},
{"key2": "value2"},
},
requestTimeout: Ptr(100 * time.Millisecond),
serverLatency: 150 * time.Millisecond,
wantErr: true,
wantErrorMessage: "context deadline exceeded",
},
{
name: "With client timeout and request timeout",
method: "POST",
path: "test",
body: map[string]any{"key": "value"},
mockResponse: "data:{\"key1\":\"value1\"}\n\ndata:{\"key2\":\"value2\"}\n\n",
mockStatusCode: http.StatusOK,
wantResponse: []map[string]any{
{"key1": "value1"},
{"key2": "value2"},
},
clientTimeout: Ptr(200 * time.Millisecond),
requestTimeout: Ptr(100 * time.Millisecond),
serverLatency: 150 * time.Millisecond,
wantErr: true,
wantErrorMessage: "context deadline exceeded",
},
{
name: "With 0 client timeout and request timeout",
method: "POST",
path: "test",
body: map[string]any{"key": "value"},
mockResponse: "data:{\"key1\":\"value1\"}\n\ndata:{\"key2\":\"value2\"}\n\n",
mockStatusCode: http.StatusOK,
wantResponse: []map[string]any{
{"key1": "value1"},
{"key2": "value2"},
},
clientTimeout: Ptr(0 * time.Millisecond),
requestTimeout: Ptr(100 * time.Millisecond),
serverLatency: 150 * time.Millisecond,
wantErr: true,
wantErrorMessage: "context deadline exceeded",
},
{
name: "With client timeout and 0 request timeout",
method: "POST",
path: "test",
body: map[string]any{"key": "value"},
mockResponse: "data:{\"key1\":\"value1\"}\n\ndata:{\"key2\":\"value2\"}\n\n",
mockStatusCode: http.StatusOK,
clientTimeout: Ptr(200 * time.Millisecond),
requestTimeout: Ptr(0 * time.Millisecond),
serverLatency: 250 * time.Millisecond,
wantResponse: []map[string]any{
{"key1": "value1"},
{"key2": "value2"},
},
wantErr: false,
},
{
name: "With 0 client timeout and 0 request timeout",
method: "POST",
path: "test",
body: map[string]any{"key": "value"},
mockResponse: "data:{\"key1\":\"value1\"}\n\ndata:{\"key2\":\"value2\"}\n\n",
mockStatusCode: http.StatusOK,
clientTimeout: Ptr(0 * time.Millisecond),
requestTimeout: Ptr(0 * time.Millisecond),
serverLatency: 150 * time.Millisecond,
wantResponse: []map[string]any{
{"key1": "value1"},
{"key2": "value2"},
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down