Skip to content

Commit

Permalink
Merge branch 'ab/feature/add-unit-field-bitcount' of https://github.c…
Browse files Browse the repository at this point in the history
…om/SoulPancake/rueidis into ab/feature/add-unit-field-bitcount
  • Loading branch information
SoulPancake committed May 17, 2024
2 parents 083d254 + 878e862 commit 4882a1d
Show file tree
Hide file tree
Showing 27 changed files with 226 additions and 100 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Go Modules Test

on: [push, pull_request]

jobs:
prepare-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- id: set-matrix
run: |
echo "matrix=$(find . -maxdepth 2 -type f -name 'go.mod' | xargs -n 1 dirname | sort -u | { echo "e2e"; cat; } | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
build:
needs: prepare-matrix
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
module: ${{fromJson(needs.prepare-matrix.outputs.matrix)}}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21.0'

- name: Test Module
run: |
module_path=${{ matrix.module }}
if [ "$module_path" == "." ]; then
list=$(go list ./...)
echo "Test Packages: $list"
for n in {1..5}; do
./dockertest.sh -skip 'Integration' $list && break
done
elif [ "$module_path" == "e2e" ]; then
list=$(go list ./...)
echo "Test Packages: $list"
for n in {1..5}; do
./dockertest.sh -run 'Integration' $list && break
done
else
cd $module_path
list=$(go list ./...)
echo "Test Packages: $list"
for n in {1..5}; do
../dockertest.sh $list && break
done
fi
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
78 changes: 39 additions & 39 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func test(t *testing.T, storeFn func() CacheStore) {
t.Fatal("CachePXAT should return a desired pttl")
}

v2, e2 = store.Flight("key", "cmd", time.Millisecond*100, now)
v2, _ = store.Flight("key", "cmd", time.Millisecond*100, now)
if v2.typ != v.typ || v2.string != v.string {
t.Fatal("flights after Update should return updated RedisMessage")
}
Expand Down
8 changes: 4 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ type connrole struct {
replica bool
}

func newClusterClient(opt *ClientOption, connFn connFn) (client *clusterClient, err error) {
client = &clusterClient{
func newClusterClient(opt *ClientOption, connFn connFn) (*clusterClient, error) {
client := &clusterClient{
cmd: cmds.NewBuilder(cmds.InitSlot),
connFn: connFn,
opt: opt,
Expand All @@ -120,11 +120,11 @@ func newClusterClient(opt *ClientOption, connFn connFn) (client *clusterClient,
return cc
}

if err = client.init(); err != nil {
if err := client.init(); err != nil {
return nil, err
}

if err = client.refresh(context.Background()); err != nil {
if err := client.refresh(context.Background()); err != nil {
return client, err
}

Expand Down
25 changes: 21 additions & 4 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2696,10 +2696,7 @@ func TestClusterClient_SendReadOperationToReplicaNodesWriteOperationToPrimaryNod
&ClientOption{
InitAddress: []string{"127.0.0.1:0"},
SendToReplicas: func(cmd Completed) bool {
if cmd.IsReadOnly() {
return true
}
return false
return cmd.IsReadOnly()
},
},
func(dst string, opt *ClientOption) conn {
Expand Down Expand Up @@ -4333,3 +4330,23 @@ func TestClusterShardsParsing(t *testing.T) {
}
})
}

// https://github.com/redis/rueidis/issues/543
func TestConnectToNonAvailableCluster(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
_, err := NewClient(ClientOption{
InitAddress: []string{"127.0.0.1:3000", "127.0.0.1:3001", "127.0.0.1:3002"},
})
if err == nil {
t.Errorf("expected connect error")
}
}
}()
}
wg.Wait()
}
13 changes: 13 additions & 0 deletions dockertest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

set -ev

go vet ./...

go install honnef.co/go/tools/cmd/staticcheck@latest
# disabled checks
# -ST1000 missing package doc in internal packages
# -ST1003 wrong naming convention would require breaking changes
# -ST1012 wrong error name convention in om package would require breaking changes
# -ST1016 violation of methods on the same type should have the same receiver name in rueidishook
# -ST1020 violation of go doc comment on exported methods in rueidiscompat
# -ST1021 violation of go doc comment on exported types in rueidiscompat
# -U1000 unused check in mock package
staticcheck -checks "all,-ST1000,-ST1003,-ST1012,-ST1016,-ST1020,-ST1021,-U1000" ./... | (grep -v "_test.go:" && exit 1 || exit 0)

trap "docker-compose down -v" EXIT
docker-compose up -d
sleep 5
Expand Down
14 changes: 12 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -55,19 +56,28 @@ func (r *RedisError) IsNil() bool {
// IsMoved checks if it is a redis MOVED message and returns moved address.
func (r *RedisError) IsMoved() (addr string, ok bool) {
if ok = strings.HasPrefix(r.string, "MOVED"); ok {
addr = strings.Split(r.string, " ")[2]
addr = fixIPv6HostPort(strings.Split(r.string, " ")[2])
}
return
}

// IsAsk checks if it is a redis ASK message and returns ask address.
func (r *RedisError) IsAsk() (addr string, ok bool) {
if ok = strings.HasPrefix(r.string, "ASK"); ok {
addr = strings.Split(r.string, " ")[2]
addr = fixIPv6HostPort(strings.Split(r.string, " ")[2])
}
return
}

func fixIPv6HostPort(addr string) string {
if strings.IndexByte(addr, '.') < 0 && len(addr) > 0 && addr[0] != '[' { // skip ipv4 and enclosed ipv6
if i := strings.LastIndexByte(addr, ':'); i >= 0 {
return net.JoinHostPort(addr[:i], addr[i+1:])
}
}
return addr
}

// IsTryAgain checks if it is a redis TRYAGAIN message and returns ask address.
func (r *RedisError) IsTryAgain() bool {
return strings.HasPrefix(r.string, "TRYAGAIN")
Expand Down
32 changes: 32 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ func TestIsRedisErr(t *testing.T) {
}
}

func TestRedisErrorIsMoved(t *testing.T) {
for _, c := range []struct {
err string
addr string
}{
{err: "MOVED 1 127.0.0.1:1", addr: "127.0.0.1:1"},
{err: "MOVED 1 [::1]:1", addr: "[::1]:1"},
{err: "MOVED 1 ::1:1", addr: "[::1]:1"},
} {
e := RedisError{typ: '-', string: c.err}
if addr, ok := e.IsMoved(); !ok || addr != c.addr {
t.Fail()
}
}
}

func TestRedisErrorIsAsk(t *testing.T) {
for _, c := range []struct {
err string
addr string
}{
{err: "ASK 1 127.0.0.1:1", addr: "127.0.0.1:1"},
{err: "ASK 1 [::1]:1", addr: "[::1]:1"},
{err: "ASK 1 ::1:1", addr: "[::1]:1"},
} {
e := RedisError{typ: '-', string: c.err}
if addr, ok := e.IsAsk(); !ok || addr != c.addr {
t.Fail()
}
}
}

func TestIsRedisBusyGroup(t *testing.T) {
err := errors.New("other")
if IsRedisBusyGroup(err) {
Expand Down
2 changes: 1 addition & 1 deletion mock/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
replace github.com/redis/rueidis => ../

require (
github.com/redis/rueidis v1.0.35
github.com/redis/rueidis v1.0.37
go.uber.org/mock v0.3.0
)

Expand Down
6 changes: 2 additions & 4 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func TestMuxDelegation(t *testing.T) {
result := make([]RedisResult, len(multi))
for j, cmd := range multi {
if s := cmd.Cmd.Slot() & uint16(len(wires)-1); s != idx {
result[j] = newErrResult(errors.New(fmt.Sprintf("wrong slot %v %v", s, idx)))
result[j] = newErrResult(fmt.Errorf("wrong slot %v %v", s, idx))
} else {
result[j] = newResult(RedisMessage{typ: '+', string: cmd.Cmd.Commands()[1]}, nil)
}
Expand Down Expand Up @@ -497,7 +497,7 @@ func TestMuxDelegation(t *testing.T) {
DoMultiCacheFn: func(multi ...CacheableTTL) *redisresults {
for _, cmd := range multi {
if s := cmd.Cmd.Slot() & uint16(len(wires)-1); s != idx {
return &redisresults{s: []RedisResult{newErrResult(errors.New(fmt.Sprintf("wrong slot %v %v", s, idx)))}}
return &redisresults{s: []RedisResult{newErrResult(fmt.Errorf("wrong slot %v %v", s, idx))}}
}
}
return &redisresults{s: []RedisResult{newErrResult(context.DeadlineExceeded)}}
Expand Down Expand Up @@ -896,7 +896,6 @@ func (m *mockWire) CleanSubscriptions() {
if m.CleanSubscriptionsFn != nil {
m.CleanSubscriptionsFn()
}
return
}

func (m *mockWire) SetPubSubHooks(hooks PubSubHooks) <-chan error {
Expand All @@ -910,7 +909,6 @@ func (m *mockWire) SetOnCloseHook(fn func(error)) {
if m.SetOnCloseHookFn != nil {
m.SetOnCloseHookFn(fn)
}
return
}

func (m *mockWire) Info() map[string]RedisMessage {
Expand Down
2 changes: 1 addition & 1 deletion om/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/redis/rueidis => ../

require (
github.com/oklog/ulid/v2 v2.1.0
github.com/redis/rueidis v1.0.35
github.com/redis/rueidis v1.0.37
)

require golang.org/x/sys v0.19.0 // indirect
7 changes: 3 additions & 4 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

const LibName = "rueidis"
const LibVer = "1.0.35"
const LibVer = "1.0.37"

var noHello = regexp.MustCompile("unknown command .?(HELLO|hello).?")

Expand Down Expand Up @@ -1164,8 +1164,8 @@ func (p *pipe) syncDo(dl time.Time, dlOk bool, cmd Completed) (resp RedisResult)
}

var msg RedisMessage
err := writeCmd(p.w, cmd.Commands())
if err = p.w.Flush(); err == nil {
err := flushCmd(p.w, cmd.Commands())
if err == nil {
msg, err = syncRead(p.r)
}
if err != nil {
Expand Down Expand Up @@ -1225,7 +1225,6 @@ abort:
for i := 0; i < len(resp); i++ {
resp[i] = newErrResult(err)
}
return
}

func syncRead(r *bufio.Reader) (m RedisMessage, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func write(o io.Writer, m RedisMessage) (err error) {
_, err = o.Write([]byte{m.typ})
switch m.typ {
case '$':
_, err = o.Write(append([]byte(strconv.Itoa(len(m.string))), '\r', '\n'))
_, _ = o.Write(append([]byte(strconv.Itoa(len(m.string))), '\r', '\n'))
_, err = o.Write(append([]byte(m.string), '\r', '\n'))
case '+', '-', '_':
_, err = o.Write(append([]byte(m.string), '\r', '\n'))
Expand Down
5 changes: 5 additions & 0 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ func writeCmd(o *bufio.Writer, cmd []string) (err error) {
return err
}

func flushCmd(o *bufio.Writer, cmd []string) (err error) {
_ = writeCmd(o, cmd)
return o.Flush()
}

const (
unexpectedNoCRLF = "received unexpected simple string message ending without CRLF"
unexpectedNumByte = "received unexpected number byte: "
Expand Down
6 changes: 2 additions & 4 deletions resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package rueidis
import (
"bufio"
"bytes"
crand "crypto/rand"
"io"
"math/rand"
"reflect"
"strconv"
"strings"
"testing"
"time"
)

const iteration = 100
Expand All @@ -18,8 +18,6 @@ var generators = map[byte]func(i int64, f float64, str string) string{}

//gocyclo:ignore
func init() {
rand.Seed(time.Now().UnixNano())

generators['$'] = func(i int64, f float64, str string) string {
return strconv.Itoa(len(str)) + "\r\n" + str + "\r\n"
}
Expand Down Expand Up @@ -683,7 +681,7 @@ func source(str string) *bufio.Reader {
func random(trim bool) string {
retry:
bs := make([]byte, randN(5000))
if _, err := rand.Read(bs); err != nil {
if _, err := crand.Read(bs); err != nil {
panic(err)
}
if trim {
Expand Down
2 changes: 1 addition & 1 deletion ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newRing(factor int) *ring {
m := &sync.Mutex{}
r.store[i].c1 = sync.NewCond(m)
r.store[i].c2 = sync.NewCond(m)
r.store[i].ch = make(chan RedisResult, 0) // this channel can't be buffered
r.store[i].ch = make(chan RedisResult) // this channel can't be buffered
}
return r
}
Expand Down
4 changes: 2 additions & 2 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func TestRing(t *testing.T) {

t.Run("PutMulti Wakeup WaitForWrite", func(t *testing.T) {
ring := newRing(DefaultRingScale)
if _, multi, ch := ring.NextWriteCmd(); ch == nil {
if _, _, ch := ring.NextWriteCmd(); ch == nil {
go func() {
time.Sleep(time.Millisecond * 100)
ring.PutMulti([]Completed{cmds.PingCmd}, nil)
}()
if _, multi, ch = ring.WaitForWrite(); ch != nil && multi[0].Commands()[0] == cmds.PingCmd.Commands()[0] {
if _, multi, ch := ring.WaitForWrite(); ch != nil && multi[0].Commands()[0] == cmds.PingCmd.Commands()[0] {
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion rueidisaside/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/redis/rueidis => ../

require (
github.com/oklog/ulid/v2 v2.1.0
github.com/redis/rueidis v1.0.35
github.com/redis/rueidis v1.0.37
)

require golang.org/x/sys v0.19.0 // indirect
Loading

0 comments on commit 4882a1d

Please sign in to comment.