Skip to content

Commit 49f4aea

Browse files
committed
Address more probe comments
1 parent 69afd6f commit 49f4aea

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

gateway/mw_organisation_activity.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,35 @@ func (k *OrganizationMonitor) refreshOrgSession() {
394394

395395
k.Logger().Debug("Background refresh started for org session")
396396

397-
session, found := k.OrgSession(k.Spec.OrgID)
398-
if found {
399-
k.cacheOrgSession(session)
400-
k.Logger().Debug("Background refresh completed successfully")
401-
} else {
402-
k.Logger().Debug("Background refresh failed")
397+
// Use timeout to prevent hanging indefinitely
398+
timeoutCtx, cancel := context.WithTimeout(context.Background(), orgSessionFetchTimeout)
399+
defer cancel()
400+
401+
type result struct {
402+
session user.SessionState
403+
found bool
404+
}
405+
resultChan := make(chan result, 1)
406+
407+
go func() {
408+
session, found := k.OrgSession(k.Spec.OrgID)
409+
select {
410+
case resultChan <- result{session: session, found: found}:
411+
case <-timeoutCtx.Done():
412+
return
413+
}
414+
}()
415+
416+
select {
417+
case res := <-resultChan:
418+
if res.found {
419+
k.cacheOrgSession(res.session)
420+
k.Logger().Debug("Background refresh completed successfully")
421+
} else {
422+
k.Logger().Debug("Background refresh failed, org session not found")
423+
}
424+
case <-timeoutCtx.Done():
425+
k.Logger().Warning("Background refresh timed out after 2s")
403426
}
404427
}
405428

gateway/mw_organisation_activity_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,12 @@ func TestOrganizationMonitorStaleWhileRevalidate(t *testing.T) {
539539
}
540540
})
541541

542-
t.Run("should respect timeout on cold start", func(t *testing.T) {
542+
t.Run("should handle cold start gracefully", func(t *testing.T) {
543543
// clear cache to simulate cold start
544544
orgSessionCache.Delete(orgID)
545545
orgRefreshInProgress.Delete(orgID)
546546

547-
// use non-existent org to trigger timeout
547+
// use non-existent org
548548
nonExistentOrgID := "non-existent-org-" + uuid.New()
549549

550550
spec := ts.Gw.apisByID[ts.Gw.apiSpecs[0].APIID]
@@ -558,7 +558,7 @@ func TestOrganizationMonitorStaleWhileRevalidate(t *testing.T) {
558558
},
559559
}
560560

561-
// cold start with non-existent org should time out at 2 seconds
561+
// cold start with non-existent org should return quickly (not found)
562562
start := time.Now()
563563
_, found := monitor.getOrgSessionWithStaleWhileRevalidate()
564564
duration := time.Since(start)
@@ -567,11 +567,9 @@ func TestOrganizationMonitorStaleWhileRevalidate(t *testing.T) {
567567
t.Error("Should not find non-existent org session")
568568
}
569569

570-
if duration < 1900*time.Millisecond {
571-
t.Errorf("Timeout happened too quickly: %v", duration)
572-
}
573-
if duration > 2500*time.Millisecond {
574-
t.Errorf("Timeout took too long: %v", duration)
570+
// should return quickly since org doesn't exist (not wait for full timeout)
571+
if duration > 500*time.Millisecond {
572+
t.Errorf("Should return quickly when org not found, took %v", duration)
575573
}
576574
})
577575
}

0 commit comments

Comments
 (0)