Always propagate context through network calls to ensure proper cancellation, timeout handling, and tracing. For HTTP requests, use http.NewRequestWithContext()
instead of http.Post()
or http.Get()
. For gRPC, ensure context is passed and enriched with necessary metadata.
When adding metadata to context:
metadata.AppendToOutgoingContext(ctx, key, value)
metadata.AppendToOutgoingContext(ctx, k1, v1, k2, v2, ...)
Example of context-aware HTTP client:
// Instead of this:
response, err := http.Post(a.opaEndpoint, "application/json", bytes.NewBuffer(jsonData))
// Do this:
req, err := http.NewRequestWithContext(ctx, "POST", a.opaEndpoint, bytes.NewBuffer(jsonData))
if err != nil {
return resultDeny, err
}
req.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(req)
Example of proper gRPC context metadata:
// Batch metadata in a single call
ctx = metadata.AppendToOutgoingContext(ctx,
"temporal-test-name", testName,
"user-id", userId,
"request-id", requestId)
Context propagation ensures that cancellation signals are properly passed throughout the system, preventing resource leaks and allowing proper tracing of distributed operations.
Enter the URL of a public GitHub repository