|
1 | 1 | package endpoint |
2 | 2 |
|
3 | | -type Endpoint struct { |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint/info" |
| 8 | +) |
| 9 | + |
| 10 | +type Endpoint interface { |
| 11 | + Info() info.Info |
| 12 | + String() string |
| 13 | + |
| 14 | + NodeID() uint32 |
| 15 | + Address() string |
| 16 | + Location() string |
| 17 | + LocalDC() bool |
| 18 | + LoadFactor() float32 |
| 19 | + LastUpdated() time.Time |
| 20 | + |
| 21 | + Touch(opts ...option) |
| 22 | +} |
| 23 | + |
| 24 | +type endpoint struct { |
4 | 25 | id uint32 |
5 | 26 | address string |
6 | 27 | location string |
7 | 28 | services []string |
8 | 29 |
|
9 | 30 | loadFactor float32 |
10 | 31 | local bool |
| 32 | + |
| 33 | + lastUpdated time.Time |
| 34 | +} |
| 35 | + |
| 36 | +func (e *endpoint) String() string { |
| 37 | + return fmt.Sprintf(`{id:%d,address:"%s",local:%t,location:"%s",loadFactor:%f,lastUpdated:"%s"}`, |
| 38 | + e.id, |
| 39 | + e.address, |
| 40 | + e.local, |
| 41 | + e.location, |
| 42 | + e.loadFactor, |
| 43 | + e.lastUpdated.Format(time.RFC3339), |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +func (e *endpoint) Info() info.Info { |
| 48 | + return info.Info{ |
| 49 | + ID: e.id, |
| 50 | + Address: e.address, |
| 51 | + LoadFactor: e.loadFactor, |
| 52 | + Local: e.local, |
| 53 | + } |
11 | 54 | } |
12 | 55 |
|
13 | | -func (e Endpoint) NodeID() uint32 { |
| 56 | +func (e *endpoint) NodeID() uint32 { |
14 | 57 | return e.id |
15 | 58 | } |
16 | 59 |
|
17 | | -func (e Endpoint) Address() (address string) { |
| 60 | +func (e *endpoint) Address() (address string) { |
18 | 61 | return e.address |
19 | 62 | } |
20 | 63 |
|
21 | | -func (e Endpoint) Location() string { |
| 64 | +func (e *endpoint) Location() string { |
22 | 65 | return e.location |
23 | 66 | } |
24 | 67 |
|
25 | | -func (e Endpoint) LocalDC() bool { |
| 68 | +func (e *endpoint) LocalDC() bool { |
26 | 69 | return e.local |
27 | 70 | } |
28 | 71 |
|
29 | | -func (e Endpoint) LoadFactor() float32 { |
| 72 | +func (e *endpoint) LoadFactor() float32 { |
30 | 73 | return e.loadFactor |
31 | 74 | } |
32 | 75 |
|
33 | | -type option func(e *Endpoint) |
| 76 | +func (e *endpoint) LastUpdated() time.Time { |
| 77 | + return e.lastUpdated |
| 78 | +} |
| 79 | + |
| 80 | +func (e *endpoint) Touch(opts ...option) { |
| 81 | + e.lastUpdated = time.Now() |
| 82 | + for _, o := range opts { |
| 83 | + o(e) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +type option func(e *endpoint) |
34 | 88 |
|
35 | 89 | func WithID(id uint32) option { |
36 | | - return func(e *Endpoint) { |
| 90 | + return func(e *endpoint) { |
37 | 91 | e.id = id |
38 | 92 | } |
39 | 93 | } |
40 | 94 |
|
41 | 95 | func WithLocation(location string) option { |
42 | | - return func(e *Endpoint) { |
| 96 | + return func(e *endpoint) { |
43 | 97 | e.location = location |
44 | 98 | } |
45 | 99 | } |
46 | 100 |
|
47 | 101 | func WithLocalDC(local bool) option { |
48 | | - return func(e *Endpoint) { |
| 102 | + return func(e *endpoint) { |
49 | 103 | e.local = local |
50 | 104 | } |
51 | 105 | } |
52 | 106 |
|
53 | 107 | func WithLoadFactor(loadFactor float32) option { |
54 | | - return func(e *Endpoint) { |
| 108 | + return func(e *endpoint) { |
55 | 109 | e.loadFactor = loadFactor |
56 | 110 | } |
57 | 111 | } |
58 | 112 |
|
59 | 113 | func WithServices(services []string) option { |
60 | | - return func(e *Endpoint) { |
| 114 | + return func(e *endpoint) { |
61 | 115 | e.services = append(e.services, services...) |
62 | 116 | } |
63 | 117 | } |
64 | 118 |
|
65 | | -func New(address string, opts ...option) (e Endpoint) { |
66 | | - e.address = address |
| 119 | +func WithLastUpdated(ts time.Time) option { |
| 120 | + return func(e *endpoint) { |
| 121 | + e.lastUpdated = ts |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func New(address string, opts ...option) Endpoint { |
| 126 | + e := &endpoint{ |
| 127 | + address: address, |
| 128 | + lastUpdated: time.Now(), |
| 129 | + } |
67 | 130 | for _, o := range opts { |
68 | | - o(&e) |
| 131 | + o(e) |
69 | 132 | } |
70 | | - return |
| 133 | + return e |
71 | 134 | } |
0 commit comments