Skip to content

Commit d1ac56a

Browse files
wolfogreGiteaBot
authored andcommitted
Increase cacheContextLifetime to reduce false reports (go-gitea#32011)
Replace go-gitea#32001. To prevent the context cache from being misused for long-term work (which would result in using invalid cache without awareness), the context cache is designed to exist for a maximum of 10 seconds. This leads to many false reports, especially in the case of slow SQL. This PR increases it to 5 minutes to reduce false reports. 5 minutes is not a very safe value, as a lot of changes may have occurred within that time frame. However, as far as I know, there has not been a case of misuse of context cache discovered so far, so I think 5 minutes should be OK. Please note that after this PR, if warning logs are found again, it should get attention, at that time it can be almost 100% certain that it is a misuse.
1 parent 6d4dfcd commit d1ac56a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

modules/cache/context.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func (cc *cacheContext) isDiscard() bool {
6363
}
6464

6565
// cacheContextLifetime is the max lifetime of cacheContext.
66-
// Since cacheContext is used to cache data in a request level context, 10s is enough.
67-
// If a cacheContext is used more than 10s, it's probably misuse.
68-
const cacheContextLifetime = 10 * time.Second
66+
// Since cacheContext is used to cache data in a request level context, 5 minutes is enough.
67+
// If a cacheContext is used more than 5 minutes, it's probably misuse.
68+
const cacheContextLifetime = 5 * time.Minute
6969

7070
var timeNow = time.Now
7171

@@ -131,7 +131,7 @@ func GetContextData(ctx context.Context, tp, key any) any {
131131
if c.Expired() {
132132
// The warning means that the cache context is misused for long-life task,
133133
// it can be resolved with WithNoCacheContext(ctx).
134-
log.Warn("cache context is expired, may be misused for long-life tasks: %v", c)
134+
log.Warn("cache context is expired, is highly likely to be misused for long-life tasks: %v", c)
135135
return nil
136136
}
137137
return c.Get(tp, key)
@@ -144,7 +144,7 @@ func SetContextData(ctx context.Context, tp, key, value any) {
144144
if c.Expired() {
145145
// The warning means that the cache context is misused for long-life task,
146146
// it can be resolved with WithNoCacheContext(ctx).
147-
log.Warn("cache context is expired, may be misused for long-life tasks: %v", c)
147+
log.Warn("cache context is expired, is highly likely to be misused for long-life tasks: %v", c)
148148
return
149149
}
150150
c.Put(tp, key, value)
@@ -157,7 +157,7 @@ func RemoveContextData(ctx context.Context, tp, key any) {
157157
if c.Expired() {
158158
// The warning means that the cache context is misused for long-life task,
159159
// it can be resolved with WithNoCacheContext(ctx).
160-
log.Warn("cache context is expired, may be misused for long-life tasks: %v", c)
160+
log.Warn("cache context is expired, is highly likely to be misused for long-life tasks: %v", c)
161161
return
162162
}
163163
c.Delete(tp, key)

modules/cache/context_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestWithCacheContext(t *testing.T) {
4545
timeNow = now
4646
}()
4747
timeNow = func() time.Time {
48-
return now().Add(10 * time.Second)
48+
return now().Add(5 * time.Minute)
4949
}
5050
v = GetContextData(ctx, field, "my_config1")
5151
assert.Nil(t, v)

0 commit comments

Comments
 (0)