Skip to content

Commit

Permalink
Update packages + use the new redis v9 package
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Mar 20, 2023
1 parent 0e8611a commit f121325
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 201 deletions.
60 changes: 20 additions & 40 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,76 +1,56 @@
module github.com/geniusrabbit/notificationcenter/v2

go 1.18
go 1.19

require (
github.com/Shopify/sarama v1.38.0
github.com/alicebob/miniredis/v2 v2.30.0
github.com/Shopify/sarama v1.38.1
github.com/alicebob/miniredis/v2 v2.30.1
github.com/allegro/bigcache v1.2.1
github.com/demdxx/gocast/v2 v2.3.1
github.com/demdxx/gocast/v2 v2.5.0
github.com/demdxx/rpool/v2 v2.0.1
github.com/elliotchance/redismock/v8 v8.11.1
github.com/go-redis/redis/v8 v8.11.5
github.com/golang/mock v1.6.0
github.com/lib/pq v1.10.7
github.com/nats-io/nats.go v1.22.1
github.com/nats-io/nats.go v1.24.0
github.com/nats-io/stan.go v0.10.4
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
go.uber.org/multierr v1.9.0
github.com/redis/go-redis/v9 v9.0.2
github.com/stretchr/testify v1.8.2
go.uber.org/multierr v1.10.0
)

require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878 // indirect
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/eapache/go-resiliency v1.3.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230111030713-bf00bc1b83b6 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v1.1.0 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-msgpack v1.1.5 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/hashicorp/raft v1.3.11 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.3 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.15.14 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/nats-io/jwt/v2 v2.3.0 // indirect
github.com/nats-io/nats-server/v2 v2.9.11 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/nats-io/nats-server/v2 v2.9.15 // indirect
github.com/nats-io/nats-streaming-server v0.25.3 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/onsi/gomega v1.24.2 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/yuin/gopher-lua v1.0.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/automaxprocs v1.5.1 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/exp v0.0.0-20230116083435-1de6713980de // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.1-0.20230105183443-b8be2fde2a9e // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
github.com/yuin/gopher-lua v1.1.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
182 changes: 52 additions & 130 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion redis/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package redis
import (
"context"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
)

type message struct {
Expand Down
55 changes: 47 additions & 8 deletions redis/options.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package redis

import (
"crypto/tls"
"net/url"
"strings"
"time"

"github.com/demdxx/gocast/v2"
"github.com/redis/go-redis/v9"

nc "github.com/geniusrabbit/notificationcenter/v2"
"github.com/geniusrabbit/notificationcenter/v2/encoder"
"github.com/geniusrabbit/notificationcenter/v2/internal/logger"
"github.com/go-redis/redis/v8"
)

type loggerInterface interface {
Expand Down Expand Up @@ -93,14 +96,45 @@ func WithRedisURL(urlString string) Option {
opt.Channels = channels
}
password, _ := u.User.Password()
network := "tcp"
if u.Scheme == "unix" {
network = "unix"
}
opt.RedisOptions = &redis.Options{
Network: "tcp",
Addr: u.Host,
Password: password,
DB: dbNum,
MaxRetries: gocast.Number[int](query.Get("max_retries")),
PoolSize: gocast.Number[int](query.Get("pool_size")),
MinIdleConns: gocast.Number[int](query.Get("min_idle_conns")),
Network: network,
Addr: u.Host,
Password: password,
DB: dbNum,
ClientName: query.Get("client_name"),
MaxRetries: gocast.Number[int](query.Get("max_retries")),
MinRetryBackoff: parseDuration(query.Get("min_retry_backoff")),
MaxRetryBackoff: parseDuration(query.Get("max_retry_backoff")),
DialTimeout: parseDuration(query.Get("dial_timeout")),
ReadTimeout: parseDuration(query.Get("read_timeout")),
WriteTimeout: parseDuration(query.Get("write_timeout")),
PoolFIFO: gocast.Bool(query.Get("pool_fifo")),
PoolSize: gocast.Number[int](query.Get("pool_size")),
PoolTimeout: parseDuration(query.Get("pool_timeout")),
MinIdleConns: gocast.Number[int](query.Get("min_idle_conns")),
MaxIdleConns: gocast.Number[int](query.Get("max_idle_conns")),
}

if query.Has("conn_max_idle_time") {
opt.RedisOptions.ConnMaxIdleTime = parseDuration(query.Get("conn_max_idle_time"))
} else {
opt.RedisOptions.ConnMaxIdleTime = parseDuration(query.Get("idle_timeout"))
}
if query.Has("conn_max_lifetime") {
opt.RedisOptions.ConnMaxLifetime = parseDuration(query.Get("conn_max_lifetime"))
} else {
opt.RedisOptions.ConnMaxLifetime = parseDuration(query.Get("max_conn_age"))
}

if u.Scheme == "rediss" {
opt.RedisOptions.TLSConfig = &tls.Config{
ServerName: u.Hostname(),
MinVersion: tls.VersionTLS12,
}
}
}
}
Expand Down Expand Up @@ -153,3 +187,8 @@ func WithLogger(logger loggerInterface) Option {
opt.Logger = logger
}
}

func parseDuration(s string) time.Duration {
d, _ := time.ParseDuration(s)
return d
}
2 changes: 1 addition & 1 deletion redis/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
nc "github.com/geniusrabbit/notificationcenter/v2"
"github.com/geniusrabbit/notificationcenter/v2/encoder"
"github.com/geniusrabbit/notificationcenter/v2/internal/logger"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion redis/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package redis
import (
"context"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
"go.uber.org/multierr"

nc "github.com/geniusrabbit/notificationcenter/v2"
Expand Down
4 changes: 2 additions & 2 deletions redis/subsub_test.go → redis/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSubSub(t *testing.T) {
func TestPubSub(t *testing.T) {
type tmsg struct{ S string }
var (
rw sync.RWMutex
msg tmsg
mr, err = miniredis.Run()
)
assert.NoError(t, err)
defer mr.Close()
defer func() { mr.Close() }()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand Down
6 changes: 3 additions & 3 deletions redis/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package redis
import (
"context"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"

nc "github.com/geniusrabbit/notificationcenter/v2"
)
Expand Down Expand Up @@ -82,9 +82,9 @@ func (s *Subscriber) message(ctx context.Context, m *redis.Message) {

// Close nstream client
func (s *Subscriber) Close() error {
err := s.cli.Close()
err := s.sub.Close()
if err == nil {
err = s.sub.Close()
err = s.cli.Close()
}
s.closeEvent <- true
return err
Expand Down
13 changes: 9 additions & 4 deletions wrappers/once/redis/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/geniusrabbit/notificationcenter/v2/internal/objecthash"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
)

// Checker provides inmemory messages test
Expand All @@ -19,9 +19,14 @@ func New(client redis.Cmdable, lifetime time.Duration) *Checker {
return &Checker{client: client, lifetime: lifetime}
}

// NewByHost checker
func NewByHost(host string, lifetime time.Duration) *Checker {
return New(redis.NewClient(&redis.Options{Addr: host}), lifetime)
// NewByURL returns checker by redis URL
// redis://[:password]@host:port/db?max_idle=3&max_active=5&idle_timeout=240s
func NewByURL(url string, lifetime time.Duration) (*Checker, error) {
opt, err := redis.ParseURL(url)
if err != nil {
return nil, err
}
return New(redis.NewClient(opt), lifetime), nil
}

// IsSkip message if was sent
Expand Down
15 changes: 4 additions & 11 deletions wrappers/once/redis/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ import (
"time"

"github.com/alicebob/miniredis/v2"
"github.com/elliotchance/redismock/v8"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
)

// newTestRedis returns a redis.Cmdable.
func newTestRedis(mr *miniredis.Miniredis) *redismock.ClientMock {
client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
return redismock.NewNiceMock(client)
}

func TestRedisChecker(t *testing.T) {
ctx := context.TODO()
msg := struct{ s string }{s: `test`}
mr, err := miniredis.Run()
assert.NoError(t, err)
defer mr.Close()
checker := New(newTestRedis(mr), time.Minute)

checker, err := NewByURL("redis://"+mr.Addr(), time.Minute)
assert.NoError(t, err)

assert.False(t, checker.IsSkip(ctx, msg))
assert.NoError(t, checker.MarkAsSent(ctx, msg))
assert.True(t, checker.IsSkip(ctx, msg))
Expand Down

0 comments on commit f121325

Please sign in to comment.