2022-05-28 09:27:55 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-07 07:25:03 +00:00
|
|
|
"errors"
|
2022-06-06 11:46:00 +00:00
|
|
|
"fmt"
|
2022-05-30 17:33:54 +00:00
|
|
|
"strings"
|
2022-05-28 09:27:55 +00:00
|
|
|
"sync"
|
2022-05-28 13:48:25 +00:00
|
|
|
"time"
|
2022-05-28 09:27:55 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/network"
|
2022-05-28 09:27:55 +00:00
|
|
|
"github.com/hashicorp/golang-lru/simplelru"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
2022-07-30 07:24:16 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2022-05-28 09:27:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type clientCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
simplelru.LRU
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:46:00 +00:00
|
|
|
type cacheItem struct {
|
|
|
|
cc *grpc.ClientConn
|
|
|
|
lastTry time.Time
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:48:25 +00:00
|
|
|
const (
|
|
|
|
defaultClientCacheSize = 10
|
|
|
|
defaultClientConnectTimeout = time.Second * 2
|
2022-06-06 11:46:00 +00:00
|
|
|
defaultReconnectInterval = time.Second * 15
|
2022-05-28 13:48:25 +00:00
|
|
|
)
|
2022-05-28 09:27:55 +00:00
|
|
|
|
2022-09-07 07:25:03 +00:00
|
|
|
var errRecentlyFailed = errors.New("client has recently failed")
|
|
|
|
|
2022-05-28 09:27:55 +00:00
|
|
|
func (c *clientCache) init() {
|
|
|
|
l, _ := simplelru.NewLRU(defaultClientCacheSize, func(key, value interface{}) {
|
|
|
|
_ = value.(*grpc.ClientConn).Close()
|
|
|
|
})
|
|
|
|
c.LRU = *l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceClient, error) {
|
|
|
|
c.Lock()
|
|
|
|
ccInt, ok := c.LRU.Get(netmapAddr)
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
2022-06-06 11:46:00 +00:00
|
|
|
item := ccInt.(cacheItem)
|
|
|
|
if item.cc == nil {
|
|
|
|
if d := time.Since(item.lastTry); d < defaultReconnectInterval {
|
2022-09-07 07:25:03 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s till the next reconnection to %s",
|
|
|
|
errRecentlyFailed, d, netmapAddr)
|
2022-06-06 11:46:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s := item.cc.GetState(); s == connectivity.Idle || s == connectivity.Ready {
|
|
|
|
return NewTreeServiceClient(item.cc), nil
|
|
|
|
}
|
|
|
|
_ = item.cc.Close()
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, err := dialTreeService(ctx, netmapAddr)
|
2022-06-06 11:46:00 +00:00
|
|
|
lastTry := time.Now()
|
2022-05-28 09:27:55 +00:00
|
|
|
|
|
|
|
c.Lock()
|
2022-06-06 11:46:00 +00:00
|
|
|
if err != nil {
|
|
|
|
c.LRU.Add(netmapAddr, cacheItem{cc: nil, lastTry: lastTry})
|
|
|
|
} else {
|
|
|
|
c.LRU.Add(netmapAddr, cacheItem{cc: cc, lastTry: lastTry})
|
|
|
|
}
|
2022-05-28 09:27:55 +00:00
|
|
|
c.Unlock()
|
|
|
|
|
2022-06-06 11:46:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-28 09:27:55 +00:00
|
|
|
return NewTreeServiceClient(cc), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialTreeService(ctx context.Context, netmapAddr string) (*grpc.ClientConn, error) {
|
|
|
|
var netAddr network.Address
|
|
|
|
if err := netAddr.FromString(netmapAddr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-30 17:33:54 +00:00
|
|
|
opts := make([]grpc.DialOption, 1, 2)
|
|
|
|
opts[0] = grpc.WithBlock()
|
|
|
|
|
|
|
|
// FIXME(@fyrchik): ugly hack #1322
|
|
|
|
if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
|
2022-07-30 07:24:16 +00:00
|
|
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
2022-05-30 17:33:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 13:48:25 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout)
|
2022-05-30 17:33:54 +00:00
|
|
|
cc, err := grpc.DialContext(ctx, netAddr.URIAddr(), opts...)
|
2022-05-28 13:48:25 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
return cc, err
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|