Skip to content

Commit e6add27

Browse files
Merge branch 'master' of github.com:carner/go-redis into expirenx
2 parents eced52f + 5dd7e00 commit e6add27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+637
-287
lines changed

.github/workflows/doctests.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Documentation Tests
2+
3+
on:
4+
push:
5+
branches: [master, examples]
6+
pull_request:
7+
branches: [master, examples]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
doctests:
14+
name: doctests
15+
runs-on: ubuntu-latest
16+
17+
services:
18+
redis-stack:
19+
image: redis/redis-stack-server:latest
20+
options: >-
21+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
22+
ports:
23+
- 6379:6379
24+
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
go-version: [ "1.18", "1.19", "1.20" ]
29+
30+
steps:
31+
- name: Set up ${{ matrix.go-version }}
32+
uses: actions/setup-go@v4
33+
with:
34+
go-version: ${{ matrix.go-version }}
35+
36+
- name: Checkout code
37+
uses: actions/checkout@v3
38+
39+
- name: Test doc examples
40+
working-directory: ./doctests
41+
run: go test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.rdb
22
testdata/*
33
.idea/
4+
.DS_Store

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## [9.0.5](https://github.com/redis/go-redis/compare/v9.0.4...v9.0.5) (2023-05-29)
2+
3+
4+
### Features
5+
6+
* Add ACL LOG ([#2536](https://github.com/redis/go-redis/issues/2536)) ([31ba855](https://github.com/redis/go-redis/commit/31ba855ddebc38fbcc69a75d9d4fb769417cf602))
7+
* add field protocol to setupClusterQueryParams ([#2600](https://github.com/redis/go-redis/issues/2600)) ([840c25c](https://github.com/redis/go-redis/commit/840c25cb6f320501886a82a5e75f47b491e46fbe))
8+
* add protocol option ([#2598](https://github.com/redis/go-redis/issues/2598)) ([3917988](https://github.com/redis/go-redis/commit/391798880cfb915c4660f6c3ba63e0c1a459e2af))
9+
10+
11+
112
## [9.0.4](https://github.com/redis/go-redis/compare/v9.0.3...v9.0.4) (2023-05-01)
213

314

cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type ClusterOptions struct {
6262

6363
OnConnect func(ctx context.Context, cn *Conn) error
6464

65+
Protocol int
6566
Username string
6667
Password string
6768

@@ -216,6 +217,7 @@ func setupClusterConn(u *url.URL, host string, o *ClusterOptions) (*ClusterOptio
216217
func setupClusterQueryParams(u *url.URL, o *ClusterOptions) (*ClusterOptions, error) {
217218
q := queryOptions{q: u.Query()}
218219

220+
o.Protocol = q.int("protocol")
219221
o.ClientName = q.string("client_name")
220222
o.MaxRedirects = q.int("max_redirects")
221223
o.ReadOnly = q.bool("read_only")
@@ -263,6 +265,7 @@ func (opt *ClusterOptions) clientOptions() *Options {
263265
Dialer: opt.Dialer,
264266
OnConnect: opt.OnConnect,
265267

268+
Protocol: opt.Protocol,
266269
Username: opt.Username,
267270
Password: opt.Password,
268271

cluster_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,35 @@ var _ = Describe("ClusterClient", func() {
583583
})
584584
}
585585

586+
Describe("ClusterClient PROTO 2", func() {
587+
BeforeEach(func() {
588+
opt = redisClusterOptions()
589+
opt.Protocol = 2
590+
client = cluster.newClusterClient(ctx, opt)
591+
592+
err := client.ForEachMaster(ctx, func(ctx context.Context, master *redis.Client) error {
593+
return master.FlushDB(ctx).Err()
594+
})
595+
Expect(err).NotTo(HaveOccurred())
596+
})
597+
598+
AfterEach(func() {
599+
_ = client.ForEachMaster(ctx, func(ctx context.Context, master *redis.Client) error {
600+
return master.FlushDB(ctx).Err()
601+
})
602+
Expect(client.Close()).NotTo(HaveOccurred())
603+
})
604+
605+
It("should CLUSTER PROTO 2", func() {
606+
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
607+
val, err := c.Do(ctx, "HELLO").Result()
608+
Expect(err).NotTo(HaveOccurred())
609+
Expect(val).Should(ContainElements("proto", int64(2)))
610+
return nil
611+
})
612+
})
613+
})
614+
586615
Describe("ClusterClient", func() {
587616
BeforeEach(func() {
588617
opt = redisClusterOptions()
@@ -746,6 +775,15 @@ var _ = Describe("ClusterClient", func() {
746775
})
747776
})
748777

778+
It("should CLUSTER PROTO 3", func() {
779+
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
780+
val, err := c.Do(ctx, "HELLO").Result()
781+
Expect(err).NotTo(HaveOccurred())
782+
Expect(val).Should(HaveKeyWithValue("proto", int64(3)))
783+
return nil
784+
})
785+
})
786+
749787
It("should CLUSTER MYSHARDID", func() {
750788
shardID, err := client.ClusterMyShardID(ctx).Result()
751789
Expect(err).NotTo(HaveOccurred())
@@ -1481,6 +1519,10 @@ var _ = Describe("ClusterClient ParseURL", func() {
14811519
test: "UseDefault",
14821520
url: "redis://localhost:123?conn_max_idle_time=",
14831521
o: &redis.ClusterOptions{Addrs: []string{"localhost:123"}, ConnMaxIdleTime: 0},
1522+
}, {
1523+
test: "Protocol",
1524+
url: "redis://localhost:123?protocol=2",
1525+
o: &redis.ClusterOptions{Addrs: []string{"localhost:123"}, Protocol: 2},
14841526
}, {
14851527
test: "ClientName",
14861528
url: "redis://localhost:123?client_name=cluster_hi",

command.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ func (cmd *Cmd) Bool() (bool, error) {
340340

341341
func toBool(val interface{}) (bool, error) {
342342
switch val := val.(type) {
343+
case bool:
344+
return val, nil
343345
case int64:
344346
return val != 0, nil
345347
case string:
@@ -2373,7 +2375,7 @@ func readStreamGroups(rd *proto.Reader) ([]XInfoStreamGroup, error) {
23732375
}
23742376
case "entries-read":
23752377
group.EntriesRead, err = rd.ReadInt()
2376-
if err != nil {
2378+
if err != nil && err != Nil {
23772379
return nil, err
23782380
}
23792381
case "lag":

commands_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6331,6 +6331,97 @@ var _ = Describe("Commands", func() {
63316331
}
63326332
}
63336333
}
6334+
6335+
Expect(res.Groups).To(Equal([]redis.XInfoStreamGroup{
6336+
{
6337+
Name: "group1",
6338+
LastDeliveredID: "3-0",
6339+
EntriesRead: 3,
6340+
Lag: 0,
6341+
PelCount: 3,
6342+
Pending: []redis.XInfoStreamGroupPending{
6343+
{ID: "1-0", Consumer: "consumer1", DeliveryTime: time.Time{}, DeliveryCount: 1},
6344+
{ID: "2-0", Consumer: "consumer1", DeliveryTime: time.Time{}, DeliveryCount: 1},
6345+
},
6346+
Consumers: []redis.XInfoStreamConsumer{
6347+
{
6348+
Name: "consumer1",
6349+
SeenTime: time.Time{},
6350+
ActiveTime: time.Time{},
6351+
PelCount: 2,
6352+
Pending: []redis.XInfoStreamConsumerPending{
6353+
{ID: "1-0", DeliveryTime: time.Time{}, DeliveryCount: 1},
6354+
{ID: "2-0", DeliveryTime: time.Time{}, DeliveryCount: 1},
6355+
},
6356+
},
6357+
{
6358+
Name: "consumer2",
6359+
SeenTime: time.Time{},
6360+
ActiveTime: time.Time{},
6361+
PelCount: 1,
6362+
Pending: []redis.XInfoStreamConsumerPending{
6363+
{ID: "3-0", DeliveryTime: time.Time{}, DeliveryCount: 1},
6364+
},
6365+
},
6366+
},
6367+
},
6368+
{
6369+
Name: "group2",
6370+
LastDeliveredID: "3-0",
6371+
EntriesRead: 3,
6372+
Lag: 0,
6373+
PelCount: 2,
6374+
Pending: []redis.XInfoStreamGroupPending{
6375+
{ID: "2-0", Consumer: "consumer1", DeliveryTime: time.Time{}, DeliveryCount: 1},
6376+
{ID: "3-0", Consumer: "consumer1", DeliveryTime: time.Time{}, DeliveryCount: 1},
6377+
},
6378+
Consumers: []redis.XInfoStreamConsumer{
6379+
{
6380+
Name: "consumer1",
6381+
SeenTime: time.Time{},
6382+
ActiveTime: time.Time{},
6383+
PelCount: 2,
6384+
Pending: []redis.XInfoStreamConsumerPending{
6385+
{ID: "2-0", DeliveryTime: time.Time{}, DeliveryCount: 1},
6386+
{ID: "3-0", DeliveryTime: time.Time{}, DeliveryCount: 1},
6387+
},
6388+
},
6389+
},
6390+
},
6391+
}))
6392+
6393+
// entries-read = nil
6394+
Expect(client.Del(ctx, "xinfo-stream-full-stream").Err()).NotTo(HaveOccurred())
6395+
id, err := client.XAdd(ctx, &redis.XAddArgs{
6396+
Stream: "xinfo-stream-full-stream",
6397+
ID: "*",
6398+
Values: []any{"k1", "v1"},
6399+
}).Result()
6400+
Expect(err).NotTo(HaveOccurred())
6401+
Expect(client.XGroupCreateMkStream(ctx, "xinfo-stream-full-stream", "xinfo-stream-full-group", "0").Err()).NotTo(HaveOccurred())
6402+
res, err = client.XInfoStreamFull(ctx, "xinfo-stream-full-stream", 0).Result()
6403+
Expect(err).NotTo(HaveOccurred())
6404+
Expect(res).To(Equal(&redis.XInfoStreamFull{
6405+
Length: 1,
6406+
RadixTreeKeys: 1,
6407+
RadixTreeNodes: 2,
6408+
LastGeneratedID: id,
6409+
MaxDeletedEntryID: "0-0",
6410+
EntriesAdded: 1,
6411+
Entries: []redis.XMessage{{ID: id, Values: map[string]any{"k1": "v1"}}},
6412+
Groups: []redis.XInfoStreamGroup{
6413+
{
6414+
Name: "xinfo-stream-full-group",
6415+
LastDeliveredID: "0-0",
6416+
EntriesRead: 0,
6417+
Lag: 1,
6418+
PelCount: 0,
6419+
Pending: []redis.XInfoStreamGroupPending{},
6420+
Consumers: []redis.XInfoStreamConsumer{},
6421+
},
6422+
},
6423+
RecordedFirstEntryID: id,
6424+
}))
63346425
})
63356426

63366427
It("should XINFO GROUPS", func() {

doctests/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Command examples for redis.io
2+
3+
These examples appear on the [Redis documentation](https://redis.io) site as part of the tabbed examples interface.
4+
5+
## How to add examples
6+
7+
- Create a Go test file with a meaningful name in the current folder.
8+
- Create a single method prefixed with `Example` and write your test in it.
9+
- Determine the id for the example you're creating and add it as the first line of the file: `// EXAMPLE: set_and_get`.
10+
- We're using the [Testable Examples](https://go.dev/blog/examples) feature of Go to test the desired output has been written to stdout.
11+
12+
### Special markup
13+
14+
See https://github.com/redis-stack/redis-stack-website#readme for more details.
15+
16+
## How to test the examples
17+
18+
- Start a Redis server locally on port 6379
19+
- CD into the `doctests` directory
20+
- Run `go test` to test all examples in the directory.
21+
- Run `go test filename.go` to test a single file
22+

doctests/lpush_lrange_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// EXAMPLE: lpush_and_lrange
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
func ExampleLPushLRange() {
12+
ctx := context.Background()
13+
14+
rdb := redis.NewClient(&redis.Options{
15+
Addr: "localhost:6379",
16+
Password: "", // no password docs
17+
DB: 0, // use default DB
18+
})
19+
20+
// HIDE_END
21+
22+
// REMOVE_START
23+
errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
24+
if errFlush != nil {
25+
panic(errFlush)
26+
}
27+
// REMOVE_END
28+
29+
listSize, err := rdb.LPush(ctx, "my_bikes", "bike:1", "bike:2").Result()
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
fmt.Println(listSize)
35+
36+
value, err := rdb.LRange(ctx, "my_bikes", 0, -1).Result()
37+
if err != nil {
38+
panic(err)
39+
}
40+
fmt.Println(value)
41+
// HIDE_START
42+
43+
// Output: 2
44+
// [bike:2 bike:1]
45+
}
46+
47+
// HIDE_END

doctests/set_get_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// EXAMPLE: set_and_get
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
func ExampleSetGet() {
12+
ctx := context.Background()
13+
14+
rdb := redis.NewClient(&redis.Options{
15+
Addr: "localhost:6379",
16+
Password: "", // no password docs
17+
DB: 0, // use default DB
18+
})
19+
20+
// HIDE_END
21+
22+
// REMOVE_START
23+
errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
24+
if errFlush != nil {
25+
panic(errFlush)
26+
}
27+
// REMOVE_END
28+
29+
err := rdb.Set(ctx, "bike:1", "Process 134", 0).Err()
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
fmt.Println("OK")
35+
36+
value, err := rdb.Get(ctx, "bike:1").Result()
37+
if err != nil {
38+
panic(err)
39+
}
40+
fmt.Printf("The name of the bike is %s", value)
41+
// HIDE_START
42+
43+
// Output: OK
44+
// The name of the bike is Process 134
45+
}
46+
47+
// HIDE_END

example/del-keys-without-ttl/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
replace github.com/redis/go-redis/v9 => ../..
66

77
require (
8-
github.com/redis/go-redis/v9 v9.0.4
8+
github.com/redis/go-redis/v9 v9.0.5
99
go.uber.org/zap v1.24.0
1010
)
1111

0 commit comments

Comments
 (0)