-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sorting functionality and new rolling strategies
Introduced a sorting mechanism for clients in the `rpc` package. Added interfaces and implementations for sticky rolling strategy, along with comprehensive test cases. Improved thread safety and replaced the old rolling strategy implementations.
- Loading branch information
Showing
8 changed files
with
275 additions
and
139 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
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
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 |
---|---|---|
@@ -1,51 +1 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type rollClient struct { | ||
callCount int | ||
name string | ||
} | ||
|
||
func TestRollingStrategy(t *testing.T) { | ||
|
||
rollingStrategy := NewRollingStrategyRoundRobin[*rollClient]() | ||
rollingStrategy.reset() | ||
|
||
clients := NewClients(2*time.Second, rollingStrategy) | ||
clients.Add(&rollClient{name: "c.1"}) | ||
clients.Add(&rollClient{name: "c.2"}) | ||
clients.Add(&rollClient{name: "c.3"}) | ||
clients.Add(&rollClient{name: "c.a"}) | ||
clients.Add(&rollClient{name: "c.b"}) | ||
|
||
var clientNames []string | ||
_, err := WithClients(clients, func(ctx context.Context, client *rollClient) (v any, err error) { | ||
clientNames = append(clientNames, client.name) | ||
if client.name == "c.3" { | ||
return nil, nil | ||
} | ||
|
||
return nil, fmt.Errorf("next please") | ||
}) | ||
|
||
require.NoError(t, err) | ||
//require.ErrorIs(t, err, ErrorNoMoreClient) | ||
require.Equal(t, []string{"c.1", "c.2", "c.3"}, clientNames) | ||
|
||
_, err = WithClients(clients, func(ctx context.Context, client *rollClient) (v any, err error) { | ||
clientNames = append(clientNames, client.name) | ||
return nil, fmt.Errorf("next please") | ||
}) | ||
|
||
require.ErrorIs(t, err, ErrorNoMoreClient) | ||
require.Equal(t, []string{"c.1", "c.2", "c.3", "c.3", "c.a", "c.b", "c.1", "c.2"}, clientNames) | ||
|
||
} |
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
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,96 @@ | ||
package rpc | ||
|
||
type RollingStrategy[C any] interface { | ||
reset() | ||
next(clients *Clients[C]) (C, error) | ||
} | ||
|
||
type StickyRollingStrategy[C any] struct { | ||
fistCallToNewClient bool | ||
usedClientCount int | ||
nextClientIndex int | ||
} | ||
|
||
func NewStickyRollingStrategy[C any]() *StickyRollingStrategy[C] { | ||
return &StickyRollingStrategy[C]{ | ||
fistCallToNewClient: true, | ||
} | ||
} | ||
|
||
func (s *StickyRollingStrategy[C]) reset() { | ||
s.usedClientCount = 0 | ||
} | ||
func (s *StickyRollingStrategy[C]) next(clients *Clients[C]) (client C, err error) { | ||
clients.lock.Lock() | ||
defer clients.lock.Unlock() | ||
|
||
if len(clients.clients) == s.usedClientCount { | ||
return client, ErrorNoMoreClient | ||
} | ||
|
||
if s.fistCallToNewClient { | ||
s.fistCallToNewClient = false | ||
client = clients.clients[0] | ||
s.usedClientCount = s.usedClientCount + 1 | ||
s.nextClientIndex = s.nextClientIndex + 1 | ||
return client, nil | ||
} | ||
|
||
if s.nextClientIndex == len(clients.clients) { //roll to 1st client | ||
s.nextClientIndex = 0 | ||
} | ||
|
||
if s.usedClientCount == 0 { //just been reset | ||
s.nextClientIndex = s.prevIndex(clients) | ||
client = clients.clients[s.nextClientIndex] | ||
s.usedClientCount = s.usedClientCount + 1 | ||
s.nextClientIndex = s.nextClientIndex + 1 | ||
return client, nil | ||
} | ||
|
||
if s.nextClientIndex == len(clients.clients) { //roll to 1st client | ||
client = clients.clients[0] | ||
s.usedClientCount = s.usedClientCount + 1 | ||
return client, nil | ||
} | ||
|
||
client = clients.clients[s.nextClientIndex] | ||
s.usedClientCount = s.usedClientCount + 1 | ||
s.nextClientIndex = s.nextClientIndex + 1 | ||
return client, nil | ||
} | ||
|
||
func (s *StickyRollingStrategy[C]) prevIndex(clients *Clients[C]) int { | ||
clients.lock.Lock() | ||
defer clients.lock.Unlock() | ||
|
||
if s.nextClientIndex == 0 { | ||
return len(clients.clients) - 1 | ||
} | ||
return s.nextClientIndex - 1 | ||
} | ||
|
||
type RollingStrategyAlwaysUseFirst[C any] struct { | ||
nextIndex int | ||
} | ||
|
||
func NewRollingStrategyAlwaysUseFirst[C any]() *RollingStrategyAlwaysUseFirst[C] { | ||
return &RollingStrategyAlwaysUseFirst[C]{} | ||
} | ||
|
||
func (s *RollingStrategyAlwaysUseFirst[C]) reset() { | ||
s.nextIndex = 0 | ||
} | ||
|
||
func (s *RollingStrategyAlwaysUseFirst[C]) next(c *Clients[C]) (client C, err error) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
if len(c.clients) <= s.nextIndex { | ||
return client, ErrorNoMoreClient | ||
} | ||
client = c.clients[s.nextIndex] | ||
s.nextIndex++ | ||
return client, 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,56 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type rollClient struct { | ||
callCount int | ||
name string | ||
sortValue uint64 | ||
} | ||
|
||
func (r *rollClient) fetchSortValue(ctx context.Context) (sortValue uint64, err error) { | ||
return r.sortValue, nil | ||
} | ||
|
||
func TestStickyRollingStrategy(t *testing.T) { | ||
|
||
rollingStrategy := NewStickyRollingStrategy[*rollClient]() | ||
rollingStrategy.reset() | ||
|
||
clients := NewClients(2*time.Second, rollingStrategy) | ||
clients.Add(&rollClient{name: "c.1"}) | ||
clients.Add(&rollClient{name: "c.2"}) | ||
clients.Add(&rollClient{name: "c.3"}) | ||
clients.Add(&rollClient{name: "c.a"}) | ||
clients.Add(&rollClient{name: "c.b"}) | ||
|
||
var clientNames []string | ||
_, err := WithClients(clients, func(ctx context.Context, client *rollClient) (v any, err error) { | ||
clientNames = append(clientNames, client.name) | ||
if client.name == "c.3" { | ||
return nil, nil | ||
} | ||
|
||
return nil, fmt.Errorf("next please") | ||
}) | ||
|
||
require.NoError(t, err) | ||
//require.ErrorIs(t, err, ErrorNoMoreClient) | ||
require.Equal(t, []string{"c.1", "c.2", "c.3"}, clientNames) | ||
|
||
_, err = WithClients(clients, func(ctx context.Context, client *rollClient) (v any, err error) { | ||
clientNames = append(clientNames, client.name) | ||
return nil, fmt.Errorf("next please") | ||
}) | ||
|
||
require.ErrorIs(t, err, ErrorNoMoreClient) | ||
require.Equal(t, []string{"c.1", "c.2", "c.3", "c.3", "c.a", "c.b", "c.1", "c.2"}, clientNames) | ||
|
||
} |
Oops, something went wrong.