github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/discovery/config/config.go (about) 1 package config 2 3 import ( 4 "time" 5 6 "github.com/jonboulle/clockwork" 7 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/config" 9 "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta" 10 "github.com/ydb-platform/ydb-go-sdk/v3/trace" 11 ) 12 13 const ( 14 DefaultInterval = time.Minute 15 ) 16 17 type Config struct { 18 config.Common 19 20 endpoint string 21 database string 22 secure bool 23 meta *meta.Meta 24 addressMutator func(address string) string 25 clock clockwork.Clock 26 27 interval time.Duration 28 trace *trace.Discovery 29 } 30 31 func New(opts ...Option) *Config { 32 c := &Config{ 33 interval: DefaultInterval, 34 trace: &trace.Discovery{}, 35 addressMutator: func(address string) string { 36 return address 37 }, 38 clock: clockwork.NewRealClock(), 39 } 40 for _, opt := range opts { 41 if opt != nil { 42 opt(c) 43 } 44 } 45 46 return c 47 } 48 49 func (c *Config) MutateAddress(fqdn string) string { 50 return c.addressMutator(fqdn) 51 } 52 53 func (c *Config) Meta() *meta.Meta { 54 return c.meta 55 } 56 57 func (c *Config) Clock() clockwork.Clock { 58 return c.clock 59 } 60 61 func (c *Config) Interval() time.Duration { 62 return c.interval 63 } 64 65 func (c *Config) Endpoint() string { 66 return c.endpoint 67 } 68 69 func (c *Config) Database() string { 70 return c.database 71 } 72 73 func (c *Config) Secure() bool { 74 return c.secure 75 } 76 77 func (c *Config) Trace() *trace.Discovery { 78 return c.trace 79 } 80 81 type Option func(c *Config) 82 83 // With applies common configuration params 84 func With(config config.Common) Option { 85 return func(c *Config) { 86 c.Common = config 87 } 88 } 89 90 // WithEndpoint set a required starting endpoint for connect 91 func WithEndpoint(endpoint string) Option { 92 return func(c *Config) { 93 c.endpoint = endpoint 94 } 95 } 96 97 // WithDatabase set a required database name. 98 func WithDatabase(database string) Option { 99 return func(c *Config) { 100 c.database = database 101 } 102 } 103 104 func WithClock(clock clockwork.Clock) Option { 105 return func(c *Config) { 106 c.clock = clock 107 } 108 } 109 110 func WithAddressMutator(addressMutator func(address string) string) Option { 111 return func(c *Config) { 112 c.addressMutator = addressMutator 113 } 114 } 115 116 // WithSecure set flag for secure connection 117 func WithSecure(ssl bool) Option { 118 return func(c *Config) { 119 c.secure = ssl 120 } 121 } 122 123 // WithMeta is not for user. 124 // 125 // This option add meta information about database connection 126 func WithMeta(meta *meta.Meta) Option { 127 return func(c *Config) { 128 c.meta = meta 129 } 130 } 131 132 // WithTrace configures discovery client calls tracing 133 func WithTrace(trace trace.Discovery, opts ...trace.DiscoveryComposeOption) Option { 134 return func(c *Config) { 135 c.trace = c.trace.Compose(&trace, opts...) 136 } 137 } 138 139 // WithInterval set the frequency of background tasks of ydb endpoints discovery. 140 // 141 // If Interval is zero then the DefaultInterval is used. 142 // 143 // If Interval is negative, then no background discovery prepared. 144 func WithInterval(interval time.Duration) Option { 145 return func(c *Config) { 146 switch { 147 case interval < 0: 148 c.interval = 0 149 case interval == 0: 150 c.interval = DefaultInterval 151 default: 152 c.interval = interval 153 } 154 } 155 }