2022-05-28 09:27:55 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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
|
|
|
|
|
|
|
"github.com/hashicorp/golang-lru/simplelru"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("skip connecting to %s (time since last error %s)",
|
|
|
|
netmapAddr, d)
|
|
|
|
}
|
|
|
|
} 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:") {
|
|
|
|
opts = append(opts, grpc.WithInsecure())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|