Skip to content

Commit b85f84a

Browse files
committed
Address bot comments
1 parent fea50ac commit b85f84a

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

gateway/middleware.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,17 @@ func (t *BaseMiddleware) OrgSessionExpiry(orgid string) int64 {
439439
return val
440440
}
441441

442-
// Cache miss, start async refresh in background, return default immediately
443-
go t.refreshOrgSessionExpiry(orgid)
442+
// Cache miss
443+
go orgSessionExpiryCache.Do(orgid, func() (interface{}, error) {
444+
return t.refreshOrgSessionExpiry(orgid)
445+
})
444446

445447
t.Logger().Debug("no cached entry found, returning 7 days (async refresh started)")
446448
return DEFAULT_ORG_SESSION_EXPIRATION
447449
}
448450

449451
// refreshOrgSessionExpiry fetches org session expiry in the background
450-
func (t *BaseMiddleware) refreshOrgSessionExpiry(orgid string) {
452+
func (t *BaseMiddleware) refreshOrgSessionExpiry(orgid string) (interface{}, error) {
451453
defer func() {
452454
if r := recover(); r != nil {
453455
t.Logger().Errorf("Panic recovered during org session expiry refresh for org %s: %v", orgid, r)
@@ -458,10 +460,12 @@ func (t *BaseMiddleware) refreshOrgSessionExpiry(orgid string) {
458460
if found && t.Spec.GlobalConfig.EnforceOrgDataAge {
459461
t.Logger().Debug("Background refresh: setting data expiry for org: ", orgid)
460462
t.SetOrgExpiry(orgid, s.DataExpires)
461-
} else {
462-
t.Logger().Debug("Background refresh: org session not found, setting default expiry")
463-
t.SetOrgExpiry(orgid, DEFAULT_ORG_SESSION_EXPIRATION)
463+
return s.DataExpires, nil
464464
}
465+
466+
t.Logger().Debug("Background refresh: org session not found, setting default expiry")
467+
t.SetOrgExpiry(orgid, DEFAULT_ORG_SESSION_EXPIRATION)
468+
return DEFAULT_ORG_SESSION_EXPIRATION, nil
465469
}
466470

467471
func (t *BaseMiddleware) UpdateRequestSession(r *http.Request) bool {

gateway/middleware_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type mockStore struct {
2424
SessionHandler
2525
//DetailNotFound is used to make mocked SessionDetail return (x,false), as if it doesn't find the session in the mocked storage.
2626
DetailNotFound bool
27+
// Delay simulates a slow RPC call for timeout testing
28+
Delay time.Duration
2729
}
2830

2931
var sess = user.SessionState{
@@ -32,6 +34,9 @@ var sess = user.SessionState{
3234
}
3335

3436
func (m mockStore) SessionDetail(orgID string, keyName string, hashed bool) (user.SessionState, bool) {
37+
if m.Delay > 0 {
38+
time.Sleep(m.Delay)
39+
}
3540
return sess.Clone(), !m.DetailNotFound
3641
}
3742

@@ -642,4 +647,41 @@ func TestBaseMiddleware_OrgSession(t *testing.T) {
642647
assert.NotEmpty(t, session.OrgID)
643648
}
644649
})
650+
651+
t.Run("should timeout when RPC call takes too long", func(t *testing.T) {
652+
timeoutOrgID := "timeout-org-" + uuid.New()
653+
654+
slowMockStore := mockStore{
655+
DetailNotFound: false,
656+
Delay: 3 * time.Second,
657+
}
658+
659+
specSlow := &APISpec{
660+
GlobalConfig: config.Config{
661+
EnforceOrgDataAge: true,
662+
LocalSessionCache: config.LocalSessionCacheConf{
663+
DisableCacheSessionState: false,
664+
},
665+
},
666+
OrgSessionManager: slowMockStore,
667+
}
668+
669+
baseMidSlow := &BaseMiddleware{
670+
Spec: specSlow,
671+
Gw: ts.Gw,
672+
logger: mainLog,
673+
}
674+
675+
start := time.Now()
676+
session, found := baseMidSlow.fetchOrgSessionWithTimeout(timeoutOrgID)
677+
elapsed := time.Since(start)
678+
679+
// Should timeout and return false
680+
assert.False(t, found, "Should timeout and return false")
681+
assert.Empty(t, session.OrgID, "Session should be empty on timeout")
682+
683+
// Should timeout around 2 seconds, not wait for the full 3 second delay
684+
assert.Less(t, elapsed, 3*time.Second, "Should timeout before slow RPC completes")
685+
assert.GreaterOrEqual(t, elapsed, 2*time.Second, "Should wait for timeout duration")
686+
})
645687
}

0 commit comments

Comments
 (0)