Skip to content

Commit 4fd50ff

Browse files
authored
Merge pull request #146 from ydb-platform/balancers-prefer
balancers.Prefer + balancers.PreferWithFallback
2 parents 0a58572 + e24ca52 commit 4fd50ff

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.12.0
2+
* Added `balancers.Prefer` and `balancers.PreferWithFallback` constructors
3+
14
## 3.11.13
25
* Added `trace.Driver.OnRepeaterWakeUp` event
36
* Refactored package `repeater`

balancers/balancer.go

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,23 @@ func SingleConn() balancer.Balancer {
2525
// PreferLocalDC creates balancer which use endpoints only in location such as initial endpoint location
2626
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location
2727
func PreferLocalDC(balancer balancer.Balancer) balancer.Balancer {
28-
return multi.Balancer(
29-
multi.WithBalancer(
30-
balancer,
31-
func(cc conn.Conn) bool {
32-
return cc.Endpoint().LocalDC()
33-
},
34-
),
28+
return Prefer(
29+
balancer,
30+
func(endpoint Endpoint) bool {
31+
return endpoint.LocalDC()
32+
},
3533
)
3634
}
3735

3836
// PreferLocalDCWithFallBack creates balancer which use endpoints only in location such as initial endpoint location
3937
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location
4038
// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead
41-
func PreferLocalDCWithFallBack(b balancer.Balancer) balancer.Balancer {
42-
return multi.Balancer(
43-
multi.WithBalancer(
44-
b,
45-
func(cc conn.Conn) bool {
46-
return cc.Endpoint().LocalDC()
47-
},
48-
),
49-
multi.WithBalancer(
50-
b.(balancer.Creator).Create(),
51-
func(cc conn.Conn) bool {
52-
return !cc.Endpoint().LocalDC()
53-
},
54-
),
39+
func PreferLocalDCWithFallBack(balancer balancer.Balancer) balancer.Balancer {
40+
return PreferWithFallback(
41+
balancer,
42+
func(endpoint Endpoint) bool {
43+
return endpoint.LocalDC()
44+
},
5545
)
5646
}
5747

@@ -64,48 +54,81 @@ func PreferLocations(balancer balancer.Balancer, locations ...string) balancer.B
6454
for i := range locations {
6555
locations[i] = strings.ToUpper(locations[i])
6656
}
67-
return multi.Balancer(
68-
multi.WithBalancer(balancer, func(cc conn.Conn) bool {
69-
location := strings.ToUpper(cc.Endpoint().Location())
57+
return Prefer(
58+
balancer,
59+
func(endpoint Endpoint) bool {
60+
location := strings.ToUpper(endpoint.Location())
7061
for _, l := range locations {
7162
if location == l {
7263
return true
7364
}
7465
}
7566
return false
76-
}),
67+
},
7768
)
7869
}
7970

8071
// PreferLocationsWithFallback creates balancer which use endpoints only in selected locations
8172
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location
8273
// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead
83-
func PreferLocationsWithFallback(b balancer.Balancer, locations ...string) balancer.Balancer {
74+
func PreferLocationsWithFallback(balancer balancer.Balancer, locations ...string) balancer.Balancer {
8475
if len(locations) == 0 {
8576
panic("empty list of locations")
8677
}
8778
for i := range locations {
8879
locations[i] = strings.ToUpper(locations[i])
8980
}
90-
return multi.Balancer(
91-
multi.WithBalancer(b, func(cc conn.Conn) bool {
92-
location := strings.ToUpper(cc.Endpoint().Location())
81+
return PreferWithFallback(
82+
balancer,
83+
func(endpoint Endpoint) bool {
84+
location := strings.ToUpper(endpoint.Location())
9385
for _, l := range locations {
9486
if location == l {
9587
return true
9688
}
9789
}
9890
return false
99-
}),
100-
multi.WithBalancer(b.(balancer.Creator).Create(), func(cc conn.Conn) bool {
101-
location := strings.ToUpper(cc.Endpoint().Location())
102-
for _, l := range locations {
103-
if location == l {
104-
return false
105-
}
106-
}
107-
return true
108-
}),
91+
},
92+
)
93+
}
94+
95+
type Endpoint interface {
96+
NodeID() uint32
97+
Address() string
98+
Location() string
99+
LocalDC() bool
100+
}
101+
102+
// Prefer creates balancer which use endpoints by filter
103+
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter
104+
func Prefer(balancer balancer.Balancer, filter func(endpoint Endpoint) bool) balancer.Balancer {
105+
return multi.Balancer(
106+
multi.WithBalancer(
107+
balancer,
108+
func(cc conn.Conn) bool {
109+
return filter(cc.Endpoint())
110+
},
111+
),
112+
)
113+
}
114+
115+
// PreferWithFallback creates balancer which use endpoints by filter
116+
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter
117+
// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead
118+
func PreferWithFallback(balancer balancer.Balancer, filter func(endpoint Endpoint) bool) balancer.Balancer {
119+
return multi.Balancer(
120+
multi.WithBalancer(
121+
balancer,
122+
func(cc conn.Conn) bool {
123+
return filter(cc.Endpoint())
124+
},
125+
),
126+
multi.WithBalancer(
127+
balancer,
128+
func(cc conn.Conn) bool {
129+
return !filter(cc.Endpoint())
130+
},
131+
),
109132
)
110133
}
111134

internal/meta/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package meta
22

33
const (
4-
Version = "ydb-go-sdk/3.11.13"
4+
Version = "ydb-go-sdk/3.12.0"
55
)

0 commit comments

Comments
 (0)