blob: d2031213cfb3d2dbaf23bb55e971add714314bd3 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2017 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package v3client
16
17import (
18 "context"
19 "time"
20
21 "go.etcd.io/etcd/clientv3"
22 "go.etcd.io/etcd/etcdserver"
23 "go.etcd.io/etcd/etcdserver/api/v3rpc"
24 "go.etcd.io/etcd/proxy/grpcproxy/adapter"
25)
26
27// New creates a clientv3 client that wraps an in-process EtcdServer. Instead
28// of making gRPC calls through sockets, the client makes direct function calls
29// to the etcd server through its api/v3rpc function interfaces.
30func New(s *etcdserver.EtcdServer) *clientv3.Client {
31 c := clientv3.NewCtxClient(context.Background())
32
33 kvc := adapter.KvServerToKvClient(v3rpc.NewQuotaKVServer(s))
34 c.KV = clientv3.NewKVFromKVClient(kvc, c)
35
36 lc := adapter.LeaseServerToLeaseClient(v3rpc.NewQuotaLeaseServer(s))
37 c.Lease = clientv3.NewLeaseFromLeaseClient(lc, c, time.Second)
38
39 wc := adapter.WatchServerToWatchClient(v3rpc.NewWatchServer(s))
40 c.Watcher = &watchWrapper{clientv3.NewWatchFromWatchClient(wc, c)}
41
42 mc := adapter.MaintenanceServerToMaintenanceClient(v3rpc.NewMaintenanceServer(s))
43 c.Maintenance = clientv3.NewMaintenanceFromMaintenanceClient(mc, c)
44
45 clc := adapter.ClusterServerToClusterClient(v3rpc.NewClusterServer(s))
46 c.Cluster = clientv3.NewClusterFromClusterClient(clc, c)
47
48 // TODO: implement clientv3.Auth interface?
49
50 return c
51}
52
53// BlankContext implements Stringer on a context so the ctx string doesn't
54// depend on the context's WithValue data, which tends to be unsynchronized
55// (e.g., x/net/trace), causing ctx.String() to throw data races.
56type blankContext struct{ context.Context }
57
58func (*blankContext) String() string { return "(blankCtx)" }
59
60// watchWrapper wraps clientv3 watch calls to blank out the context
61// to avoid races on trace data.
62type watchWrapper struct{ clientv3.Watcher }
63
64func (ww *watchWrapper) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
65 return ww.Watcher.Watch(&blankContext{ctx}, key, opts...)
66}