/
example.go
53 lines (43 loc) · 1.17 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
"time"
"github.com/go-redis/redis"
)
func main() {
redisAddr := os.Getenv("REDIS_HOST") + ":" + os.Getenv("REDIS_PORT")
fmt.Println("REDIS=", redisAddr)
now := time.Now()
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: "",
DB: 0,
})
client.Ping()
fmt.Println("Connect.Since=", time.Since(now))
now = time.Now()
pubsub := client.Subscribe("mychannel2")
defer pubsub.Close()
fmt.Println("client.Subscribed", "Time:", time.Now().Format("15:04:05.999999"))
for i := 0; i < 3; i++ {
msgi, err := pubsub.ReceiveTimeout(time.Second)
if err != nil {
fmt.Println("ReceiveTimeout", "Time:", time.Now().Format("15:04:05.999999"))
break
}
switch msg := msgi.(type) {
case *redis.Subscription:
fmt.Println("subscribed to", msg.Channel, "Time:", time.Now().Format("15:04:05.999999"))
_, err := client.Publish("mychannel2", "hello").Result()
if err != nil {
panic(err)
}
case *redis.Message:
fmt.Println("received", msg.Payload, "from", msg.Channel, "Time:", time.Now().Format("15:04:05.999999"))
default:
panic("unreached")
}
}
fmt.Println("End.Since=", time.Since(now))
}