mirror of
https://github.com/hanzoai/psrpc.git
synced 2026-09-30 23:47:22 +00:00
PSRPC for Hanzo LiveKit, forked from livekit/psrpc (Apache-2.0). Two rewrites: github.com/livekit/psrpc -> github.com/hanzoai/psrpc github.com/redis/go-redis/v9 -> github.com/hanzokv/go/v9 psrpc's message bus (bus.go, internal/bus/bus_redis.go, internal/bus/bustest) speaks to a KV over go-redis. After hanzoai/livekit moved to our client, psrpc was the one remaining edge pulling upstream go-redis into that binary — not our code, but our dependency, the same shape redsync had for hanzoai/git. The other livekit/* modules (protocol, mediatransportutil, mageutil) are NOT forked and should not be: none of them touch a KV client, so they are ordinary external dependencies and their names are addresses.
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
// Copyright 2023 LiveKit, 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 client
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/hanzoai/psrpc"
|
|
"github.com/hanzoai/psrpc/internal"
|
|
)
|
|
|
|
func TestAffinity(t *testing.T) {
|
|
testAffinity(t, psrpc.SelectionOpts{
|
|
AcceptFirstAvailable: true,
|
|
}, "1")
|
|
|
|
testAffinity(t, psrpc.SelectionOpts{
|
|
AcceptFirstAvailable: true,
|
|
MinimumAffinity: 0.5,
|
|
}, "2")
|
|
|
|
testAffinity(t, psrpc.SelectionOpts{
|
|
ShortCircuitTimeout: time.Millisecond * 150,
|
|
}, "2")
|
|
|
|
testAffinity(t, psrpc.SelectionOpts{
|
|
MinimumAffinity: 0.4,
|
|
AffinityTimeout: 0,
|
|
ShortCircuitTimeout: time.Millisecond * 250,
|
|
}, "3")
|
|
|
|
testAffinity(t, psrpc.SelectionOpts{
|
|
MinimumAffinity: 0.3,
|
|
AffinityTimeout: time.Millisecond * 250,
|
|
ShortCircuitTimeout: time.Millisecond * 200,
|
|
}, "2")
|
|
|
|
testAffinity(t, psrpc.SelectionOpts{
|
|
AffinityTimeout: time.Millisecond * 600,
|
|
}, "5")
|
|
}
|
|
|
|
func testAffinity(t *testing.T, opts psrpc.SelectionOpts, expectedID string) {
|
|
c := make(chan *internal.ClaimRequest, 100)
|
|
go func() {
|
|
c <- &internal.ClaimRequest{
|
|
RequestId: "1",
|
|
ServerId: "1",
|
|
Affinity: 0.1,
|
|
}
|
|
time.Sleep(time.Millisecond * 100)
|
|
c <- &internal.ClaimRequest{
|
|
RequestId: "1",
|
|
ServerId: "2",
|
|
Affinity: 0.5,
|
|
}
|
|
time.Sleep(time.Millisecond * 200)
|
|
c <- &internal.ClaimRequest{
|
|
RequestId: "1",
|
|
ServerId: "3",
|
|
Affinity: 0.7,
|
|
}
|
|
c <- &internal.ClaimRequest{
|
|
RequestId: "1",
|
|
ServerId: "4",
|
|
Affinity: 0.1,
|
|
}
|
|
time.Sleep(time.Millisecond * 200)
|
|
c <- &internal.ClaimRequest{
|
|
RequestId: "1",
|
|
ServerId: "5",
|
|
Affinity: 0.9,
|
|
}
|
|
}()
|
|
serverID, err := selectServer(context.Background(), c, nil, opts)
|
|
require.NoError(t, err)
|
|
require.Equal(t, expectedID, serverID)
|
|
}
|