Skip to content

Instantly share code, notes, and snippets.

@zhaoyao
Created August 16, 2017 06:43
Show Gist options
  • Save zhaoyao/1563a218ea0df666786435a7e821e3f6 to your computer and use it in GitHub Desktop.
Save zhaoyao/1563a218ea0df666786435a7e821e3f6 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"sync"
"google.golang.org/grpc"
"github.com/coreos/etcd/clientv3"
)
func test(c *clientv3.Client, wg *sync.WaitGroup, i int) {
defer wg.Done()
ctx := context.TODO()
role := fmt.Sprintf("test-role-%d", i)
user := fmt.Sprintf("test-user-%d", i)
_, err := c.RoleAdd(ctx, role)
if err != nil {
panic(err)
}
_, err = c.RoleGrantPermission(ctx, role, "", clientv3.GetPrefixRangeEnd(""), clientv3.PermissionType(clientv3.PermReadWrite))
if err != nil {
panic(err)
}
_, err = c.UserAdd(ctx, user, "123")
if err != nil {
panic(err)
}
//for _ = range time.Tick(time.Second) {
key := "test-key"
val := "testval"
_, err = c.Txn(ctx).If(
clientv3.Compare(clientv3.Value(key), "=", val),
).Else(
clientv3.OpPut(key, val),
).Commit()
if err != nil {
panic(err)
}
//}
}
func main() {
c, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
Username: "root",
Password: "root",
DialOptions: []grpc.DialOption{grpc.WithBlock()},
})
if err != nil {
panic(err)
}
concurreny := 10
wg := &sync.WaitGroup{}
wg.Add(concurreny)
for i := 0; i < concurreny; i++ {
go test(c, wg, i)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment