Skip to content

Commit 673a3ea

Browse files
committed
set contexts are now used as parent contexts for timeouts
1 parent 1154ed8 commit 673a3ea

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

go/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ func main() {
120120
```
121121
### Custom Context
122122

123-
If you want even greater control over the lifecycle of the HTTP requests consider providing a [context](https://pkg.go.dev/context). Contexts can be set for all requests or per request.
123+
If you want even greater control over the lifecycle of the HTTP requests consider providing a [context](https://pkg.go.dev/context). Contexts can be set for all requests or per request. They can be combined with the timeout options mentioned in the previous section to fine-tune the lifecycle of your requests.
124124

125125
If you wish to include timeout functionality in your custom context then you should leverage [context.WithTimeout](https://pkg.go.dev/context#WithTimeout).
126126

127-
> Note: Setting a context will override any "Timeout" settings that you have applied!
127+
> Note: Custom contexts will be used as the parent context for any timeout you set as specified in the previous section. If the parent context gets cancelled it will propagate to the child context, but if the timeout context times out it does not propagate to the parent context.
128128
129129
#### Custom Context for all requests
130130

131-
Follow the example code snippet below if you want all requests to use the same context:
131+
Follow the example code snippet below if you want all requests to use the same parent context:
132132

133133
```go
134134
import "context"
@@ -146,13 +146,13 @@ func main() {
146146
}
147147
```
148148

149-
> Note: A context set here will also apply to background requests to fetch/refresh oauth tokens, which are normally separated from contexts set via the Timeout property.
149+
> Note: A context set here will become the parent context for all API calls as well as all requests to fetch/refresh oauth tokens, which are normally completely isolated from contexts set via the Timeout property. In this case the token refresh requests and each individual API call will share a common parent context.
150150
151151
#### Custom Context per request
152152

153153
Follow the example here to set a context for a specific request.
154154

155-
> Note: This will override any "Timeout" settings that you have applied, as well as any context set in the SDK config as outlined in the previus section!
155+
> Note: This will be used as the parent context for any timeout setting you've specified for API calls. If you've set contexts in both your API config and in the request options the request options context will be used instead. Background requests to fetch/refresh oauth tokens will NOT use a context set via request options - it will default to use a generic background context or, if you've also set a context in the API config it will still use that as specified in the previous section.
156156
157157
```go
158158
import "context"

go/rtl/auth.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,26 @@ func (s *AuthSession) Do(result interface{}, method, ver, path string, reqPars m
128128
}
129129
}
130130

131-
var ctx context.Context
131+
parent := context.Background()
132+
if s.Config.Context != nil {
133+
parent = s.Config.Context
134+
}
132135
if options != nil && options.Context != nil {
133-
ctx = options.Context
134-
} else if s.Config.Context != nil {
135-
ctx = s.Config.Context
136-
} else {
137-
// create request context with timeout from options or else config or else 120 seconds
138-
var timeoutInSeconds int32 = 120 //seconds
139-
if s.Config.Timeout != 0 {
140-
timeoutInSeconds = s.Config.Timeout
141-
}
142-
if options != nil && options.Timeout != 0 {
143-
timeoutInSeconds = options.Timeout
144-
}
145-
var cncl context.CancelFunc
146-
ctx, cncl = context.WithTimeout(context.Background(), time.Second*time.Duration(timeoutInSeconds))
147-
defer cncl()
136+
parent = options.Context
148137
}
149138

139+
// create request context with timeout from options or else config or else 120 seconds
140+
var timeoutInSeconds int32 = 120
141+
if s.Config.Timeout != 0 {
142+
timeoutInSeconds = s.Config.Timeout
143+
}
144+
if options != nil && options.Timeout != 0 {
145+
timeoutInSeconds = options.Timeout
146+
}
147+
148+
ctx, cncl := context.WithTimeout(parent, time.Second*time.Duration(timeoutInSeconds))
149+
defer cncl()
150+
150151
// create new request
151152
req, err := http.NewRequestWithContext(ctx, method, u, bytes.NewBufferString(bodyString))
152153
if err != nil {

go/rtl/auth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func TestAuthSession_Do_Timeout(t *testing.T) {
724724
}
725725
})
726726

727-
t.Run("Context set in AuthSession config overrides Timeouts", func(t *testing.T) {
727+
t.Run("Parent context timeout propagates to timeout child context", func(t *testing.T) {
728728
mux := http.NewServeMux()
729729
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
730730
server := httptest.NewServer(mux)
@@ -757,7 +757,7 @@ func TestAuthSession_Do_Timeout(t *testing.T) {
757757
}
758758
})
759759

760-
t.Run("Context set in options overrides config ctx and all Timeouts", func(t *testing.T) {
760+
t.Run("Parent context set in options overrides config ctx and propagates to child timout", func(t *testing.T) {
761761
mux := http.NewServeMux()
762762
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
763763
server := httptest.NewServer(mux)

0 commit comments

Comments
 (0)