-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed panics on msg.Name != "" & redis is disabled
- Loading branch information
1 parent
17eecbc
commit 8e5e581
Showing
2 changed files
with
64 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package taskq | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/hashicorp/golang-lru/simplelru" | ||
) | ||
|
||
type Storage interface { | ||
Exists(ctx context.Context, key string) bool | ||
} | ||
|
||
var _ Storage = (*localStorage)(nil) | ||
var _ Storage = (*redisStorage)(nil) | ||
|
||
// LOCAL | ||
|
||
type localStorage struct { | ||
mu sync.Mutex | ||
cache *simplelru.LRU | ||
} | ||
|
||
func (s localStorage) Exists(_ context.Context, key string) bool { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if s.cache == nil { | ||
var err error | ||
s.cache, err = simplelru.NewLRU(128000, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
_, ok := s.cache.Get(key) | ||
if ok { | ||
return true | ||
} | ||
|
||
s.cache.Add(key, nil) | ||
return false | ||
} | ||
|
||
// REDIS | ||
|
||
type redisStorage struct { | ||
redis Redis | ||
} | ||
|
||
func newRedisStorage(redis Redis) redisStorage { | ||
return redisStorage{ | ||
redis: redis, | ||
} | ||
} | ||
|
||
func (s redisStorage) Exists(ctx context.Context, key string) bool { | ||
val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result() | ||
if err != nil { | ||
return true | ||
} | ||
return !val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters