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

Function provideEsFactory

otes/dependency.go:62–103  ·  view source on GitHub ↗

Provide creates Factory and *elastic.Client. It is a valid dependency for package core.

(option *providersOption)

Source from the content-addressed store, hash-verified

60// Provide creates Factory and *elastic.Client. It is a valid dependency for
61// package core.
62func provideEsFactory(option *providersOption) func(p factoryIn) (Factory, func()) {
63 if option.interceptor == nil {
64 option.interceptor = func(name string, opt *Config) {}
65 }
66 if option.clientConstructor == nil {
67 option.clientConstructor = newClient
68 }
69 return func(p factoryIn) (Factory, func()) {
70 factory := di.NewFactory(func(name string) (di.Pair, error) {
71 var conf Config
72 if err := p.Conf.Unmarshal(fmt.Sprintf("es.%s", name), &conf); err != nil {
73 if name != "default" {
74 return di.Pair{}, fmt.Errorf("elastic configuration %s not valid: %w", name, err)
75 }
76 conf.URL = []string{"http://127.0.0.1:9200"}
77 }
78
79 option.interceptor(name, &conf)
80
81 client, err := option.clientConstructor(ClientArgs{
82 Name: name,
83 Conf: &conf,
84 Populator: p.Populator,
85 })
86 if err != nil {
87 return di.Pair{}, err
88 }
89
90 return di.Pair{
91 Conn: client,
92 Closer: func() {
93 client.Stop()
94 },
95 }, nil
96 })
97 f := Factory{factory}
98 if option.reloadable {
99 f.SubscribeReloadEventFrom(p.Dispatcher)
100 }
101 return f, f.Close
102 }
103}
104
105// ClientArgs are arguments for constructing elasticsearch clients.
106// Use this as input when providing custom constructor.

Callers 3

TestTracingFunction · 0.85
TestNewEsFactoryFunction · 0.85
ProvidersFunction · 0.85

Calls 4

NewFactoryFunction · 0.92
UnmarshalMethod · 0.65
SprintfMethod · 0.65

Tested by 2

TestTracingFunction · 0.68
TestNewEsFactoryFunction · 0.68