feat: add timeout to SMD token refresh and benchmark - #130
Conversation
3360725 to
ba43b84
Compare
Introduce a 10‑second timeout for SMD token refresh operations to prevent potential deadlocks when the OIDC token endpoint is slow or unresponsive. A new constant defaultRefreshTimeout is defined and used with context.WithTimeout in both RefreshToken and refreshTokenIfCurrent. A refreshLock mutex is added to serialize concurrent refresh attempts, ensuring only one refresh request proceeds at a time. Additionally, a benchmark BenchmarkRefreshToken (in smdclient_benchmark_test.go) measures the latency of the refresh path. Comprehensive tests have been added to verify correct behavior under concurrent requests. These changes improve robustness of the SMD client during token expiration handling. Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
ba43b84 to
206a36f
Compare
| if err != nil { | ||
| return err | ||
| } | ||
| r, err := http.DefaultClient.Do(req) |
There was a problem hiding this comment.
shouldn't this be s.smdClient rather than http.DefaultClient to benefit from our custom configs?
There was a problem hiding this comment.
Not sure how I missed that... Fixed in 0a95d36. Added nil-check for the client, let me know if the error message should be revised.
| defer s.accessTokenMutex.Unlock() | ||
| return s.refreshTokenLocked() | ||
| // Serialize refresh to avoid concurrent token fetches. | ||
| s.refreshLock.Lock() |
There was a problem hiding this comment.
I think s.refreshLock doesn't exist in this branch.
There was a problem hiding this comment.
Whoops, somehow did not get committed (fe17047). I also somehow missed running go build to catch that...
| if err != nil { | ||
| return err | ||
| } | ||
| r, err := http.DefaultClient.Do(req) |
There was a problem hiding this comment.
s.SMDClient has a built-in http client that is optimized for SMD queries. It is probably better here.
There was a problem hiding this comment.
Dumb error on my part... Addressed in 0a95d36.
Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
Description
Introduces a 10‑second timeout for SMD token refresh operations to prevent potential deadlocks when the OIDC token endpoint is slow or unresponsive. A new constant
defaultRefreshTimeoutis defined and used withcontext.WithTimeoutin bothRefreshTokenandrefreshTokenIfCurrent. ArefreshLockmutex is added to serialize concurrent refresh attempts, ensuring only one refresh request proceeds at a time. Additionally, a benchmarkBenchmarkRefreshToken(insmdclient_benchmark_test.go) measures the latency of the refresh path. Comprehensive tests have been added to verify correct behavior under concurrent requests. These changes improve robustness of the SMD client during token expiration handling.Fixes #128 (comment)
Checklist
make test(or equivalent) locally and all tests passgit commit -s) with my real name and email<filename>.licensesidecarLICENSES/directoryType of Change