Skip to content

Commit

Permalink
Add roundrobin picker.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchadwick-buf committed Jun 7, 2023
1 parent 56b67ac commit ee2a56d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
54 changes: 54 additions & 0 deletions balancer/picker/roundrobin.go
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
}
42 changes: 42 additions & 0 deletions balancer/picker/roundrobin_test.go
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)
}

0 comments on commit ee2a56d

Please sign in to comment.