github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/discovery/config/config.go (about)

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