[#19] Prepare using connection pool
Signed-off-by: Pavel Korotkov <pavel@nspcc.ru>
This commit is contained in:
parent
3a5d9fe94c
commit
0b6e138270
3 changed files with 64 additions and 64 deletions
66
app.go
66
app.go
|
@ -22,10 +22,9 @@ type (
|
||||||
plant neofs.ClientPlant
|
plant neofs.ClientPlant
|
||||||
cfg *viper.Viper
|
cfg *viper.Viper
|
||||||
auxiliaryLog logger.Logger
|
auxiliaryLog logger.Logger
|
||||||
web *fasthttp.Server
|
webServer *fasthttp.Server
|
||||||
jobDone chan struct{}
|
jobDone chan struct{}
|
||||||
webDone chan struct{}
|
webDone chan struct{}
|
||||||
enableDefaultTimestamp bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
App interface {
|
App interface {
|
||||||
|
@ -59,59 +58,53 @@ func newApp(ctx context.Context, opt ...Option) App {
|
||||||
a := &app{
|
a := &app{
|
||||||
log: zap.L(),
|
log: zap.L(),
|
||||||
cfg: viper.GetViper(),
|
cfg: viper.GetViper(),
|
||||||
web: new(fasthttp.Server),
|
webServer: new(fasthttp.Server),
|
||||||
jobDone: make(chan struct{}),
|
jobDone: make(chan struct{}),
|
||||||
webDone: make(chan struct{}),
|
webDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
for i := range opt {
|
for i := range opt {
|
||||||
opt[i](a)
|
opt[i](a)
|
||||||
}
|
}
|
||||||
a.enableDefaultTimestamp = a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
|
|
||||||
a.auxiliaryLog = logger.GRPC(a.log)
|
a.auxiliaryLog = logger.GRPC(a.log)
|
||||||
|
|
||||||
if a.cfg.GetBool(cmdVerbose) {
|
if a.cfg.GetBool(cmdVerbose) {
|
||||||
grpclog.SetLoggerV2(a.auxiliaryLog)
|
grpclog.SetLoggerV2(a.auxiliaryLog)
|
||||||
}
|
}
|
||||||
|
|
||||||
// conTimeout := a.cfg.GetDuration(cfgConTimeout)
|
// conTimeout := a.cfg.GetDuration(cfgConTimeout)
|
||||||
// reqTimeout := a.cfg.GetDuration(cfgReqTimeout)
|
// reqTimeout := a.cfg.GetDuration(cfgReqTimeout)
|
||||||
// tckTimeout := a.cfg.GetDuration(cfgRebalance)
|
// tckTimeout := a.cfg.GetDuration(cfgRebalance)
|
||||||
|
|
||||||
// -- setup FastHTTP server --
|
// -- setup FastHTTP server --
|
||||||
a.web.Name = "neofs-http-gate"
|
a.webServer.Name = "neofs-http-gate"
|
||||||
a.web.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
a.webServer.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
||||||
a.web.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
|
a.webServer.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
|
||||||
a.web.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
a.webServer.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
||||||
a.web.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
a.webServer.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
||||||
a.web.DisableHeaderNamesNormalizing = true
|
a.webServer.DisableHeaderNamesNormalizing = true
|
||||||
a.web.NoDefaultServerHeader = true
|
a.webServer.NoDefaultServerHeader = true
|
||||||
a.web.NoDefaultContentType = true
|
a.webServer.NoDefaultContentType = true
|
||||||
a.web.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize)
|
a.webServer.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize)
|
||||||
|
|
||||||
// -- -- -- -- -- -- FIXME -- -- -- -- -- --
|
// -- -- -- -- -- -- FIXME -- -- -- -- -- --
|
||||||
// Does not work with StreamRequestBody,
|
// Does not work with StreamRequestBody,
|
||||||
// some bugs with readMultipartForm
|
// some bugs with readMultipartForm
|
||||||
// https://github.com/valyala/fasthttp/issues/968
|
// https://github.com/valyala/fasthttp/issues/968
|
||||||
a.web.DisablePreParseMultipartForm = true
|
a.webServer.DisablePreParseMultipartForm = true
|
||||||
a.web.StreamRequestBody = a.cfg.GetBool(cfgWebStreamRequestBody)
|
a.webServer.StreamRequestBody = a.cfg.GetBool(cfgWebStreamRequestBody)
|
||||||
// -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
var cl neofs.ConnectionList
|
||||||
var cl connectionList
|
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
address := a.cfg.GetString(cfgPeers + "." + strconv.Itoa(i) + ".address")
|
address := a.cfg.GetString(cfgPeers + "." + strconv.Itoa(i) + ".address")
|
||||||
weight := a.cfg.GetFloat64(cfgPeers + "." + strconv.Itoa(i) + ".weight")
|
weight := a.cfg.GetFloat64(cfgPeers + "." + strconv.Itoa(i) + ".weight")
|
||||||
if address == "" {
|
if address == "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
cl = append(cl, connection{address: address, weight: weight})
|
cl.Add(address, weight)
|
||||||
a.log.Info("add connection peer", zap.String("address", address), zap.Float64("weight", weight))
|
a.log.Info("add connection", zap.String("address", address), zap.Float64("weight", weight))
|
||||||
}
|
}
|
||||||
sort.Sort(sort.Reverse(cl))
|
sort.Sort(sort.Reverse(cl))
|
||||||
cred, err := neofs.NewCredentials(a.cfg.GetString(cmdNeoFSKey))
|
creds, err := neofs.NewCredentials(a.cfg.GetString(cmdNeoFSKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.log.Fatal("could not get credentials", zap.Error(err))
|
a.log.Fatal("could not get neofs credentials", zap.Error(err))
|
||||||
}
|
}
|
||||||
a.plant, err = neofs.NewClientPlant(ctx, cl[0].address, cred)
|
a.plant, err = neofs.NewClientPlant(ctx, cl, creds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.log.Fatal("failed to create neofs client")
|
a.log.Fatal("failed to create neofs client")
|
||||||
}
|
}
|
||||||
|
@ -120,7 +113,6 @@ func newApp(ctx context.Context, opt ...Option) App {
|
||||||
|
|
||||||
func (a *app) Wait() {
|
func (a *app) Wait() {
|
||||||
a.log.Info("starting application")
|
a.log.Info("starting application")
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-a.jobDone: // wait for job is stopped
|
case <-a.jobDone: // wait for job is stopped
|
||||||
<-a.webDone
|
<-a.webDone
|
||||||
|
@ -136,10 +128,11 @@ func (a *app) Worker(ctx context.Context) {
|
||||||
func (a *app) Serve(ctx context.Context) {
|
func (a *app) Serve(ctx context.Context) {
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
a.log.Info("shutting down web server", zap.Error(a.web.Shutdown()))
|
a.log.Info("shutting down web server", zap.Error(a.webServer.Shutdown()))
|
||||||
close(a.webDone)
|
close(a.webDone)
|
||||||
}()
|
}()
|
||||||
uploader := uploader.New(a.log, a.plant, a.enableDefaultTimestamp)
|
edts := a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
|
||||||
|
uploader := uploader.New(a.log, a.plant, edts)
|
||||||
downloader, err := downloader.New(ctx, a.log, a.plant)
|
downloader, err := downloader.New(ctx, a.log, a.plant)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.log.Fatal("failed to create downloader", zap.Error(err))
|
a.log.Fatal("failed to create downloader", zap.Error(err))
|
||||||
|
@ -167,19 +160,8 @@ func (a *app) Serve(ctx context.Context) {
|
||||||
}
|
}
|
||||||
bind := a.cfg.GetString(cfgListenAddress)
|
bind := a.cfg.GetString(cfgListenAddress)
|
||||||
a.log.Info("running web server", zap.String("address", bind))
|
a.log.Info("running web server", zap.String("address", bind))
|
||||||
a.web.Handler = r.Handler
|
a.webServer.Handler = r.Handler
|
||||||
if err := a.web.ListenAndServe(bind); err != nil {
|
if err := a.webServer.ListenAndServe(bind); err != nil {
|
||||||
a.log.Fatal("could not start server", zap.Error(err))
|
a.log.Fatal("could not start server", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type connection struct {
|
|
||||||
address string
|
|
||||||
weight float64
|
|
||||||
}
|
|
||||||
|
|
||||||
type connectionList []connection
|
|
||||||
|
|
||||||
func (p connectionList) Len() int { return len(p) }
|
|
||||||
func (p connectionList) Less(i, j int) bool { return p[i].weight < p[j].weight }
|
|
||||||
func (p connectionList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
||||||
|
|
|
@ -102,9 +102,27 @@ func (cc *neofsClient) OwnerID() *owner.ID {
|
||||||
return cc.ownerID
|
return cc.ownerID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientPlant(ctx context.Context, address string, creds Credentials) (ClientPlant, error) {
|
type Connection struct {
|
||||||
|
address string
|
||||||
|
weight float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConnectionList []Connection
|
||||||
|
|
||||||
|
func (p ConnectionList) Len() int { return len(p) }
|
||||||
|
func (p ConnectionList) Less(i, j int) bool { return p[i].weight < p[j].weight }
|
||||||
|
func (p ConnectionList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||||
|
|
||||||
|
func (cl *ConnectionList) Add(address string, weight float64) ConnectionList {
|
||||||
|
*cl = append(*cl, Connection{address, weight})
|
||||||
|
return *cl
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientPlant(ctx context.Context, connectionList ConnectionList, creds Credentials) (ClientPlant, error) {
|
||||||
toctx, c := context.WithTimeout(ctx, nodeConnectionTimeout)
|
toctx, c := context.WithTimeout(ctx, nodeConnectionTimeout)
|
||||||
defer c()
|
defer c()
|
||||||
|
// TODO: Use connection pool here.
|
||||||
|
address := connectionList[0].address
|
||||||
conn, err := grpc.DialContext(toctx, address, grpc.WithInsecure(), grpc.WithBlock())
|
conn, err := grpc.DialContext(toctx, address, grpc.WithInsecure(), grpc.WithBlock())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == context.DeadlineExceeded {
|
if err == context.DeadlineExceeded {
|
||||||
|
|
|
@ -16,9 +16,9 @@ type (
|
||||||
PrivateKey() *ecdsa.PrivateKey
|
PrivateKey() *ecdsa.PrivateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
cred struct {
|
credentials struct {
|
||||||
key *ecdsa.PrivateKey
|
key *ecdsa.PrivateKey
|
||||||
owner *owner.ID
|
ownerID *owner.ID
|
||||||
wif string
|
wif string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -34,26 +34,26 @@ func NewCredentials(secret string) (Credentials, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrivateKey returns ecdsa.PrivateKey.
|
// PrivateKey returns ecdsa.PrivateKey.
|
||||||
func (c *cred) PrivateKey() *ecdsa.PrivateKey {
|
func (c *credentials) PrivateKey() *ecdsa.PrivateKey {
|
||||||
return c.key
|
return c.key
|
||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey returns ecdsa.PublicKey.
|
// PublicKey returns ecdsa.PublicKey.
|
||||||
func (c *cred) PublicKey() *ecdsa.PublicKey {
|
func (c *credentials) PublicKey() *ecdsa.PublicKey {
|
||||||
return &c.key.PublicKey
|
return &c.key.PublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// Owner returns owner.ID.
|
// Owner returns owner.ID.
|
||||||
func (c *cred) Owner() *owner.ID {
|
func (c *credentials) Owner() *owner.ID {
|
||||||
return c.owner
|
return c.ownerID
|
||||||
}
|
}
|
||||||
|
|
||||||
// WIF returns string representation of WIF.
|
// WIF returns string representation of WIF.
|
||||||
func (c *cred) WIF() string {
|
func (c *credentials) WIF() string {
|
||||||
return c.wif
|
return c.wif
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFromPrivateKey(key *ecdsa.PrivateKey) (*cred, error) {
|
func setFromPrivateKey(key *ecdsa.PrivateKey) (*credentials, error) {
|
||||||
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -63,5 +63,5 @@ func setFromPrivateKey(key *ecdsa.PrivateKey) (*cred, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &cred{key: key, owner: ownerID, wif: wif}, nil
|
return &credentials{key: key, ownerID: ownerID, wif: wif}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue