...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/v3client/v3client.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api/v3client

     1  // 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  
    15  package v3client
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"go.etcd.io/etcd/client/v3"
    22  	"go.etcd.io/etcd/server/v3/etcdserver"
    23  	"go.etcd.io/etcd/server/v3/etcdserver/api/v3rpc"
    24  	"go.etcd.io/etcd/server/v3/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.
    30  func New(s *etcdserver.EtcdServer) *clientv3.Client {
    31  	c := clientv3.NewCtxClient(context.Background(), clientv3.WithZapLogger(s.Logger()))
    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  	a := adapter.AuthServerToAuthClient(v3rpc.NewAuthServer(s))
    49  	c.Auth = clientv3.NewAuthFromAuthClient(a, c)
    50  
    51  	return c
    52  }
    53  
    54  // BlankContext implements Stringer on a context so the ctx string doesn't
    55  // depend on the context's WithValue data, which tends to be unsynchronized
    56  // (e.g., x/net/trace), causing ctx.String() to throw data races.
    57  type blankContext struct{ context.Context }
    58  
    59  func (*blankContext) String() string { return "(blankCtx)" }
    60  
    61  // watchWrapper wraps clientv3 watch calls to blank out the context
    62  // to avoid races on trace data.
    63  type watchWrapper struct{ clientv3.Watcher }
    64  
    65  func (ww *watchWrapper) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
    66  	return ww.Watcher.Watch(&blankContext{ctx}, key, opts...)
    67  }
    68  

View as plain text