All checks were successful
Vulncheck / Vulncheck (push) Successful in 1m7s
Pre-commit hooks / Pre-commit (push) Successful in 1m36s
Build / Build Components (push) Successful in 1m47s
Tests and linters / gopls check (push) Successful in 3m50s
Tests and linters / Tests (push) Successful in 4m4s
Tests and linters / Run gofumpt (push) Successful in 4m0s
Tests and linters / Staticcheck (push) Successful in 4m7s
Tests and linters / Lint (push) Successful in 4m11s
Tests and linters / Tests with -race (push) Successful in 4m46s
OCI image / Build container images (push) Successful in 4m30s
Background trees sync creates grpc connection with `grpc.WithDefaultCallOptions(grpc.WaitForReady(true))` option. When grpc connection created with this option, client will wait until a connection becomes available or the RPC's deadline is reached. As background sync has no timeout in context, so in case of client is in TRANSIENT_FAILURE RPC call will hang forever. Change-Id: I17c8c1d2779bb81c541f47dd0e558e0b8ed2e7c1 Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package tree
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
internalNet "git.frostfs.info/TrueCloudLab/frostfs-node/internal/net"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
|
"github.com/hashicorp/golang-lru/v2/simplelru"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/connectivity"
|
|
)
|
|
|
|
type clientCache struct {
|
|
sync.Mutex
|
|
simplelru.LRU[string, cacheItem]
|
|
key *ecdsa.PrivateKey
|
|
ds *internalNet.DialerSource
|
|
}
|
|
|
|
type cacheItem struct {
|
|
cc *grpc.ClientConn
|
|
lastTry time.Time
|
|
}
|
|
|
|
const (
|
|
defaultClientCacheSize = 32
|
|
defaultClientConnectTimeout = time.Second * 2
|
|
defaultReconnectInterval = time.Second * 15
|
|
)
|
|
|
|
var errRecentlyFailed = errors.New("client has recently failed")
|
|
|
|
func (c *clientCache) init(pk *ecdsa.PrivateKey, ds *internalNet.DialerSource) {
|
|
l, _ := simplelru.NewLRU(defaultClientCacheSize, func(_ string, value cacheItem) {
|
|
if conn := value.cc; conn != nil {
|
|
_ = conn.Close()
|
|
}
|
|
})
|
|
c.LRU = *l
|
|
c.key = pk
|
|
c.ds = ds
|
|
}
|
|
|
|
func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceClient, error) {
|
|
c.Lock()
|
|
ccInt, ok := c.Get(netmapAddr)
|
|
c.Unlock()
|
|
|
|
if ok {
|
|
item := ccInt
|
|
if item.cc == nil {
|
|
if d := time.Since(item.lastTry); d < defaultReconnectInterval {
|
|
return nil, fmt.Errorf("%w: %s till the next reconnection to %s",
|
|
errRecentlyFailed, d, netmapAddr)
|
|
}
|
|
} else {
|
|
if s := item.cc.GetState(); s == connectivity.Idle || s == connectivity.Ready {
|
|
return NewTreeServiceClient(item.cc), nil
|
|
}
|
|
_ = item.cc.Close()
|
|
}
|
|
}
|
|
|
|
var netAddr network.Address
|
|
if err := netAddr.FromString(netmapAddr); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cc, err := dialTreeService(ctx, netAddr, c.key, c.ds)
|
|
lastTry := time.Now()
|
|
|
|
c.Lock()
|
|
if err != nil {
|
|
c.Add(netmapAddr, cacheItem{cc: nil, lastTry: lastTry})
|
|
} else {
|
|
c.Add(netmapAddr, cacheItem{cc: cc, lastTry: lastTry})
|
|
}
|
|
c.Unlock()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewTreeServiceClient(cc), nil
|
|
}
|