Skip to content

Commit 8e5e581

Browse files
committed
fixed panics on msg.Name != "" & redis is disabled
1 parent 17eecbc commit 8e5e581

File tree

2 files changed

+64
-58
lines changed

2 files changed

+64
-58
lines changed

storage.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package taskq
2+
3+
import (
4+
"context"
5+
"sync"
6+
"time"
7+
8+
"github.com/hashicorp/golang-lru/simplelru"
9+
)
10+
11+
type Storage interface {
12+
Exists(ctx context.Context, key string) bool
13+
}
14+
15+
var _ Storage = (*localStorage)(nil)
16+
var _ Storage = (*redisStorage)(nil)
17+
18+
// LOCAL
19+
20+
type localStorage struct {
21+
mu sync.Mutex
22+
cache *simplelru.LRU
23+
}
24+
25+
func (s localStorage) Exists(_ context.Context, key string) bool {
26+
s.mu.Lock()
27+
defer s.mu.Unlock()
28+
29+
if s.cache == nil {
30+
var err error
31+
s.cache, err = simplelru.NewLRU(128000, nil)
32+
if err != nil {
33+
panic(err)
34+
}
35+
}
36+
37+
_, ok := s.cache.Get(key)
38+
if ok {
39+
return true
40+
}
41+
42+
s.cache.Add(key, nil)
43+
return false
44+
}
45+
46+
// REDIS
47+
48+
type redisStorage struct {
49+
redis Redis
50+
}
51+
52+
func newRedisStorage(redis Redis) redisStorage {
53+
return redisStorage{
54+
redis: redis,
55+
}
56+
}
57+
58+
func (s redisStorage) Exists(ctx context.Context, key string) bool {
59+
val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result()
60+
if err != nil {
61+
return true
62+
}
63+
return !val
64+
}

taskq.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"context"
55
"log"
66
"os"
7-
"sync"
87
"time"
98

109
"github.com/go-redis/redis/v8"
11-
"github.com/hashicorp/golang-lru/simplelru"
1210
"github.com/vmihailenco/taskq/v3/internal"
1311
)
1412

@@ -41,59 +39,3 @@ type Redis interface {
4139
ScriptExists(ctx context.Context, scripts ...string) *redis.BoolSliceCmd
4240
ScriptLoad(ctx context.Context, script string) *redis.StringCmd
4341
}
44-
45-
type Storage interface {
46-
Exists(ctx context.Context, key string) bool
47-
}
48-
49-
type redisStorage struct {
50-
redis Redis
51-
}
52-
53-
var _ Storage = (*redisStorage)(nil)
54-
55-
func newRedisStorage(redis Redis) redisStorage {
56-
return redisStorage{
57-
redis: redis,
58-
}
59-
}
60-
61-
func (s redisStorage) Exists(ctx context.Context, key string) bool {
62-
if localCacheExists(key) {
63-
return true
64-
}
65-
66-
val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result()
67-
if err != nil {
68-
return true
69-
}
70-
return !val
71-
}
72-
73-
//------------------------------------------------------------------------------
74-
75-
var (
76-
mu sync.Mutex
77-
cache *simplelru.LRU
78-
)
79-
80-
func localCacheExists(key string) bool {
81-
mu.Lock()
82-
defer mu.Unlock()
83-
84-
if cache == nil {
85-
var err error
86-
cache, err = simplelru.NewLRU(128000, nil)
87-
if err != nil {
88-
panic(err)
89-
}
90-
}
91-
92-
_, ok := cache.Get(key)
93-
if ok {
94-
return true
95-
}
96-
97-
cache.Add(key, nil)
98-
return false
99-
}

0 commit comments

Comments
 (0)