Skip to content

Commit 094d25f

Browse files
committed
Extracted round robin load balancing strategy in a separate file
1 parent 6213106 commit 094d25f

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

src/v1/internal/connection-providers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Rediscovery from './rediscovery';
2525
import hasFeature from './features';
2626
import {DnsHostNameResolver, DummyHostNameResolver} from './host-name-resolvers';
2727
import RoutingUtil from './routing-util';
28-
import {RoundRobinLoadBalancingStrategy} from './load-balancing-strategies';
28+
import RoundRobinLoadBalancingStrategy from './round-robin-load-balancing-strategy';
2929

3030
class ConnectionProvider {
3131

src/v1/internal/load-balancing-strategies.js renamed to src/v1/internal/load-balancing-strategy.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
import RoundRobinArrayIndex from './round-robin-array-index';
2019

21-
class LoadBalancingStrategy {
20+
21+
/**
22+
* A facility to select most appropriate reader or writer among the given addresses for request processing.
23+
*/
24+
export default class LoadBalancingStrategy {
2225

2326
/**
2427
* Select next most appropriate reader from the list of given readers.
@@ -38,35 +41,3 @@ class LoadBalancingStrategy {
3841
throw new Error('Abstract function');
3942
}
4043
}
41-
42-
export class RoundRobinLoadBalancingStrategy extends LoadBalancingStrategy {
43-
44-
constructor() {
45-
super();
46-
this._readersIndex = new RoundRobinArrayIndex();
47-
this._writersIndex = new RoundRobinArrayIndex();
48-
}
49-
50-
/**
51-
* @inheritDoc
52-
*/
53-
selectReader(knownReaders) {
54-
return this._select(knownReaders, this._readersIndex);
55-
}
56-
57-
/**
58-
* @inheritDoc
59-
*/
60-
selectWriter(knownWriters) {
61-
return this._select(knownWriters, this._writersIndex);
62-
}
63-
64-
_select(addresses, roundRobinIndex) {
65-
const length = addresses.length;
66-
if (length === 0) {
67-
return null;
68-
}
69-
const index = roundRobinIndex.next(length);
70-
return addresses[index];
71-
}
72-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2002-2017 "Neo Technology,","
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import RoundRobinArrayIndex from './round-robin-array-index';
20+
import LoadBalancingStrategy from './load-balancing-strategy';
21+
22+
export default class RoundRobinLoadBalancingStrategy extends LoadBalancingStrategy {
23+
24+
constructor() {
25+
super();
26+
this._readersIndex = new RoundRobinArrayIndex();
27+
this._writersIndex = new RoundRobinArrayIndex();
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
selectReader(knownReaders) {
34+
return this._select(knownReaders, this._readersIndex);
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
selectWriter(knownWriters) {
41+
return this._select(knownWriters, this._writersIndex);
42+
}
43+
44+
_select(addresses, roundRobinIndex) {
45+
const length = addresses.length;
46+
if (length === 0) {
47+
return null;
48+
}
49+
const index = roundRobinIndex.next(length);
50+
return addresses[index];
51+
}
52+
}

test/internal/load-balancing-strategies.test.js renamed to test/internal/round-robin-load-balancing-strategy.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
import {RoundRobinLoadBalancingStrategy} from '../../src/v1/internal/load-balancing-strategies';
19+
import RoundRobinLoadBalancingStrategy from '../../src/v1/internal/round-robin-load-balancing-strategy';
2020

2121
describe('RoundRobinLoadBalancingStrategy', () => {
2222

0 commit comments

Comments
 (0)