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

     1  package endpoint
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  )
     8  
     9  type Info interface {
    10  	NodeID() uint32
    11  	Address() string
    12  	LocalDC() bool
    13  	Location() string
    14  	LastUpdated() time.Time
    15  	LoadFactor() float32
    16  }
    17  
    18  type Endpoint interface {
    19  	Info
    20  
    21  	String() string
    22  	Copy() Endpoint
    23  	Touch(opts ...Option)
    24  }
    25  
    26  type endpoint struct {
    27  	mu       sync.RWMutex
    28  	id       uint32
    29  	address  string
    30  	location string
    31  	services []string
    32  
    33  	loadFactor float32
    34  	local      bool
    35  
    36  	lastUpdated time.Time
    37  }
    38  
    39  func (e *endpoint) Copy() Endpoint {
    40  	e.mu.RLock()
    41  	defer e.mu.RUnlock()
    42  
    43  	return &endpoint{
    44  		id:          e.id,
    45  		address:     e.address,
    46  		location:    e.location,
    47  		services:    append(make([]string, 0, len(e.services)), e.services...),
    48  		loadFactor:  e.loadFactor,
    49  		local:       e.local,
    50  		lastUpdated: e.lastUpdated,
    51  	}
    52  }
    53  
    54  func (e *endpoint) String() string {
    55  	e.mu.RLock()
    56  	defer e.mu.RUnlock()
    57  
    58  	return fmt.Sprintf(`{id:%d,address:%q,local:%t,location:%q,loadFactor:%f,lastUpdated:%q}`,
    59  		e.id,
    60  		e.address,
    61  		e.local,
    62  		e.location,
    63  		e.loadFactor,
    64  		e.lastUpdated.Format(time.RFC3339),
    65  	)
    66  }
    67  
    68  func (e *endpoint) NodeID() uint32 {
    69  	e.mu.RLock()
    70  	defer e.mu.RUnlock()
    71  
    72  	return e.id
    73  }
    74  
    75  func (e *endpoint) Address() (address string) {
    76  	e.mu.RLock()
    77  	defer e.mu.RUnlock()
    78  
    79  	return e.address
    80  }
    81  
    82  func (e *endpoint) Location() string {
    83  	e.mu.RLock()
    84  	defer e.mu.RUnlock()
    85  
    86  	return e.location
    87  }
    88  
    89  func (e *endpoint) LocalDC() bool {
    90  	e.mu.RLock()
    91  	defer e.mu.RUnlock()
    92  
    93  	return e.local
    94  }
    95  
    96  func (e *endpoint) LoadFactor() float32 {
    97  	e.mu.RLock()
    98  	defer e.mu.RUnlock()
    99  
   100  	return e.loadFactor
   101  }
   102  
   103  func (e *endpoint) LastUpdated() time.Time {
   104  	e.mu.RLock()
   105  	defer e.mu.RUnlock()
   106  
   107  	return e.lastUpdated
   108  }
   109  
   110  func (e *endpoint) Touch(opts ...Option) {
   111  	e.mu.Lock()
   112  	defer e.mu.Unlock()
   113  	for _, o := range append(
   114  		[]Option{
   115  			withLastUpdated(time.Now()),
   116  		},
   117  		opts...,
   118  	) {
   119  		o(e)
   120  	}
   121  }
   122  
   123  type Option func(e *endpoint)
   124  
   125  func WithID(id uint32) Option {
   126  	return func(e *endpoint) {
   127  		e.id = id
   128  	}
   129  }
   130  
   131  func WithLocation(location string) Option {
   132  	return func(e *endpoint) {
   133  		e.location = location
   134  	}
   135  }
   136  
   137  func WithLocalDC(local bool) Option {
   138  	return func(e *endpoint) {
   139  		e.local = local
   140  	}
   141  }
   142  
   143  func WithLoadFactor(loadFactor float32) Option {
   144  	return func(e *endpoint) {
   145  		e.loadFactor = loadFactor
   146  	}
   147  }
   148  
   149  func WithServices(services []string) Option {
   150  	return func(e *endpoint) {
   151  		e.services = append(e.services, services...)
   152  	}
   153  }
   154  
   155  func withLastUpdated(ts time.Time) Option {
   156  	return func(e *endpoint) {
   157  		e.lastUpdated = ts
   158  	}
   159  }
   160  
   161  func New(address string, opts ...Option) *endpoint {
   162  	e := &endpoint{
   163  		address:     address,
   164  		lastUpdated: time.Now(),
   165  	}
   166  	for _, o := range opts {
   167  		if o != nil {
   168  			o(e)
   169  		}
   170  	}
   171  
   172  	return e
   173  }