Created
January 28, 2020 13:10
-
-
Save nonsense/f773565a3158a1b1618186b8f9c43580 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"sync" | |
"time" | |
"github.com/go-redis/redis/v7" | |
) | |
var workers int | |
func init() { | |
flag.IntVar(&workers, "workers", 10, "number of workers") | |
} | |
func main() { | |
flag.Parse() | |
var wg sync.WaitGroup | |
for i := 1; i <= workers; i++ { | |
i := i | |
wg.Add(1) | |
go func(j int) { | |
defer wg.Done() | |
opts := &redis.Options{ | |
Addr: "redis-master:6379", | |
MaxRetries: 5, | |
MinRetryBackoff: 1 * time.Second, | |
MaxRetryBackoff: 3 * time.Second, | |
DialTimeout: 10 * time.Second, | |
ReadTimeout: 10 * time.Second, | |
} | |
client := redis.NewClient(opts) | |
pong, err := client.Ping().Result() | |
if err != nil { | |
fmt.Println("worker ", j, "ERROR: ", err.Error()) | |
return | |
} | |
fmt.Println("worker ", j, "pong: ", pong) | |
key := fmt.Sprintf("key_%d", j) | |
err = client.Set(key, key, 0).Err() | |
if err != nil { | |
fmt.Println("worker ", j, "ERROR: ", err.Error()) | |
return | |
} | |
val, err := client.Get(key).Result() | |
if err != nil { | |
fmt.Println("worker ", j, "ERROR: ", err.Error()) | |
return | |
} | |
fmt.Println("worker ", j, "key: ", val) | |
key2 := fmt.Sprintf("key2_%d", j) | |
val2, err := client.Get(key2).Result() | |
if err == redis.Nil { | |
fmt.Println("worker ", j, ": key2 does not exist") | |
} else if err != nil { | |
fmt.Println("worker ", j, "ERROR: ", err.Error()) | |
return | |
} else { | |
fmt.Println("worker ", j, ": ", val2) | |
} | |
}(i) | |
} | |
wg.Wait() | |
fmt.Println("finish") | |
time.Sleep(3600 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment