Skip to content

Commit 6654691

Browse files
committed
balancers.CreateFromConfig + balancer.Balancer.Create
1 parent 8d2793b commit 6654691

File tree

8 files changed

+62
-42
lines changed

8 files changed

+62
-42
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
## 3.14.1
2+
* Added `balacers.CreateFromConfig` balancer creator
3+
* Added `Create` method to interface `balancer.Balancer`
4+
15
## 3.14.0
2-
* Added `balacers.FromConfig` balancers creator
6+
* Added `balacers.FromConfig` balancer creator
37

48
## 3.13.3
59
* Fixed linter issues

balancers/config.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,15 @@ func WithParseErrorHandler(errorHandler func(error)) fromConfigOption {
4848
}
4949
}
5050

51-
func FromConfig(config string, opts ...fromConfigOption) (b balancer.Balancer) {
51+
func CreateFromConfig(config string) (balancer.Balancer, error) {
5252
var (
53-
h = fromConfigOptionsHolder{
54-
fallbackBalancer: Default(),
55-
}
56-
c balancersConfig
53+
b balancer.Balancer
54+
err error
55+
c balancersConfig
5756
)
5857

59-
for _, o := range opts {
60-
o(&h)
61-
}
62-
63-
if err := json.Unmarshal([]byte(config), &c); err != nil {
64-
if h.errorHandler != nil {
65-
h.errorHandler(err)
66-
}
67-
return h.fallbackBalancer
58+
if err = json.Unmarshal([]byte(config), &c); err != nil {
59+
return nil, err
6860
}
6961

7062
switch c.Type {
@@ -75,30 +67,47 @@ func FromConfig(config string, opts ...fromConfigOption) (b balancer.Balancer) {
7567
case typeRoundRobin:
7668
b = RoundRobin()
7769
default:
78-
if h.errorHandler != nil {
79-
h.errorHandler(errors.Errorf("unknown type of balancer: %s", c.Type))
80-
}
81-
return h.fallbackBalancer
70+
return nil, errors.Errorf("unknown type of balancer: %s", c.Type)
8271
}
8372

8473
switch c.Prefer {
8574
case preferLocalDC:
8675
if c.Fallback {
87-
return PreferLocalDCWithFallBack(b)
76+
return PreferLocalDCWithFallBack(b), nil
8877
}
89-
return PreferLocalDC(b)
78+
return PreferLocalDC(b), nil
9079
case preferLocations:
9180
if len(c.Locations) == 0 {
92-
if h.errorHandler != nil {
93-
h.errorHandler(errors.Errorf("empty locations list in balancer '%s' config", c.Type))
94-
}
95-
return h.fallbackBalancer
81+
return nil, errors.Errorf("empty locations list in balancer '%s' config", c.Type)
9682
}
9783
if c.Fallback {
98-
return PreferLocationsWithFallback(b, c.Locations...)
84+
return PreferLocationsWithFallback(b, c.Locations...), nil
9985
}
100-
return PreferLocations(b, c.Locations...)
86+
return PreferLocations(b, c.Locations...), nil
10187
default:
102-
return b
88+
return b, nil
10389
}
10490
}
91+
92+
func FromConfig(config string, opts ...fromConfigOption) balancer.Balancer {
93+
var (
94+
h = fromConfigOptionsHolder{
95+
fallbackBalancer: Default(),
96+
}
97+
b balancer.Balancer
98+
err error
99+
)
100+
for _, o := range opts {
101+
o(&h)
102+
}
103+
104+
b, err = CreateFromConfig(config)
105+
if err != nil {
106+
if h.errorHandler != nil {
107+
h.errorHandler(err)
108+
}
109+
return h.fallbackBalancer
110+
}
111+
112+
return b
113+
}

balancers/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010

1111
type testBalancer struct{}
1212

13+
func (t testBalancer) Create() balancer.Balancer {
14+
panic("unexpected call")
15+
}
16+
1317
func (t testBalancer) Next() conn.Conn {
1418
panic("unexpected call")
1519
}

internal/balancer/balancer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ type Balancer interface {
2929

3030
// Contains returns true if Balancer contains requested element.
3131
Contains(Element) bool
32-
}
3332

34-
type Creator interface {
33+
// Create makes empty balancer with same implementation
3534
Create() Balancer
3635
}

internal/balancer/multi/multi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type multi struct {
2626
func (m *multi) Create() balancer.Balancer {
2727
bb := make([]balancer.Balancer, len(m.balancer))
2828
for i, b := range m.balancer {
29-
bb[i] = b.(balancer.Creator).Create()
29+
bb[i] = b.Create()
3030
}
3131
return &multi{
3232
balancer: bb,

internal/balancer/stub/stub.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type stubBalancer struct {
1616
OnRemove func(balancer.Element) bool
1717
OnPessimize func(context.Context, balancer.Element) error
1818
OnContains func(balancer.Element) bool
19+
OnCreate func() balancer.Balancer
1920
}
2021

2122
func Balancer() (*list.List, balancer.Balancer) {
@@ -55,6 +56,13 @@ func Balancer() (*list.List, balancer.Balancer) {
5556
}
5657
}
5758

59+
func (s stubBalancer) Create() balancer.Balancer {
60+
if f := s.OnCreate; f != nil {
61+
return f()
62+
}
63+
return nil
64+
}
65+
5866
func (s stubBalancer) Next() conn.Conn {
5967
if f := s.OnNext; f != nil {
6068
return f()

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.14.0"
4+
Version = "ydb-go-sdk/3.14.1"
55
)

with.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package ydb
33
import (
44
"context"
55
"sync/atomic"
6-
7-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer"
86
)
97

108
var nextID = uint64(0)
@@ -14,14 +12,12 @@ func (c *connection) With(ctx context.Context, opts ...Option) (Connection, erro
1412
return c, nil
1513
}
1614

17-
if creator, ok := c.config.Balancer().(balancer.Creator); ok {
18-
opts = append(
19-
opts,
20-
WithBalancer(
21-
creator.Create(),
22-
),
23-
)
24-
}
15+
opts = append(
16+
opts,
17+
WithBalancer(
18+
c.config.Balancer().Create(),
19+
),
20+
)
2521

2622
id := atomic.AddUint64(&nextID, 1)
2723

0 commit comments

Comments
 (0)