2021-04-05 14:48:01 +00:00
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
2021-04-05 17:10:03 +00:00
|
|
|
"sync"
|
2021-04-05 14:48:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-04-07 12:54:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
|
|
"github.com/pkg/errors"
|
2021-04-05 14:48:01 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PoolBuilderOptions struct {
|
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
NodeConnectionTimeout time.Duration
|
|
|
|
NodeRequestTimeout time.Duration
|
|
|
|
ClientRebalanceInterval time.Duration
|
2021-04-07 12:54:30 +00:00
|
|
|
SessionExpirationEpoch uint64
|
2021-04-05 17:10:03 +00:00
|
|
|
weights []float64
|
|
|
|
connections []*grpc.ClientConn
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PoolBuilder struct {
|
|
|
|
addresses []string
|
|
|
|
weights []float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *PoolBuilder) AddNode(address string, weight float64) *PoolBuilder {
|
|
|
|
pb.addresses = append(pb.addresses, address)
|
|
|
|
pb.weights = append(pb.weights, weight)
|
|
|
|
return pb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (Pool, error) {
|
|
|
|
totalWeight := 0.0
|
|
|
|
for _, w := range pb.weights {
|
|
|
|
totalWeight += w
|
|
|
|
}
|
|
|
|
if math.Abs(totalWeight-1.0) >= 1e-4 {
|
|
|
|
return nil, errors.New("total weight must be equal to unity")
|
|
|
|
}
|
|
|
|
var cons = make([]*grpc.ClientConn, len(pb.addresses))
|
|
|
|
for i, address := range pb.addresses {
|
|
|
|
con, err := func() (*grpc.ClientConn, error) {
|
|
|
|
toctx, c := context.WithTimeout(ctx, options.NodeConnectionTimeout)
|
|
|
|
defer c()
|
|
|
|
return grpc.DialContext(toctx, address, grpc.WithInsecure(), grpc.WithBlock())
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cons[i] = con
|
|
|
|
}
|
2021-04-05 17:10:03 +00:00
|
|
|
options.weights = pb.weights
|
|
|
|
options.connections = cons
|
|
|
|
return new(ctx, options)
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Pool interface {
|
2021-04-07 12:54:30 +00:00
|
|
|
ConnectionArtifacts() (client.Client, *token.SessionToken, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientPack struct {
|
|
|
|
client client.Client
|
|
|
|
sessionToken *token.SessionToken
|
|
|
|
healthy bool
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type pool struct {
|
2021-04-07 12:54:30 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
sampler *Sampler
|
|
|
|
clientPacks []*clientPack
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 17:10:03 +00:00
|
|
|
func new(ctx context.Context, options *PoolBuilderOptions) (Pool, error) {
|
2021-04-07 12:54:30 +00:00
|
|
|
clientPacks := make([]*clientPack, len(options.weights))
|
2021-04-05 17:10:03 +00:00
|
|
|
for i, con := range options.connections {
|
|
|
|
c, err := client.New(client.WithDefaultPrivateKey(options.Key), client.WithGRPCConnection(con))
|
2021-04-05 14:48:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-07 12:54:30 +00:00
|
|
|
st, err := c.CreateSession(ctx, options.SessionExpirationEpoch)
|
|
|
|
if err != nil {
|
|
|
|
address := "unknown"
|
|
|
|
if epi, err := c.EndpointInfo(ctx); err == nil {
|
|
|
|
address = epi.NodeInfo().Address()
|
|
|
|
}
|
|
|
|
return nil, errors.Wrapf(err, "failed to create neofs session token for client %s", address)
|
|
|
|
}
|
|
|
|
clientPacks[i] = &clientPack{client: c, sessionToken: st, healthy: true}
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
2021-04-07 12:54:30 +00:00
|
|
|
sampler := NewSampler(options.weights, source)
|
|
|
|
pool := &pool{sampler: sampler, clientPacks: clientPacks}
|
2021-04-05 17:10:03 +00:00
|
|
|
go func() {
|
|
|
|
ticker := time.NewTimer(options.ClientRebalanceInterval)
|
|
|
|
for range ticker.C {
|
|
|
|
ok := true
|
2021-04-07 12:54:30 +00:00
|
|
|
for i, clientPack := range pool.clientPacks {
|
2021-04-05 17:10:03 +00:00
|
|
|
func() {
|
|
|
|
tctx, c := context.WithTimeout(ctx, options.NodeRequestTimeout)
|
|
|
|
defer c()
|
2021-04-07 12:54:30 +00:00
|
|
|
if _, err := clientPack.client.EndpointInfo(tctx); err != nil {
|
2021-04-05 17:10:03 +00:00
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
pool.lock.Lock()
|
2021-04-07 12:54:30 +00:00
|
|
|
pool.clientPacks[i].healthy = ok
|
2021-04-05 17:10:03 +00:00
|
|
|
pool.lock.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
ticker.Reset(options.ClientRebalanceInterval)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return pool, nil
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:54:30 +00:00
|
|
|
func (p *pool) ConnectionArtifacts() (client.Client, *token.SessionToken, error) {
|
2021-04-05 17:10:03 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
2021-04-07 12:54:30 +00:00
|
|
|
if len(p.clientPacks) == 1 {
|
|
|
|
cp := p.clientPacks[0]
|
|
|
|
if cp.healthy {
|
|
|
|
return cp.client, cp.sessionToken, nil
|
2021-04-05 17:10:03 +00:00
|
|
|
}
|
2021-04-07 12:54:30 +00:00
|
|
|
return nil, nil, errors.New("no healthy client")
|
2021-04-05 17:10:03 +00:00
|
|
|
}
|
2021-04-08 09:37:55 +00:00
|
|
|
attempts := 3 * len(p.clientPacks)
|
|
|
|
for k := 0; k < attempts; k++ {
|
|
|
|
i := p.sampler.Next()
|
|
|
|
if cp := p.clientPacks[i]; cp.healthy {
|
|
|
|
return cp.client, cp.sessionToken, nil
|
2021-04-05 17:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 12:54:30 +00:00
|
|
|
return nil, nil, errors.New("no healthy client")
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|