2020-07-06 09:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-03 15:19:56 +00:00
|
|
|
"crypto/ecdsa"
|
2020-07-12 23:00:47 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-07-06 09:18:16 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-authmate/accessbox/hcs"
|
2020-08-06 12:02:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/handler"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/layer"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/pool"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/auth"
|
2020-07-06 09:18:16 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
2020-07-07 11:25:13 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
App struct {
|
2020-10-13 09:31:23 +00:00
|
|
|
cli pool.Pool
|
|
|
|
ctr *auth.Center
|
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
tls *tlsConfig
|
|
|
|
obj layer.Client
|
|
|
|
api api.Handler
|
2020-07-06 09:18:16 +00:00
|
|
|
|
|
|
|
conTimeout time.Duration
|
|
|
|
reqTimeout time.Duration
|
|
|
|
|
2020-07-12 23:00:47 +00:00
|
|
|
reBalance time.Duration
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
maxClients api.MaxClients
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
webDone chan struct{}
|
|
|
|
wrkDone chan struct{}
|
|
|
|
}
|
2020-07-13 15:50:11 +00:00
|
|
|
|
|
|
|
tlsConfig struct {
|
|
|
|
KeyFile string
|
|
|
|
CertFile string
|
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
func newApp(ctx context.Context, l *zap.Logger, v *viper.Viper) *App {
|
2020-07-06 09:18:16 +00:00
|
|
|
var (
|
2020-10-13 09:31:23 +00:00
|
|
|
err error
|
|
|
|
cli pool.Pool
|
|
|
|
tls *tlsConfig
|
|
|
|
caller api.Handler
|
|
|
|
ctr *auth.Center
|
|
|
|
obj layer.Client
|
|
|
|
|
|
|
|
gaKey *hcs.X25519Keys
|
|
|
|
nfKey *ecdsa.PrivateKey
|
|
|
|
|
2020-07-15 20:16:27 +00:00
|
|
|
reBalance = defaultRebalanceTimer
|
2020-07-06 09:18:16 +00:00
|
|
|
conTimeout = defaultConnectTimeout
|
|
|
|
reqTimeout = defaultRequestTimeout
|
2020-07-22 13:02:32 +00:00
|
|
|
|
|
|
|
maxClientsCount = defaultMaxClientsCount
|
|
|
|
maxClientsDeadline = defaultMaxClientsDeadline
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
2020-07-13 15:50:11 +00:00
|
|
|
|
2020-07-07 11:25:13 +00:00
|
|
|
if v := v.GetDuration(cfgConnectTimeout); v > 0 {
|
2020-07-06 09:18:16 +00:00
|
|
|
conTimeout = v
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:25:13 +00:00
|
|
|
if v := v.GetDuration(cfgRequestTimeout); v > 0 {
|
2020-07-06 09:18:16 +00:00
|
|
|
reqTimeout = v
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
if v := v.GetInt(cfgMaxClientsCount); v > 0 {
|
|
|
|
maxClientsCount = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := v.GetDuration(cfgMaxClientsDeadline); v > 0 {
|
|
|
|
maxClientsDeadline = v
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
if v := v.GetDuration(cfgRebalanceTimer); v > 0 {
|
|
|
|
reBalance = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if nfKey, err = fetchNeoFSKey(v); err != nil {
|
|
|
|
l.Fatal("could not load NeoFS private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if gaKey, err = fetchGateAuthKeys(v); err != nil {
|
|
|
|
l.Fatal("could not load gate auth key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet(cfgTLSKeyFile) && v.IsSet(cfgTLSCertFile) {
|
|
|
|
tls = &tlsConfig{
|
|
|
|
KeyFile: v.GetString(cfgTLSKeyFile),
|
|
|
|
CertFile: v.GetString(cfgTLSCertFile),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peers := fetchPeers(l, v)
|
|
|
|
|
2020-07-07 11:25:13 +00:00
|
|
|
poolConfig := &pool.Config{
|
2020-10-13 09:31:23 +00:00
|
|
|
ConnectTimeout: conTimeout,
|
|
|
|
RequestTimeout: reqTimeout,
|
2020-07-07 11:25:13 +00:00
|
|
|
ConnectionTTL: v.GetDuration(cfgConnectionTTL),
|
|
|
|
|
2020-08-06 11:56:40 +00:00
|
|
|
Peers: peers,
|
2020-07-07 11:25:13 +00:00
|
|
|
|
|
|
|
Logger: l,
|
2020-10-13 09:31:23 +00:00
|
|
|
PrivateKey: nfKey,
|
2020-07-07 11:25:13 +00:00
|
|
|
|
|
|
|
GRPCLogger: gRPCLogger(l),
|
|
|
|
GRPCVerbose: v.GetBool(cfgGRPCVerbose),
|
|
|
|
|
|
|
|
ClientParameters: keepalive.ClientParameters{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli, err = pool.New(poolConfig); err != nil {
|
2020-07-15 20:16:27 +00:00
|
|
|
l.Fatal("could not prepare pool connections", zap.Error(err))
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
{ // prepare auth center
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, conTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
params := &authCenterParams{
|
|
|
|
Logger: l,
|
|
|
|
Pool: cli,
|
|
|
|
|
|
|
|
Timeout: conTimeout,
|
|
|
|
|
|
|
|
GateAuthKeys: gaKey,
|
|
|
|
NeoFSPrivateKey: nfKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctr, err = fetchAuthCenter(ctx, params); err != nil {
|
|
|
|
l.Fatal("failed to initialize auth center", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
{ // should establish connection with NeoFS Storage Nodes
|
2020-10-13 09:31:23 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, conTimeout)
|
2020-07-06 09:18:16 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cli.ReBalance(ctx)
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
if _, err = cli.Connection(ctx); err != nil {
|
2020-07-06 09:18:16 +00:00
|
|
|
l.Fatal("could not establish connection",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
if obj, err = layer.NewLayer(l, cli, nfKey); err != nil {
|
2020-07-24 16:03:57 +00:00
|
|
|
l.Fatal("could not prepare ObjectLayer", zap.Error(err))
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:50:04 +00:00
|
|
|
if caller, err = handler.New(l, obj); err != nil {
|
|
|
|
l.Fatal("could not initialize API handler", zap.Error(err))
|
2020-07-24 16:10:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
return &App{
|
2020-10-13 09:31:23 +00:00
|
|
|
ctr: ctr,
|
|
|
|
cli: cli,
|
|
|
|
log: l,
|
|
|
|
cfg: v,
|
|
|
|
obj: obj,
|
|
|
|
tls: tls,
|
|
|
|
api: caller,
|
2020-07-06 09:18:16 +00:00
|
|
|
|
|
|
|
webDone: make(chan struct{}, 1),
|
|
|
|
wrkDone: make(chan struct{}, 1),
|
|
|
|
|
2020-07-12 23:00:47 +00:00
|
|
|
reBalance: reBalance,
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
maxClients: api.NewMaxClientsMiddleware(maxClientsCount, maxClientsDeadline),
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
conTimeout: conTimeout,
|
|
|
|
reqTimeout: reqTimeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 23:00:47 +00:00
|
|
|
func (a *App) Wait() {
|
2020-07-06 09:18:16 +00:00
|
|
|
a.log.Info("application started")
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
select {
|
|
|
|
case <-a.wrkDone: // wait for worker is stopped
|
|
|
|
<-a.webDone
|
|
|
|
case <-a.webDone: // wait for web-server is stopped
|
|
|
|
<-a.wrkDone
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
a.log.Info("application finished")
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Server(ctx context.Context) {
|
2020-07-12 23:00:47 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
lis net.Listener
|
|
|
|
lic net.ListenConfig
|
2020-07-13 09:51:21 +00:00
|
|
|
srv = new(http.Server)
|
2020-07-12 23:00:47 +00:00
|
|
|
addr = a.cfg.GetString(cfgListenAddress)
|
|
|
|
)
|
|
|
|
|
|
|
|
if lis, err = lic.Listen(ctx, "tcp", addr); err != nil {
|
|
|
|
a.log.Fatal("could not prepare listener",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:51:21 +00:00
|
|
|
router := newS3Router()
|
|
|
|
|
2020-07-12 23:00:47 +00:00
|
|
|
// Attach app-specific routes:
|
2020-07-13 09:51:21 +00:00
|
|
|
attachHealthy(router, a.cli)
|
|
|
|
attachMetrics(router, a.cfg, a.log)
|
|
|
|
attachProfiler(router, a.cfg, a.log)
|
|
|
|
|
|
|
|
// Attach S3 API:
|
2020-10-13 09:31:23 +00:00
|
|
|
api.Attach(router, a.maxClients, a.api, a.ctr, a.log)
|
2020-07-13 09:51:21 +00:00
|
|
|
|
|
|
|
// Use mux.Router as http.Handler
|
|
|
|
srv.Handler = router
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
a.log.Info("starting server",
|
|
|
|
zap.String("bind", addr))
|
|
|
|
|
2020-07-13 15:50:11 +00:00
|
|
|
switch a.tls {
|
|
|
|
case nil:
|
|
|
|
if err = srv.Serve(lis); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.log.Fatal("listen and serve",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
a.log.Info("using certificate",
|
|
|
|
zap.String("key", a.tls.KeyFile),
|
|
|
|
zap.String("cert", a.tls.CertFile))
|
2020-07-13 11:23:23 +00:00
|
|
|
|
2020-07-13 15:50:11 +00:00
|
|
|
if err = srv.ServeTLS(lis, a.tls.CertFile, a.tls.KeyFile); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.log.Fatal("listen and serve",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
}()
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
a.log.Info("stopping server",
|
|
|
|
zap.Error(srv.Shutdown(ctx)))
|
|
|
|
|
|
|
|
close(a.webDone)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Worker(ctx context.Context) {
|
2020-07-12 23:00:47 +00:00
|
|
|
tick := time.NewTimer(a.reBalance)
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break loop
|
|
|
|
case <-tick.C:
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, a.conTimeout)
|
|
|
|
a.cli.ReBalance(ctx)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
tick.Reset(a.reBalance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tick.Stop()
|
|
|
|
a.cli.Close()
|
|
|
|
a.log.Info("stopping worker")
|
|
|
|
close(a.wrkDone)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|