Skip to content

Commit

Permalink
Merge pull request #153 from ydb-platform/balancer.create
Browse files Browse the repository at this point in the history
v3.14.1: balancers.CreateFromConfig + balancer.Balancer.Create
  • Loading branch information
asmyasnikov authored Mar 12, 2022
2 parents 731a317 + 6654691 commit 9caa466
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 42 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 3.14.1
* Added `balacers.CreateFromConfig` balancer creator
* Added `Create` method to interface `balancer.Balancer`

## 3.14.0
* Added `balacers.FromConfig` balancers creator
* Added `balacers.FromConfig` balancer creator

## 3.13.3
* Fixed linter issues
Expand Down
63 changes: 36 additions & 27 deletions balancers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,15 @@ func WithParseErrorHandler(errorHandler func(error)) fromConfigOption {
}
}

func FromConfig(config string, opts ...fromConfigOption) (b balancer.Balancer) {
func CreateFromConfig(config string) (balancer.Balancer, error) {
var (
h = fromConfigOptionsHolder{
fallbackBalancer: Default(),
}
c balancersConfig
b balancer.Balancer
err error
c balancersConfig
)

for _, o := range opts {
o(&h)
}

if err := json.Unmarshal([]byte(config), &c); err != nil {
if h.errorHandler != nil {
h.errorHandler(err)
}
return h.fallbackBalancer
if err = json.Unmarshal([]byte(config), &c); err != nil {
return nil, err
}

switch c.Type {
Expand All @@ -75,30 +67,47 @@ func FromConfig(config string, opts ...fromConfigOption) (b balancer.Balancer) {
case typeRoundRobin:
b = RoundRobin()
default:
if h.errorHandler != nil {
h.errorHandler(errors.Errorf("unknown type of balancer: %s", c.Type))
}
return h.fallbackBalancer
return nil, errors.Errorf("unknown type of balancer: %s", c.Type)
}

switch c.Prefer {
case preferLocalDC:
if c.Fallback {
return PreferLocalDCWithFallBack(b)
return PreferLocalDCWithFallBack(b), nil
}
return PreferLocalDC(b)
return PreferLocalDC(b), nil
case preferLocations:
if len(c.Locations) == 0 {
if h.errorHandler != nil {
h.errorHandler(errors.Errorf("empty locations list in balancer '%s' config", c.Type))
}
return h.fallbackBalancer
return nil, errors.Errorf("empty locations list in balancer '%s' config", c.Type)
}
if c.Fallback {
return PreferLocationsWithFallback(b, c.Locations...)
return PreferLocationsWithFallback(b, c.Locations...), nil
}
return PreferLocations(b, c.Locations...)
return PreferLocations(b, c.Locations...), nil
default:
return b
return b, nil
}
}

func FromConfig(config string, opts ...fromConfigOption) balancer.Balancer {
var (
h = fromConfigOptionsHolder{
fallbackBalancer: Default(),
}
b balancer.Balancer
err error
)
for _, o := range opts {
o(&h)
}

b, err = CreateFromConfig(config)
if err != nil {
if h.errorHandler != nil {
h.errorHandler(err)
}
return h.fallbackBalancer
}

return b
}
4 changes: 4 additions & 0 deletions balancers/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (

type testBalancer struct{}

func (t testBalancer) Create() balancer.Balancer {
panic("unexpected call")
}

func (t testBalancer) Next() conn.Conn {
panic("unexpected call")
}
Expand Down
3 changes: 1 addition & 2 deletions internal/balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ type Balancer interface {

// Contains returns true if Balancer contains requested element.
Contains(Element) bool
}

type Creator interface {
// Create makes empty balancer with same implementation
Create() Balancer
}
2 changes: 1 addition & 1 deletion internal/balancer/multi/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type multi struct {
func (m *multi) Create() balancer.Balancer {
bb := make([]balancer.Balancer, len(m.balancer))
for i, b := range m.balancer {
bb[i] = b.(balancer.Creator).Create()
bb[i] = b.Create()
}
return &multi{
balancer: bb,
Expand Down
8 changes: 8 additions & 0 deletions internal/balancer/stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type stubBalancer struct {
OnRemove func(balancer.Element) bool
OnPessimize func(context.Context, balancer.Element) error
OnContains func(balancer.Element) bool
OnCreate func() balancer.Balancer
}

func Balancer() (*list.List, balancer.Balancer) {
Expand Down Expand Up @@ -55,6 +56,13 @@ func Balancer() (*list.List, balancer.Balancer) {
}
}

func (s stubBalancer) Create() balancer.Balancer {
if f := s.OnCreate; f != nil {
return f()
}
return nil
}

func (s stubBalancer) Next() conn.Conn {
if f := s.OnNext; f != nil {
return f()
Expand Down
2 changes: 1 addition & 1 deletion internal/meta/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package meta

const (
Version = "ydb-go-sdk/3.14.0"
Version = "ydb-go-sdk/3.14.1"
)
16 changes: 6 additions & 10 deletions with.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package ydb
import (
"context"
"sync/atomic"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer"
)

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

if creator, ok := c.config.Balancer().(balancer.Creator); ok {
opts = append(
opts,
WithBalancer(
creator.Create(),
),
)
}
opts = append(
opts,
WithBalancer(
c.config.Balancer().Create(),
),
)

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

Expand Down

0 comments on commit 9caa466

Please sign in to comment.