MCPcopy Create free account
hub / github.com/DoNewsCode/core / provideFactory

Function provideFactory

otetcd/dependency.go:83–136  ·  view source on GitHub ↗

provideFactory creates Factory. It is a valid dependency for package core.

(option *providersOption)

Source from the content-addressed store, hash-verified

81// provideFactory creates Factory. It is a valid
82// dependency for package core.
83func provideFactory(option *providersOption) func(p factoryIn) (Factory, func()) {
84 if option.interceptor == nil {
85 option.interceptor = func(name string, options *clientv3.Config) {}
86 }
87
88 return func(p factoryIn) (Factory, func()) {
89 factory := di.NewFactory(func(name string) (di.Pair, error) {
90 var conf Option
91 if err := p.Conf.Unmarshal(fmt.Sprintf("etcd.%s", name), &conf); err != nil {
92 return di.Pair{}, fmt.Errorf("etcd configuration %s not valid: %w", name, err)
93 }
94 if len(conf.Endpoints) == 0 {
95 conf.Endpoints = []string{"127.0.0.1:2379"}
96 }
97 co := clientv3.Config{
98 Endpoints: conf.Endpoints,
99 AutoSyncInterval: duration(conf.AutoSyncInterval),
100 DialTimeout: duration(conf.dialTimeout()),
101 DialKeepAliveTime: duration(conf.DialKeepAliveTime),
102 DialKeepAliveTimeout: duration(conf.DialKeepAliveTimeout),
103 MaxCallSendMsgSize: conf.MaxCallSendMsgSize,
104 MaxCallRecvMsgSize: conf.MaxCallRecvMsgSize,
105 TLS: conf.TLS,
106 Username: conf.Username,
107 Password: conf.Password,
108 RejectOldCluster: conf.RejectOldCluster,
109 DialOptions: conf.DialOptions,
110 Context: conf.Context,
111 LogConfig: conf.LogConfig,
112 PermitWithoutStream: conf.PermitWithoutStream,
113 }
114 if p.Tracer != nil {
115 co.DialOptions = append(
116 co.DialOptions,
117 grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(p.Tracer)),
118 grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(p.Tracer)),
119 )
120 }
121 option.interceptor(name, &co)
122 client, _ := clientv3.New(co)
123 return di.Pair{
124 Conn: client,
125 Closer: func() {
126 _ = client.Close()
127 },
128 }, nil
129 })
130 etcdFactory := Factory{factory}
131 if option.reloadable {
132 etcdFactory.SubscribeReloadEventFrom(p.Dispatcher)
133 }
134 return etcdFactory, etcdFactory.Close
135 }
136}
137
138func provideDefaultClient(maker Maker) (*clientv3.Client, error) {
139 return maker.Make("default")

Callers 3

TestTracingFunction · 0.70
TestProvideFactoryFunction · 0.70
ProvidersFunction · 0.70

Calls 7

dialTimeoutMethod · 0.95
NewFactoryFunction · 0.92
durationFunction · 0.85
CloseMethod · 0.80
UnmarshalMethod · 0.65
SprintfMethod · 0.65

Tested by 2

TestTracingFunction · 0.56
TestProvideFactoryFunction · 0.56