Skip to content

Commit 083bc9f

Browse files
committed
Implement context propagation through to SessionDetail
1 parent 10c3d16 commit 083bc9f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

gateway/auth_manager.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gateway
22

33
import (
4+
"context"
45
"encoding/base64"
56
"encoding/json"
67
"strings"
@@ -21,6 +22,7 @@ type SessionHandler interface {
2122
UpdateSession(keyName string, session *user.SessionState, resetTTLTo int64, hashed bool) error
2223
RemoveSession(orgID string, keyName string, hashed bool) bool
2324
SessionDetail(orgID string, keyName string, hashed bool) (user.SessionState, bool)
25+
SessionDetailContext(ctx context.Context, orgID string, keyName string, hashed bool) (user.SessionState, bool)
2426
KeyExpired(newSession *user.SessionState) bool
2527
Sessions(filter string) []string
2628
ResetQuota(string, *user.SessionState, bool)
@@ -159,6 +161,28 @@ func (b *DefaultSessionManager) RemoveSession(orgID string, keyName string, hash
159161

160162
// SessionDetail returns the session detail using the storage engine (either in memory or Redis)
161163
func (b *DefaultSessionManager) SessionDetail(orgID string, keyName string, hashed bool) (user.SessionState, bool) {
164+
return b.fetchSessionDetail(nil, orgID, keyName, hashed)
165+
}
166+
167+
// SessionDetailContext returns the session detail using the storage engine with context support for cancellation
168+
func (b *DefaultSessionManager) SessionDetailContext(ctx context.Context, orgID string, keyName string, hashed bool) (user.SessionState, bool) {
169+
return b.fetchSessionDetail(ctx, orgID, keyName, hashed)
170+
}
171+
172+
// fetchSessionDetail is the internal implementation shared by SessionDetail and SessionDetailContext
173+
func (b *DefaultSessionManager) fetchSessionDetail(ctx context.Context, orgID string, keyName string, hashed bool) (user.SessionState, bool) {
174+
if ctx != nil {
175+
select {
176+
case <-ctx.Done():
177+
log.WithFields(logrus.Fields{
178+
"prefix": "auth-mgr",
179+
"inbound-key": b.Gw.obfuscateKey(keyName),
180+
}).Debug("Context cancelled")
181+
return user.SessionState{}, false
182+
default:
183+
}
184+
}
185+
162186
var jsonKeyVal string
163187
var err error
164188
keyId := keyName

gateway/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func (t *BaseMiddleware) fetchOrgSessionWithTimeout(orgID string) (user.SessionS
368368
}
369369
}()
370370

371-
session, found := t.Spec.OrgSessionManager.SessionDetail(orgID, orgID, false)
371+
session, found := t.Spec.OrgSessionManager.SessionDetailContext(timeoutCtx, orgID, orgID, false)
372372
if found && t.Spec.GlobalConfig.EnforceOrgDataAge {
373373
t.Logger().Debug("Setting data expiry: ", orgID)
374374
t.Gw.ExpiryCache.Set(session.OrgID, session.DataExpires, cache.DefaultExpiration)

gateway/middleware_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gateway
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
@@ -40,6 +41,17 @@ func (m mockStore) SessionDetail(orgID string, keyName string, hashed bool) (use
4041
return sess.Clone(), !m.DetailNotFound
4142
}
4243

44+
func (m mockStore) SessionDetailContext(ctx context.Context, orgID string, keyName string, hashed bool) (user.SessionState, bool) {
45+
if m.Delay > 0 {
46+
select {
47+
case <-time.After(m.Delay):
48+
case <-ctx.Done():
49+
return user.SessionState{}, false
50+
}
51+
}
52+
return sess.Clone(), !m.DetailNotFound
53+
}
54+
4355
func TestBaseMiddleware_OrgSessionExpiry(t *testing.T) {
4456
ts := StartTest(nil)
4557
defer ts.Close()

0 commit comments

Comments
 (0)