Last active
September 19, 2021 07:31
-
-
Save amyangfei/23346332933d833fc42bd0c7d9406a67 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"context" | |
"fmt" | |
_ "net/http/pprof" | |
"time" | |
"github.com/pingcap/log" | |
"go.etcd.io/etcd/clientv3" | |
"go.uber.org/zap" | |
"golang.org/x/sync/errgroup" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/backoff" | |
) | |
func watch(ctx context.Context, cli *clientv3.Client, key string) error { | |
errg, ctx := errgroup.WithContext(ctx) | |
lease, err := cli.Grant(ctx, 5) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
_, err = cli.Put(ctx, key, "test value", clientv3.WithLease(lease.ID)) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
_, err = cli.KeepAliveOnce(ctx, lease.ID) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
start := time.Now() | |
errg.Go(func() error { | |
ch := cli.Watch(ctx, key, clientv3.WithFilterPut()) | |
for resp := range ch { | |
for _, event := range resp.Events { | |
log.Info("watch found key deleted", zap.String("key", string(event.Kv.Key)), zap.Duration("duration", time.Since(start))) | |
return nil | |
} | |
} | |
return nil | |
}) | |
return errg.Wait() | |
} | |
func test(ctx context.Context) { | |
cli, err := clientv3.New(clientv3.Config{ | |
Endpoints: []string{"http://127.0.0.1:2379"}, | |
Context: ctx, | |
DialTimeout: 10 * time.Second, | |
DialOptions: []grpc.DialOption{ | |
grpc.WithBlock(), | |
grpc.WithConnectParams(grpc.ConnectParams{ | |
Backoff: backoff.Config{ | |
BaseDelay: time.Second, | |
Multiplier: 1.1, | |
Jitter: 0.1, | |
MaxDelay: 3 * time.Second, | |
}, | |
MinConnectTimeout: 5 * time.Second, | |
}), | |
}, | |
}) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
log.Info("start") | |
errg, ctx := errgroup.WithContext(ctx) | |
for i := 0; i < 20; i++ { | |
i := i | |
key := fmt.Sprintf("/test/lease/%d", i) | |
time.Sleep(time.Millisecond * 50) | |
errg.Go(func() error { | |
return watch(ctx, cli, key) | |
}) | |
} | |
err = errg.Wait() | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
} | |
func main() { | |
test(context.Background()) | |
} |
This file contains 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 ( | |
"context" | |
"fmt" | |
_ "net/http/pprof" | |
"time" | |
"github.com/pingcap/log" | |
"go.etcd.io/etcd/clientv3" | |
"go.uber.org/zap" | |
"golang.org/x/sync/errgroup" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/backoff" | |
) | |
func watch(ctx context.Context, cli *clientv3.Client, key string) error { | |
errg, ctx := errgroup.WithContext(ctx) | |
lease, err := cli.Grant(ctx, 5) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
_, err = cli.Put(ctx, key, "test value", clientv3.WithLease(lease.ID)) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
_, err = cli.KeepAliveOnce(ctx, lease.ID) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
start := time.Now() | |
errg.Go(func() error { | |
ch := cli.Watch(ctx, key, clientv3.WithFilterPut()) | |
for resp := range ch { | |
for _, event := range resp.Events { | |
log.Info("watch found key deleted", zap.String("key", string(event.Kv.Key)), zap.Duration("duration", time.Since(start))) | |
return nil | |
} | |
} | |
return nil | |
}) | |
return errg.Wait() | |
} | |
func test(ctx context.Context) { | |
cli, err := clientv3.New(clientv3.Config{ | |
Endpoints: []string{"http://127.0.0.1:2379"}, | |
Context: ctx, | |
DialTimeout: 10 * time.Second, | |
DialOptions: []grpc.DialOption{ | |
grpc.WithBlock(), | |
grpc.WithConnectParams(grpc.ConnectParams{ | |
Backoff: backoff.Config{ | |
BaseDelay: time.Second, | |
Multiplier: 1.1, | |
Jitter: 0.1, | |
MaxDelay: 3 * time.Second, | |
}, | |
MinConnectTimeout: 5 * time.Second, | |
}), | |
}, | |
}) | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
log.Info("start") | |
errg, ctx := errgroup.WithContext(ctx) | |
for i := 0; i < 4000; i++ { | |
i := i | |
key := fmt.Sprintf("/test/lease/%d", i) | |
errg.Go(func() error { | |
return watch(ctx, cli, key) | |
}) | |
} | |
err = errg.Wait() | |
if err != nil { | |
log.Panic(err.Error()) | |
} | |
} | |
func main() { | |
test(context.Background()) | |
} |
Author
amyangfei
commented
Jan 1, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment