@@ -2,6 +2,7 @@ package endpoint
22
33import (
44 "fmt"
5+ "sync"
56 "time"
67
78 "github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint/info"
@@ -10,6 +11,7 @@ import (
1011type Endpoint interface {
1112 Info () info.Info
1213 String () string
14+ Copy () Endpoint
1315
1416 NodeID () uint32
1517 Address () string
@@ -22,6 +24,7 @@ type Endpoint interface {
2224}
2325
2426type endpoint struct {
27+ mu sync.RWMutex
2528 id uint32
2629 address string
2730 location string
@@ -33,7 +36,23 @@ type endpoint struct {
3336 lastUpdated time.Time
3437}
3538
39+ func (e * endpoint ) Copy () Endpoint {
40+ e .mu .RLock ()
41+ defer e .mu .RUnlock ()
42+ return & endpoint {
43+ id : e .id ,
44+ address : e .address ,
45+ location : e .location ,
46+ services : append (make ([]string , 0 , len (e .services )), e .services ... ),
47+ loadFactor : e .loadFactor ,
48+ local : e .local ,
49+ lastUpdated : e .lastUpdated ,
50+ }
51+ }
52+
3653func (e * endpoint ) String () string {
54+ e .mu .RLock ()
55+ defer e .mu .RUnlock ()
3756 return fmt .Sprintf (`{id:%d,address:"%s",local:%t,location:"%s",loadFactor:%f,lastUpdated:"%s"}` ,
3857 e .id ,
3958 e .address ,
@@ -45,6 +64,8 @@ func (e *endpoint) String() string {
4564}
4665
4766func (e * endpoint ) Info () info.Info {
67+ e .mu .RLock ()
68+ defer e .mu .RUnlock ()
4869 return info.Info {
4970 ID : e .id ,
5071 Address : e .address ,
@@ -54,30 +75,44 @@ func (e *endpoint) Info() info.Info {
5475}
5576
5677func (e * endpoint ) NodeID () uint32 {
78+ e .mu .RLock ()
79+ defer e .mu .RUnlock ()
5780 return e .id
5881}
5982
6083func (e * endpoint ) Address () (address string ) {
84+ e .mu .RLock ()
85+ defer e .mu .RUnlock ()
6186 return e .address
6287}
6388
6489func (e * endpoint ) Location () string {
90+ e .mu .RLock ()
91+ defer e .mu .RUnlock ()
6592 return e .location
6693}
6794
6895func (e * endpoint ) LocalDC () bool {
96+ e .mu .RLock ()
97+ defer e .mu .RUnlock ()
6998 return e .local
7099}
71100
72101func (e * endpoint ) LoadFactor () float32 {
102+ e .mu .RLock ()
103+ defer e .mu .RUnlock ()
73104 return e .loadFactor
74105}
75106
76107func (e * endpoint ) LastUpdated () time.Time {
108+ e .mu .RLock ()
109+ defer e .mu .RUnlock ()
77110 return e .lastUpdated
78111}
79112
80113func (e * endpoint ) Touch (opts ... option ) {
114+ e .mu .Lock ()
115+ defer e .mu .Unlock ()
81116 e .lastUpdated = time .Now ()
82117 for _ , o := range opts {
83118 o (e )
@@ -88,36 +123,48 @@ type option func(e *endpoint)
88123
89124func WithID (id uint32 ) option {
90125 return func (e * endpoint ) {
126+ e .mu .Lock ()
127+ defer e .mu .Unlock ()
91128 e .id = id
92129 }
93130}
94131
95132func WithLocation (location string ) option {
96133 return func (e * endpoint ) {
134+ e .mu .Lock ()
135+ defer e .mu .Unlock ()
97136 e .location = location
98137 }
99138}
100139
101140func WithLocalDC (local bool ) option {
102141 return func (e * endpoint ) {
142+ e .mu .Lock ()
143+ defer e .mu .Unlock ()
103144 e .local = local
104145 }
105146}
106147
107148func WithLoadFactor (loadFactor float32 ) option {
108149 return func (e * endpoint ) {
150+ e .mu .Lock ()
151+ defer e .mu .Unlock ()
109152 e .loadFactor = loadFactor
110153 }
111154}
112155
113156func WithServices (services []string ) option {
114157 return func (e * endpoint ) {
158+ e .mu .Lock ()
159+ defer e .mu .Unlock ()
115160 e .services = append (e .services , services ... )
116161 }
117162}
118163
119164func WithLastUpdated (ts time.Time ) option {
120165 return func (e * endpoint ) {
166+ e .mu .Lock ()
167+ defer e .mu .Unlock ()
121168 e .lastUpdated = ts
122169 }
123170}
0 commit comments