Skip to content

Commit 94c87aa

Browse files
committed
Address probe comments
1 parent 458bee5 commit 94c87aa

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

gateway/mw_organisation_activity.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ type orgCacheEntry struct {
3030
var orgSessionCache sync.Map
3131
var orgRefreshInProgress sync.Map // prevents multiple concurrent refreshes
3232

33+
// Cache expiry durations for org sessions
34+
const (
35+
orgSessionSoftExpiry = 10 * time.Minute
36+
orgSessionHardExpiry = 1 * time.Hour
37+
orgSessionFetchTimeout = 2 * time.Second
38+
)
39+
3340
// RateLimitAndQuotaCheck will check the incoming request and key whether it is within it's quota and
3441
// within it's rate limit, it makes use of the SessionLimiter object to do this
3542
type OrganizationMonitor struct {
@@ -350,20 +357,31 @@ func (k *OrganizationMonitor) getOrgSessionWithStaleWhileRevalidate() (user.Sess
350357
}
351358

352359
k.Logger().Debug("No cached org session, attempting fresh fetch with timeout")
353-
timeoutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
360+
timeoutCtx, cancel := context.WithTimeout(context.Background(), orgSessionFetchTimeout)
354361
defer cancel()
355362

356-
sessionChan := make(chan user.SessionState, 1)
363+
type result struct {
364+
session user.SessionState
365+
found bool
366+
}
367+
resultChan := make(chan result, 1)
368+
357369
go func() {
358-
if session, found := k.OrgSession(k.Spec.OrgID); found {
359-
sessionChan <- session
370+
session, found := k.OrgSession(k.Spec.OrgID)
371+
select {
372+
case resultChan <- result{session: session, found: found}:
373+
case <-timeoutCtx.Done():
374+
return
360375
}
361376
}()
362377

363378
select {
364-
case session := <-sessionChan:
365-
k.cacheOrgSession(session)
366-
return session, true
379+
case res := <-resultChan:
380+
if res.found {
381+
k.cacheOrgSession(res.session)
382+
return res.session, true
383+
}
384+
return user.SessionState{}, false
367385
case <-timeoutCtx.Done():
368386
k.Logger().Warning("Org session fetch timed out after 2s")
369387
return user.SessionState{}, false
@@ -390,8 +408,8 @@ func (k *OrganizationMonitor) cacheOrgSession(session user.SessionState) {
390408
now := time.Now()
391409
entry := &orgCacheEntry{
392410
session: session.Clone(),
393-
softExpiry: now.Add(10 * time.Minute).UnixNano(),
394-
hardExpiry: now.Add(1 * time.Hour).UnixNano(),
411+
softExpiry: now.Add(orgSessionSoftExpiry).UnixNano(),
412+
hardExpiry: now.Add(orgSessionHardExpiry).UnixNano(),
395413
}
396414

397415
orgSessionCache.Store(k.Spec.OrgID, entry)

0 commit comments

Comments
 (0)