@@ -81,6 +81,9 @@ type Opts struct {
8181 CheckTimeout time.Duration
8282 // ConnectionHandler provides an ability to handle connection updates.
8383 ConnectionHandler ConnectionHandler
84+ // BalancerFactory - a factory for creating balancing,
85+ // contains the pool size as well as the connections for which it is used.
86+ BalancerFactory BalancerFactory
8487}
8588
8689/*
@@ -110,9 +113,9 @@ type ConnectionPool struct {
110113
111114 state state
112115 done chan struct {}
113- roPool * roundRobinStrategy
114- rwPool * roundRobinStrategy
115- anyPool * roundRobinStrategy
116+ roPool BalancingPool
117+ rwPool BalancingPool
118+ anyPool BalancingPool
116119 poolsMutex sync.RWMutex
117120 watcherContainer watcherContainer
118121}
@@ -153,6 +156,10 @@ func newEndpoint(name string, dialer tarantool.Dialer, opts tarantool.Opts) *end
153156// opts. Instances must have unique names.
154157func ConnectWithOpts (ctx context.Context , instances []Instance ,
155158 opts Opts ) (* ConnectionPool , error ) {
159+ if opts .BalancerFactory == nil {
160+ opts .BalancerFactory = & RoundRobinFactory {}
161+ }
162+
156163 unique := make (map [string ]bool )
157164 for _ , instance := range instances {
158165 if _ , ok := unique [instance .Name ]; ok {
@@ -166,9 +173,9 @@ func ConnectWithOpts(ctx context.Context, instances []Instance,
166173 }
167174
168175 size := len (instances )
169- rwPool := newRoundRobinStrategy (size )
170- roPool := newRoundRobinStrategy (size )
171- anyPool := newRoundRobinStrategy (size )
176+ rwPool := opts . BalancerFactory . Create (size )
177+ roPool := opts . BalancerFactory . Create (size )
178+ anyPool := opts . BalancerFactory . Create (size )
172179
173180 connPool := & ConnectionPool {
174181 ends : make (map [string ]* endpoint ),
@@ -218,15 +225,15 @@ func (p *ConnectionPool) ConnectedNow(mode Mode) (bool, error) {
218225 }
219226 switch mode {
220227 case ANY :
221- return ! p .anyPool . IsEmpty ( ), nil
228+ return ! IsEmpty ( p .anyPool ), nil
222229 case RW :
223- return ! p .rwPool . IsEmpty ( ), nil
230+ return ! IsEmpty ( p .rwPool ), nil
224231 case RO :
225- return ! p .roPool . IsEmpty ( ), nil
232+ return ! IsEmpty ( p .roPool ), nil
226233 case PreferRW :
227234 fallthrough
228235 case PreferRO :
229- return ! p .rwPool . IsEmpty ( ) || ! p .roPool . IsEmpty ( ), nil
236+ return ! IsEmpty ( p .rwPool ) || ! IsEmpty ( p .roPool ), nil
230237 default :
231238 return false , ErrNoHealthyInstance
232239 }
0 commit comments