Skip to content

Commit 4f972cb

Browse files
committed
flush out race conditions
1 parent 724008f commit 4f972cb

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

cmd/cache-offloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func getInMemoryStorage(cfg config.Config) http.Cacher {
2121
case "lru":
2222
return storage.NewLRUCache(cfg.CacheConfig.Size, cfg.CacheConfig.StaleInSeconds)
2323
case "lfu":
24-
storage.NewLFUCache(cfg.CacheConfig.Size, cfg.CacheConfig.StaleInSeconds)
24+
return storage.NewLFUCache(cfg.CacheConfig.Size, cfg.CacheConfig.StaleInSeconds)
2525
default:
2626
log.Error().Msgf("Unknown cache strategy: %s", cfg.CacheConfig.Strategy)
2727
log.Fatal().Msg("Supported cache strategies are LRU and LFU")

dev.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# ServerConfig
22
SERVER_PORT=8000
3-
SERVER_LOG_LEVEL=DEBUG
3+
SERVER_LOG_LEVEL=ERROR
44
SERVER_STORAGE=memory
55
DOWNSTREAM_HOST=http://httpbin.org
66
SHUTDOWN_GRACE_PERIOD=30
77

88
# CacheConfig
99
CACHE_SHOULD_HASH_QUERY="true"
1010
CACHE_HASH_QUERY_IGNORE=utm,live
11-
CACHE_STRATEGY=LRU
11+
CACHE_STRATEGY=LFU
1212
CACHE_SIZE_MB=10
1313
CACHE_STALE_WHILE_REVALIDATE_SEC=60
1414
CACHE_IGNORE_ENDPOINTS=/status,/health

pkg/storage/memory-lfu.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func (lfu *LFUCache) LookUp(ctx context.Context, key string) (*model.Response, e
103103
proc := make(chan *model.Response, 1)
104104

105105
go func() {
106-
lfu.mtx.RLock()
107-
defer lfu.mtx.RUnlock()
106+
lfu.mtx.Lock()
107+
defer lfu.mtx.Unlock()
108108

109109
if val, found := lfu.cache[key]; found {
110110
lfu.update(val)
@@ -136,6 +136,9 @@ func (lfu *LFUCache) LookUp(ctx context.Context, key string) (*model.Response, e
136136
}
137137

138138
func (lfu *LFUCache) update(node *list.Element) {
139+
if node == nil {
140+
return
141+
}
139142
parent := node.Value.(*LfuNode).parent
140143
count := parent.counter
141144
freqList := parent.lruCache

pkg/storage/memory-lru.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func (lru *LRUCache) LookUp(ctx context.Context, key string) (*model.Response, e
8484
proc := make(chan *model.Response, 1)
8585

8686
go func() {
87-
lru.mtx.RLock()
88-
defer lru.mtx.RUnlock()
87+
lru.mtx.Lock()
88+
defer lru.mtx.Unlock()
8989

9090
if value, found := lru.cache[key]; found {
9191
lru.responses.MoveToFront(value)

race.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import http from "k6/http";
2+
3+
function getRandomNumberBetween(end) {
4+
return Math.floor(Math.random() * end) + 1;
5+
}
6+
7+
export default function () {
8+
for (let id = 1; id <= 100; id++) {
9+
const randInt = getRandomNumberBetween(100);
10+
let url;
11+
let tag = {};
12+
if (randInt % 2 === 0) {
13+
url = `http://localhost:8000/headers?utm=${id}`;
14+
tag = {
15+
name: "utm",
16+
};
17+
} else {
18+
url = `http://localhost:8000/headers?bobby=${id}`;
19+
tag = {
20+
name: "bobby",
21+
};
22+
}
23+
http.get(url, {
24+
tags: tag,
25+
});
26+
}
27+
}

0 commit comments

Comments
 (0)