Skip to content

Commit

Permalink
more refactoring and add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mjc-gh committed Feb 3, 2024
1 parent 5186327 commit 2942c92
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 33 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ uniq.Clear() // DEL uniq

## TODO

- Finish remaining code `TODOs`
- More test coverage:
- Better coverage all possible generic types for collections
- Test Redis commands with some sort of test env `ProcessHook`. This
Expand Down
8 changes: 4 additions & 4 deletions connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func SetConfiguration(name, namespace, url string, opts ...RedisOption) error {
return nil
}

func getConnection(name string) (*redis.Client, *string, error) {
func getConnection(name string) (*redis.Client, string, error) {
config, configured := configs[name]
if !configured {
return nil, nil, fmt.Errorf("%s is not a configured configuration", name)
return nil, "", fmt.Errorf("%s is not a configured configuration", name)
}

conn, ok := connections[name]
if ok {
return conn, &config.namespace, nil
return conn, config.namespace, nil
}

conn = redis.NewClient(config.options)
Expand All @@ -51,7 +51,7 @@ func getConnection(name string) (*redis.Client, *string, error) {
conn.AddHook(newCmdLoggingHook(debugLogger))
}

return conn, &config.namespace, nil
return conn, config.namespace, nil
}

type cmdLoggingHook struct {
Expand Down
8 changes: 7 additions & 1 deletion counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ func (c *Counter) Decrement(by int64) (int64, error) {
func (c *Counter) Value() (v int64) {
v, err := c.client.Get(c.ctx, c.key).Int64()
if err != nil && err != redis.Nil {
// TODO debug logging
if debugLogger != nil {
debugLogger.Warn("Counter#Value", err)
}
}

return
}

func (c *Counter) ValueResult() (int64, error) {
return c.client.Get(c.ctx, c.key).Int64()
}

func (c *Counter) Reset() (err error) {
_, err = c.client.Del(c.ctx, c.key).Result()
return
Expand Down
13 changes: 12 additions & 1 deletion counter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kredis

import "time"
import (
"time"
)

func (s *KredisTestSuite) TestCounter() {
c, err := NewCounter("counter")
Expand Down Expand Up @@ -58,3 +60,12 @@ func (s *KredisTestSuite) TestCounterWithDefaultAndExpiry() {

s.Empty(c.Value())
}

func (s *KredisTestSuite) TestCounterWIthBadConnection() {
c, _ := NewCounter("counter", WithConfigName("badconn"))

s.Empty(c.Value())
s.Equal([]string{
"Counter#Value dial tcp [::1]:1234: connect: connection refused",
}, testWarnings)
}
29 changes: 18 additions & 11 deletions cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,33 @@ func NewCycle(key string, values []string, opts ...ProxyOption) (*Cycle, error)
return &Cycle{Proxy: *proxy, values: values}, nil
}

func (c *Cycle) Index() int64 {
return c.value()
func (c *Cycle) Index() (idx int64) {
idx, err := c.client.Get(c.ctx, c.key).Int64()
if err != nil && err != redis.Nil {
if debugLogger != nil {
debugLogger.Warn("Cycle#Index", err)
}
}

return
}

func (c *Cycle) IndexResult() (int64, error) {
return c.client.Get(c.ctx, c.key).Int64()
}

func (c *Cycle) Value() string {
return c.values[c.Index()]
}

func (c *Cycle) Next() (err error) {
value := (c.value() + 1) % int64(len(c.values))
_, err = c.client.Set(c.ctx, c.key, value, c.expiresIn).Result()

return
}

func (c *Cycle) value() (v int64) {
v, err := c.client.Get(c.ctx, c.key).Int64()
idx, err := c.client.Get(c.ctx, c.key).Int64()
if err != nil && err != redis.Nil {
// TODO debug logging
return // GET error
}

value := (idx + 1) % int64(len(c.values))
_, err = c.client.Set(c.ctx, c.key, value, c.expiresIn).Result()

return
}
4 changes: 3 additions & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (f *Flag) SoftMark(opts ...FlagMarkOption) error {
func (f *Flag) IsMarked() bool {
n, err := f.client.Exists(f.ctx, f.key).Result()
if err != nil && err != redis.Nil {
// TODO debug logging
if debugLogger != nil {
debugLogger.Warn("Flag#IsMarked", err)
}
}

return n > 0
Expand Down
12 changes: 12 additions & 0 deletions kredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kredis

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/suite"
Expand All @@ -11,13 +12,24 @@ type KredisTestSuite struct {
suite.Suite
}

type testLogger struct{ stdLogger }

var testWarnings []string

func (tl testLogger) Warn(fnName string, err error) {
testWarnings = append(testWarnings, fmt.Sprintf("%s %s", fnName, err.Error()))
}

func (suite *KredisTestSuite) SetupTest() {
// TODO use a unique namespace for each test (thus potentially enabling
// parallel tests)
SetConfiguration("shared", "ns", "redis://localhost:6379/2")
SetConfiguration("badconn", "", "redis://localhost:1234/0")

EnableDebugLogging()

testWarnings = []string{}
SetDebugLogger(&testLogger{})
}

func (suite *KredisTestSuite) TearDownTest() {
Expand Down
9 changes: 5 additions & 4 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func DisableDebugLogging() {
debugLogger = nil
}

// TODO add a way to configure a user provided value that implements the logging interface
// func SetCommandLogger(userLogger cmdLogging)
func SetDebugLogger(userLogger logging) {
debugLogger = userLogger
}

func (log stdLogger) Info(cmd redis.Cmder, dur time.Duration) {
name, key, args := cmdLogParts(cmd)
Expand All @@ -45,8 +46,8 @@ func (log stdLogger) Info(cmd redis.Cmder, dur time.Duration) {
}
}

func (log stdLogger) Warn(msg string, err error) {

func (log stdLogger) Warn(fnName string, err error) {
fmt.Printf("Kredis error in %s: %s", fnName, err.Error())
}

var cmdColor = color.New(color.FgYellow).SprintFunc()
Expand Down
4 changes: 2 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewProxy(key string, opts ...ProxyOption) (*Proxy, error) {
return nil, err
}

if namespace != nil {
key = fmt.Sprintf("%s:%s", *namespace, key)
if namespace != "" {
key = fmt.Sprintf("%s:%s", namespace, key)
}

return &Proxy{
Expand Down
3 changes: 1 addition & 2 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ func (s *KredisTestSuite) TestNewProxyWithConfigName() {

s.NoError(e)
s.NotNil(p)

// TODO assert namespace and connnection details are right??
s.Equal("key", p.key)
}

func (s *KredisTestSuite) TestNewProxyWithExpiresIn() {
Expand Down
12 changes: 6 additions & 6 deletions slot.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kredis

import (
"fmt"

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

Expand Down Expand Up @@ -93,8 +91,9 @@ func (s *Slot) TakenResult() (t int64, err error) {
func (s *Slot) incr() {
_, err := s.client.Incr(s.ctx, s.key).Result()
if err != nil {
// TODO debug logging
fmt.Println(err)
if debugLogger != nil {
debugLogger.Warn("Slot#Reserve()", err)
}
}

s.RefreshTTL()
Expand All @@ -103,8 +102,9 @@ func (s *Slot) incr() {
func (s *Slot) decr() {
_, err := s.client.Decr(s.ctx, s.key).Result()
if err != nil {
// TODO debug logging
fmt.Println(err)
if debugLogger != nil {
debugLogger.Warn("Slot#Release()", err)
}
}

s.RefreshTTL()
Expand Down

0 comments on commit 2942c92

Please sign in to comment.