Skip to content

Commit 97939c6

Browse files
committed
Address probe comments
1 parent 23024cd commit 97939c6

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

gateway/middleware.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,28 +392,27 @@ func (t *BaseMiddleware) OrgSessionExpiry(orgid string) int64 {
392392
func (t *BaseMiddleware) refreshOrgSessionExpiry(orgid string) {
393393
t.Logger().Debug("Background refresh started for org: ", orgid)
394394

395-
_, err, _ := orgSessionExpiryCache.Do(orgid, func() (interface{}, error) {
395+
orgSessionExpiryCache.Do(orgid, func() (interface{}, error) {
396396
cachedVal, found := t.Gw.ExpiryCache.Get(orgid)
397397
if found {
398398
t.Logger().Debug("Org expiry already refreshed")
399+
399400
return cachedVal, nil
400401
}
401402

402403
s, found := t.OrgSession(orgid)
403404
if found && t.Spec.GlobalConfig.EnforceOrgDataAge {
404405
t.Logger().Debug("Successfully refreshed org expiry from RPC: ", orgid)
405406
t.SetOrgExpiry(orgid, s.DataExpires)
407+
406408
return s.DataExpires, nil
407409
}
408410

409411
t.Logger().Debug("RPC call failed, setting default expiry for org: ", orgid)
410412
t.SetOrgExpiry(orgid, DEFAULT_ORG_SESSION_EXPIRATION)
413+
411414
return DEFAULT_ORG_SESSION_EXPIRATION, nil
412415
})
413-
414-
if err != nil {
415-
t.Logger().WithError(err).Debug("Error during background org expiry refresh")
416-
}
417416
}
418417

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

gateway/middleware_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
headers2 "github.com/TykTechnologies/tyk/header"
1414
"github.com/TykTechnologies/tyk/internal/cache"
1515
"github.com/TykTechnologies/tyk/internal/uuid"
16+
"github.com/TykTechnologies/tyk/rpc"
1617
"github.com/TykTechnologies/tyk/test"
1718

1819
"github.com/TykTechnologies/tyk/config"
@@ -57,7 +58,7 @@ func TestBaseMiddleware_OrgSessionExpiry(t *testing.T) {
5758
assert.Equal(t, v, got)
5859
})
5960

60-
t.Run("should return default and trigger background refresh on cache miss", func(t *testing.T) {
61+
t.Run("should return default immediately on cache miss", func(t *testing.T) {
6162
m := &BaseMiddleware{
6263
Spec: &APISpec{
6364
GlobalConfig: config.Config{
@@ -116,10 +117,18 @@ func TestBaseMiddleware_OrgSessionExpiry(t *testing.T) {
116117
ts.Gw.ExpiryCache.Delete(noOrgSess)
117118

118119
got := m.OrgSessionExpiry(noOrgSess)
119-
assert.Equal(t, DEFAULT_ORG_SESSION_EXPIRATION, got)
120+
assert.Equal(t, DEFAULT_ORG_SESSION_EXPIRATION, got, "Should return default immediately on cache miss")
121+
122+
m.refreshOrgSessionExpiry(noOrgSess)
123+
124+
cachedVal, found := ts.Gw.ExpiryCache.Get(noOrgSess)
125+
assert.True(t, found, "Cache should be populated after background refresh")
126+
assert.Equal(t, DEFAULT_ORG_SESSION_EXPIRATION, cachedVal.(int64), "Cache should contain default expiration when org not found")
120127
})
121128

122129
t.Run("should return default immediately in emergency mode", func(t *testing.T) {
130+
rpc.SetEmergencyMode(t, true)
131+
defer rpc.SetEmergencyMode(t, false)
123132

124133
m := &BaseMiddleware{
125134
Spec: &APISpec{
@@ -154,7 +163,13 @@ func TestBaseMiddleware_OrgSessionExpiry(t *testing.T) {
154163
ts.Gw.ExpiryCache.Delete(sess.OrgID)
155164

156165
got := m.OrgSessionExpiry(sess.OrgID)
157-
assert.Equal(t, DEFAULT_ORG_SESSION_EXPIRATION, got)
166+
assert.Equal(t, DEFAULT_ORG_SESSION_EXPIRATION, got, "Should return default immediately on cache miss")
167+
168+
m.refreshOrgSessionExpiry(sess.OrgID)
169+
170+
cachedVal, found := ts.Gw.ExpiryCache.Get(sess.OrgID)
171+
assert.True(t, found, "Cache should be populated after background refresh")
172+
assert.Equal(t, DEFAULT_ORG_SESSION_EXPIRATION, cachedVal.(int64), "Cache should contain default expiration when EnforceOrgDataAge is disabled")
158173
})
159174
}
160175

0 commit comments

Comments
 (0)