Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed panics on msg.Name != "" & redis is disabled #106

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ func (opt *QueueOptions) Init() {
}

if opt.Storage == nil {
opt.Storage = newRedisStorage(opt.Redis)
if opt.Redis != nil {
opt.Storage = newRedisStorage(opt.Redis)
} else {
opt.Storage = localStorage{}
}
}

if !opt.RateLimit.IsZero() && opt.RateLimiter == nil && opt.Redis != nil {
Expand Down
64 changes: 64 additions & 0 deletions storage.go
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
}
58 changes: 0 additions & 58 deletions taskq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"log"
"os"
"sync"
"time"

"github.com/go-redis/redis/v8"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/vmihailenco/taskq/v3/internal"
)

Expand Down Expand Up @@ -41,59 +39,3 @@ type Redis interface {
ScriptExists(ctx context.Context, scripts ...string) *redis.BoolSliceCmd
ScriptLoad(ctx context.Context, script string) *redis.StringCmd
}

type Storage interface {
Exists(ctx context.Context, key string) bool
}

type redisStorage struct {
redis Redis
}

var _ Storage = (*redisStorage)(nil)

func newRedisStorage(redis Redis) redisStorage {
return redisStorage{
redis: redis,
}
}

func (s redisStorage) Exists(ctx context.Context, key string) bool {
if localCacheExists(key) {
return true
}

val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result()
if err != nil {
return true
}
return !val
}

//------------------------------------------------------------------------------

var (
mu sync.Mutex
cache *simplelru.LRU
)

func localCacheExists(key string) bool {
mu.Lock()
defer mu.Unlock()

if cache == nil {
var err error
cache, err = simplelru.NewLRU(128000, nil)
if err != nil {
panic(err)
}
}

_, ok := cache.Get(key)
if ok {
return true
}

cache.Add(key, nil)
return false
}