-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56b67ac
commit ee2a56d
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package picker | ||
|
||
import ( | ||
"math/rand" | ||
"net/http" | ||
"sync/atomic" | ||
|
||
"github.com/bufbuild/go-http-balancer/balancer/conn" | ||
) | ||
|
||
type roundRobinFactory struct{} | ||
|
||
type roundRobin struct { | ||
conns []conn.Conn | ||
// +checkatomic | ||
counter atomic.Uint64 | ||
} | ||
|
||
// NewRoundRobinFactory creates a picker factory that picks connections in a | ||
// "round-robin" fashion, that is to say, in sequential order. In order to | ||
// mitigate the risk of a "thundering herd" scenario, the order of connections | ||
// is randomized each time the list of hosts changes. | ||
func NewRoundRobinFactory() Factory { | ||
return &roundRobinFactory{} | ||
} | ||
|
||
func (f roundRobinFactory) New(_ Picker, allConns conn.Connections) Picker { | ||
numConns := allConns.Len() | ||
conns := make([]conn.Conn, numConns) | ||
for i := 0; i < numConns; i++ { | ||
j := rand.Intn(i + 1) //nolint:gosec | ||
conns[i] = conns[j] | ||
conns[j] = allConns.Get(i) | ||
} | ||
return &roundRobin{conns: conns} | ||
} | ||
|
||
func (r *roundRobin) Pick(_ *http.Request) (conn conn.Conn, whenDone func(), err error) { | ||
return r.conns[r.counter.Add(1)%uint64(len(r.conns))], nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package picker_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/bufbuild/go-http-balancer/balancer/conn" | ||
"github.com/bufbuild/go-http-balancer/balancer/picker" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRoundRobinPicker(t *testing.T) { | ||
t.Parallel() | ||
|
||
// TODO: when possible, it'd be good to test multiple connections to verify | ||
// that shuffling works and that we get round-robin behavior. | ||
|
||
dummyConn := conn.Conn(nil) | ||
allConns := dummyConns{[]conn.Conn{ | ||
dummyConn, | ||
}} | ||
|
||
factory := picker.NewRoundRobinFactory() | ||
picker := factory.New(nil, allConns) | ||
conn, _, err := picker.Pick(&http.Request{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, dummyConn, conn) | ||
} |