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

NewRedisLimitCounter: no error to return #21

Merged
merged 3 commits into from
Oct 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
// Expose Prometheus endpoint at /metrics path.
r.Use(telemetry.Collector(telemetry.Config{AllowAny: true}))

rc, _ := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
rc := httprateredis.NewCounter(&httprateredis.Config{
Host: "127.0.0.1", Port: 6379,
})

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/go-chi/httprate-redis
go 1.19

require (
github.com/alicebob/miniredis/v2 v2.33.0
github.com/go-chi/httprate v0.12.0
github.com/redis/go-redis/v9 v9.6.1
golang.org/x/sync v0.7.0
)

require (
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/alicebob/miniredis/v2 v2.33.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-chi/httprate v0.12.0 h1:08D/te3pOTJe5+VAZTQrHxwdsH2NyliiUoRD1naKaMg=
github.com/go-chi/httprate v0.12.0/go.mod h1:TUepLXaz/pCjmCtf/obgOQJ2Sz6rC8fSf5cAt5cnTt0=
github.com/redis/go-redis/v9 v9.6.0 h1:NLck+Rab3AOTHw21CGRpvQpgTrAU4sgdCswqGtlhGRA=
github.com/redis/go-redis/v9 v9.6.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
Expand Down
13 changes: 10 additions & 3 deletions httprateredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ func WithRedisLimitCounter(cfg *Config) httprate.Option {
if cfg.Disabled {
return httprate.WithNoop()
}
rc, _ := NewRedisLimitCounter(cfg)
return httprate.WithLimitCounter(rc)
return httprate.WithLimitCounter(NewCounter(cfg))
}

func NewRedisLimitCounter(cfg *Config) (*redisCounter, error) {
c := NewCounter(cfg)
if err := c.client.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("ping failed: %w", err)
}
return c, nil
}

func NewCounter(cfg *Config) *redisCounter {
if cfg == nil {
cfg = &Config{}
}
Expand Down Expand Up @@ -90,7 +97,7 @@ func NewRedisLimitCounter(cfg *Config) (*redisCounter, error) {
})
}

return rc, nil
return rc
}

type redisCounter struct {
Expand Down
10 changes: 2 additions & 8 deletions httprateredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestRedisCounter(t *testing.T) {
limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
limitCounter := httprateredis.NewCounter(&httprateredis.Config{
Host: "localhost",
Port: 6379,
MaxIdle: 0,
Expand All @@ -23,9 +23,6 @@ func TestRedisCounter(t *testing.T) {
FallbackTimeout: time.Second,
FallbackDisabled: true,
})
if err != nil {
t.Fatalf("redis not available: %v", err)
}
defer limitCounter.Close()

limitCounter.Config(1000, time.Minute)
Expand Down Expand Up @@ -156,7 +153,7 @@ func TestRedisCounter(t *testing.T) {
}

func BenchmarkLocalCounter(b *testing.B) {
limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
limitCounter := httprateredis.NewCounter(&httprateredis.Config{
Host: "localhost",
Port: 6379,
DBIndex: 0,
Expand All @@ -167,9 +164,6 @@ func BenchmarkLocalCounter(b *testing.B) {
FallbackDisabled: true,
FallbackTimeout: 5 * time.Second,
})
if err != nil {
b.Fatalf("redis not available: %v", err)
}
defer limitCounter.Close()

limitCounter.Config(1000, time.Minute)
Expand Down
8 changes: 4 additions & 4 deletions local_fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import (
// Test local in-memory counter fallback, which gets activated in case Redis is not available.
func TestLocalFallback(t *testing.T) {
redis, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
redisPort, _ := strconv.Atoi(redis.Port())

var onErrorCalled bool
var onFallbackCalled bool

limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
limitCounter := httprateredis.NewCounter(&httprateredis.Config{
Host: redis.Host(),
Port: uint16(redisPort),
MaxIdle: 0,
Expand All @@ -30,9 +33,6 @@ func TestLocalFallback(t *testing.T) {
OnError: func(err error) { onErrorCalled = true },
OnFallbackChange: func(fallbackActivated bool) { onFallbackCalled = true },
})
if err != nil {
t.Fatalf("redis not available: %v", err)
}

limitCounter.Config(1000, time.Minute)

Expand Down
Loading