Merge pull request #7816 from heyitsanthony/v3client-blankctx

v3client: wrap watch ctxs with blank ctx
This commit is contained in:
Anthony Romano 2017-04-25 21:53:14 -07:00 committed by GitHub
commit d2e69b339f

View File

@ -15,13 +15,14 @@
package v3client package v3client
import ( import (
"context"
"time" "time"
"github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/etcdserver" "github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/etcdserver/api/v3rpc" "github.com/coreos/etcd/etcdserver/api/v3rpc"
"github.com/coreos/etcd/proxy/grpcproxy/adapter" "github.com/coreos/etcd/proxy/grpcproxy/adapter"
"golang.org/x/net/context"
) )
// New creates a clientv3 client that wraps an in-process EtcdServer. Instead // New creates a clientv3 client that wraps an in-process EtcdServer. Instead
@ -37,7 +38,7 @@ func New(s *etcdserver.EtcdServer) *clientv3.Client {
c.Lease = clientv3.NewLeaseFromLeaseClient(lc, time.Second) c.Lease = clientv3.NewLeaseFromLeaseClient(lc, time.Second)
wc := adapter.WatchServerToWatchClient(v3rpc.NewWatchServer(s)) wc := adapter.WatchServerToWatchClient(v3rpc.NewWatchServer(s))
c.Watcher = clientv3.NewWatchFromWatchClient(wc) c.Watcher = &watchWrapper{clientv3.NewWatchFromWatchClient(wc)}
mc := adapter.MaintenanceServerToMaintenanceClient(v3rpc.NewMaintenanceServer(s)) mc := adapter.MaintenanceServerToMaintenanceClient(v3rpc.NewMaintenanceServer(s))
c.Maintenance = clientv3.NewMaintenanceFromMaintenanceClient(mc) c.Maintenance = clientv3.NewMaintenanceFromMaintenanceClient(mc)
@ -49,3 +50,18 @@ func New(s *etcdserver.EtcdServer) *clientv3.Client {
return c return c
} }
// BlankContext implements Stringer on a context so the ctx string doesn't
// depend on the context's WithValue data, which tends to be unsynchronized
// (e.g., x/net/trace), causing ctx.String() to throw data races.
type blankContext struct{ context.Context }
func (*blankContext) String() string { return "(blankCtx)" }
// watchWrapper wraps clientv3 watch calls to blank out the context
// to avoid races on trace data.
type watchWrapper struct{ clientv3.Watcher }
func (ww *watchWrapper) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
return ww.Watcher.Watch(&blankContext{ctx}, key, opts...)
}