2020-07-06 09:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-22 13:25:29 +00:00
|
|
|
"encoding/hex"
|
2023-09-08 11:17:14 +00:00
|
|
|
"encoding/xml"
|
2023-10-17 14:49:57 +00:00
|
|
|
"errors"
|
2022-04-25 09:57:58 +00:00
|
|
|
"fmt"
|
2023-09-08 11:17:14 +00:00
|
|
|
"io"
|
2023-10-17 14:49:57 +00:00
|
|
|
"net"
|
2020-07-12 23:00:47 +00:00
|
|
|
"net/http"
|
2022-09-12 09:45:54 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-08-30 18:24:22 +00:00
|
|
|
"runtime/debug"
|
2022-09-12 12:30:51 +00:00
|
|
|
"sync"
|
2022-09-12 09:45:54 +00:00
|
|
|
"syscall"
|
2021-09-08 07:08:56 +00:00
|
|
|
"time"
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2023-11-23 07:44:50 +00:00
|
|
|
v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
2023-06-09 06:34:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-06-14 08:50:22 +00:00
|
|
|
grpctracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/handler"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-10-05 08:05:21 +00:00
|
|
|
s3middleware "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/notifications"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/resolver"
|
2024-02-06 13:44:49 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/tokens"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs"
|
2023-10-05 13:25:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/frostfsid"
|
2024-03-12 08:32:51 +00:00
|
|
|
ffidcontract "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/frostfsid/contract"
|
2023-12-05 09:12:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/policy"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/policy/contract"
|
2023-04-03 11:04:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/version"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/wallet"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/metrics"
|
2023-10-17 14:49:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/control"
|
|
|
|
controlSvc "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/control/server"
|
2023-03-14 14:31:15 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/tree"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
2023-06-09 06:34:04 +00:00
|
|
|
treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree"
|
2023-08-03 12:08:22 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2023-07-05 14:05:45 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2021-06-22 14:11:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-07-06 09:18:16 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
2023-11-23 07:44:50 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2023-03-14 14:31:15 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
2023-09-08 11:17:14 +00:00
|
|
|
const awsDefaultNamespace = "http://s3.amazonaws.com/doc/2006-03-01/"
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
type (
|
2021-05-13 20:25:31 +00:00
|
|
|
// App is the main application structure.
|
2020-07-06 09:18:16 +00:00
|
|
|
App struct {
|
2023-10-05 08:05:21 +00:00
|
|
|
ctr s3middleware.Center
|
2023-06-09 06:34:04 +00:00
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
pool *pool.Pool
|
|
|
|
treePool *treepool.Pool
|
|
|
|
key *keys.PrivateKey
|
|
|
|
nc *notifications.Controller
|
|
|
|
obj layer.Client
|
|
|
|
api api.Handler
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
frostfsid *frostfsid.FrostFSID
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
policyStorage *policy.Storage
|
2023-11-27 14:55:55 +00:00
|
|
|
|
2024-02-11 18:00:56 +00:00
|
|
|
servers []Server
|
|
|
|
unbindServers []ServerInfo
|
|
|
|
mu sync.RWMutex
|
2022-11-09 10:07:18 +00:00
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
controlAPI *grpc.Server
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
metrics *metrics.AppMetrics
|
2022-09-12 13:46:55 +00:00
|
|
|
bucketResolver *resolver.BucketResolver
|
2022-09-12 14:30:41 +00:00
|
|
|
services []*Service
|
|
|
|
settings *appSettings
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
webDone chan struct{}
|
|
|
|
wrkDone chan struct{}
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
appSettings struct {
|
2023-12-13 14:44:18 +00:00
|
|
|
logLevel zap.AtomicLevel
|
|
|
|
maxClient maxClientsConfig
|
|
|
|
defaultMaxAge int
|
2024-02-11 18:00:56 +00:00
|
|
|
reconnectInterval time.Duration
|
2023-12-13 14:44:18 +00:00
|
|
|
notificatorEnabled bool
|
|
|
|
resolveZoneList []string
|
|
|
|
isResolveListAllow bool // True if ResolveZoneList contains allowed zones
|
|
|
|
frostfsidValidation bool
|
2023-09-08 11:17:14 +00:00
|
|
|
|
2023-10-09 12:34:51 +00:00
|
|
|
mu sync.RWMutex
|
2023-11-21 08:51:07 +00:00
|
|
|
namespaces Namespaces
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS bool
|
|
|
|
bypassContentEncodingInChunks bool
|
|
|
|
clientCut bool
|
|
|
|
maxBufferSizeForPut uint64
|
2023-10-02 08:52:07 +00:00
|
|
|
md5Enabled bool
|
2024-02-14 14:04:35 +00:00
|
|
|
aclEnabled bool
|
2023-11-16 12:10:51 +00:00
|
|
|
namespaceHeader string
|
|
|
|
defaultNamespaces []string
|
2023-10-17 14:49:57 +00:00
|
|
|
authorizedControlAPIKeys [][]byte
|
2023-12-05 12:49:13 +00:00
|
|
|
policyDenyByDefault bool
|
2023-07-05 14:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
maxClientsConfig struct {
|
|
|
|
deadline time.Duration
|
|
|
|
count int
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
lvl zap.AtomicLevel
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
func newApp(ctx context.Context, log *Logger, v *viper.Viper) *App {
|
2023-06-09 06:34:04 +00:00
|
|
|
objPool, treePool, key := getPools(ctx, log.logger, v)
|
2020-10-13 09:31:23 +00:00
|
|
|
|
2024-02-06 13:44:49 +00:00
|
|
|
cfg := tokens.Config{
|
|
|
|
FrostFS: frostfs.NewAuthmateFrostFS(objPool, key),
|
|
|
|
Key: key,
|
|
|
|
CacheConfig: getAccessBoxCacheConfig(v, log.logger),
|
|
|
|
RemovingCheckAfterDurations: fetchRemovingCheckInterval(v, log.logger),
|
|
|
|
}
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
// prepare auth center
|
2024-02-06 13:44:49 +00:00
|
|
|
ctr := auth.New(tokens.New(cfg), v.GetStringSlice(cfgAllowedAccessKeyIDPrefixes))
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
app := &App{
|
2023-06-09 06:34:04 +00:00
|
|
|
ctr: ctr,
|
|
|
|
log: log.logger,
|
|
|
|
cfg: v,
|
|
|
|
pool: objPool,
|
|
|
|
treePool: treePool,
|
|
|
|
key: key,
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
webDone: make(chan struct{}, 1),
|
|
|
|
wrkDone: make(chan struct{}, 1),
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
settings: newAppSettings(log, v, key),
|
2020-10-13 09:31:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
app.init(ctx)
|
2022-07-29 06:26:11 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
return app
|
|
|
|
}
|
2020-10-13 09:31:23 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
func (a *App) init(ctx context.Context) {
|
2023-08-30 18:24:22 +00:00
|
|
|
a.setRuntimeParameters()
|
2023-12-08 07:44:13 +00:00
|
|
|
a.initFrostfsID(ctx)
|
2023-12-05 09:12:35 +00:00
|
|
|
a.initPolicyStorage(ctx)
|
2023-12-08 07:44:13 +00:00
|
|
|
a.initAPI(ctx)
|
2022-09-12 14:30:41 +00:00
|
|
|
a.initMetrics()
|
2023-12-08 07:44:13 +00:00
|
|
|
a.initControlAPI()
|
2022-11-09 10:07:18 +00:00
|
|
|
a.initServers(ctx)
|
2023-05-31 16:35:20 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
2020-11-24 07:09:58 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
func (a *App) initLayer(ctx context.Context) {
|
|
|
|
a.initResolver()
|
2022-04-07 14:36:44 +00:00
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
// prepare random key for anonymous requests
|
|
|
|
randomKey, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.CouldntGenerateRandomKey, zap.Error(err))
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 12:08:22 +00:00
|
|
|
var gateOwner user.ID
|
|
|
|
user.IDFromKey(&gateOwner, a.key.PrivateKey.PublicKey)
|
|
|
|
|
2021-11-22 09:16:05 +00:00
|
|
|
layerCfg := &layer.Config{
|
2024-01-22 09:33:59 +00:00
|
|
|
Cache: layer.NewCache(getCacheOptions(a.cfg, a.log)),
|
2021-11-22 09:16:05 +00:00
|
|
|
AnonKey: layer.AnonymousKey{
|
|
|
|
Key: randomKey,
|
|
|
|
},
|
2023-08-03 12:08:22 +00:00
|
|
|
GateOwner: gateOwner,
|
2022-09-12 14:30:41 +00:00
|
|
|
Resolver: a.bucketResolver,
|
2023-06-09 06:34:04 +00:00
|
|
|
TreeService: tree.NewTree(services.NewPoolWrapper(a.treePool), a.log),
|
2023-07-14 12:30:47 +00:00
|
|
|
Features: a.settings,
|
2021-11-22 09:16:05 +00:00
|
|
|
}
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
// prepare object layer
|
2023-08-03 12:08:22 +00:00
|
|
|
a.obj = layer.NewLayer(a.log, frostfs.NewFrostFS(a.pool, a.key), layerCfg)
|
2020-11-24 07:09:58 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
if a.cfg.GetBool(cfgEnableNATS) {
|
|
|
|
nopts := getNotificationsOptions(a.cfg, a.log)
|
|
|
|
a.nc, err = notifications.NewController(nopts, a.log)
|
2022-03-25 11:13:54 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.FailedToEnableNotifications, zap.Error(err))
|
2022-03-25 11:13:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
if err = a.obj.Initialize(ctx, a.nc); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.CouldntInitializeLayer, zap.Error(err))
|
2022-03-25 11:13:54 +00:00
|
|
|
}
|
2022-03-05 06:58:54 +00:00
|
|
|
}
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
2022-03-05 06:58:54 +00:00
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
func newAppSettings(log *Logger, v *viper.Viper, key *keys.PrivateKey) *appSettings {
|
2023-07-10 09:17:44 +00:00
|
|
|
settings := &appSettings{
|
2023-12-13 14:44:18 +00:00
|
|
|
logLevel: log.lvl,
|
|
|
|
maxClient: newMaxClients(v),
|
|
|
|
defaultMaxAge: fetchDefaultMaxAge(v, log.logger),
|
2024-02-11 18:00:56 +00:00
|
|
|
reconnectInterval: fetchReconnectInterval(v),
|
2023-12-13 14:44:18 +00:00
|
|
|
notificatorEnabled: v.GetBool(cfgEnableNATS),
|
|
|
|
frostfsidValidation: v.GetBool(cfgFrostfsIDValidationEnabled),
|
2023-09-08 11:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
settings.resolveZoneList = v.GetStringSlice(cfgResolveBucketAllow)
|
|
|
|
settings.isResolveListAllow = len(settings.resolveZoneList) > 0
|
|
|
|
if !settings.isResolveListAllow {
|
|
|
|
settings.resolveZoneList = v.GetStringSlice(cfgResolveBucketDeny)
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
2023-07-10 09:17:44 +00:00
|
|
|
|
2023-12-14 13:21:09 +00:00
|
|
|
settings.update(v, log.logger, key)
|
2023-07-10 09:17:44 +00:00
|
|
|
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:21:09 +00:00
|
|
|
func (s *appSettings) update(v *viper.Viper, log *zap.Logger, key *keys.PrivateKey) {
|
2024-02-09 14:38:14 +00:00
|
|
|
s.updateNamespacesSettings(v, log)
|
2023-12-14 13:21:09 +00:00
|
|
|
s.useDefaultXMLNamespace(v.GetBool(cfgKludgeUseDefaultXMLNS))
|
2024-02-14 14:04:35 +00:00
|
|
|
s.setACLEnabled(v.GetBool(cfgKludgeACLEnabled))
|
2023-12-14 13:21:09 +00:00
|
|
|
s.setBypassContentEncodingInChunks(v.GetBool(cfgKludgeBypassContentEncodingCheckInChunks))
|
|
|
|
s.setClientCut(v.GetBool(cfgClientCut))
|
|
|
|
s.setBufferMaxSizeForPut(v.GetUint64(cfgBufferMaxSizeForPut))
|
|
|
|
s.setMD5Enabled(v.GetBool(cfgMD5Enabled))
|
|
|
|
s.setAuthorizedControlAPIKeys(append(fetchAuthorizedKeys(log, v), key.PublicKey()))
|
|
|
|
s.setPolicyDenyByDefault(v.GetBool(cfgPolicyDenyByDefault))
|
|
|
|
}
|
|
|
|
|
2024-02-09 14:38:14 +00:00
|
|
|
func (s *appSettings) updateNamespacesSettings(v *viper.Viper, log *zap.Logger) {
|
|
|
|
nsHeader := v.GetString(cfgResolveNamespaceHeader)
|
|
|
|
nsConfig, defaultNamespaces := fetchNamespacesConfig(log, v)
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.namespaceHeader = nsHeader
|
|
|
|
s.defaultNamespaces = defaultNamespaces
|
|
|
|
s.namespaces = nsConfig.Namespaces
|
|
|
|
}
|
|
|
|
|
2023-07-10 09:17:44 +00:00
|
|
|
func (s *appSettings) BypassContentEncodingInChunks() bool {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.bypassContentEncodingInChunks
|
2023-07-10 09:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) setBypassContentEncodingInChunks(bypass bool) {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
s.bypassContentEncodingInChunks = bypass
|
|
|
|
s.mu.Unlock()
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2023-07-14 12:30:47 +00:00
|
|
|
func (s *appSettings) ClientCut() bool {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.clientCut
|
2023-07-14 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) setClientCut(clientCut bool) {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
s.clientCut = clientCut
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-08-25 10:07:59 +00:00
|
|
|
func (s *appSettings) BufferMaxSizeForPut() uint64 {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.maxBufferSizeForPut
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) setBufferMaxSizeForPut(size uint64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.maxBufferSizeForPut = size
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
func (s *appSettings) DefaultPlacementPolicy(namespace string) netmap.PlacementPolicy {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2023-11-21 08:51:07 +00:00
|
|
|
return s.namespaces[namespace].LocationConstraints[defaultConstraintName]
|
2023-09-08 11:17:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
func (s *appSettings) PlacementPolicy(namespace, constraint string) (netmap.PlacementPolicy, bool) {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RLock()
|
2023-12-05 09:12:35 +00:00
|
|
|
placementPolicy, ok := s.namespaces[namespace].LocationConstraints[constraint]
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RUnlock()
|
|
|
|
|
2023-12-05 09:12:35 +00:00
|
|
|
return placementPolicy, ok
|
2023-09-08 11:17:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
func (s *appSettings) CopiesNumbers(namespace, constraint string) ([]uint32, bool) {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RLock()
|
2023-11-21 08:51:07 +00:00
|
|
|
copiesNumbers, ok := s.namespaces[namespace].CopiesNumbers[constraint]
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
return copiesNumbers, ok
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
func (s *appSettings) DefaultCopiesNumbers(namespace string) []uint32 {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2023-11-21 08:51:07 +00:00
|
|
|
return s.namespaces[namespace].CopiesNumbers[defaultConstraintName]
|
2023-09-08 11:17:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 12:34:51 +00:00
|
|
|
func (s *appSettings) NewXMLDecoder(r io.Reader) *xml.Decoder {
|
2023-09-08 11:17:14 +00:00
|
|
|
dec := xml.NewDecoder(r)
|
|
|
|
|
|
|
|
s.mu.RLock()
|
2023-10-09 12:34:51 +00:00
|
|
|
if s.defaultXMLNS {
|
2023-09-08 11:17:14 +00:00
|
|
|
dec.DefaultSpace = awsDefaultNamespace
|
|
|
|
}
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
return dec
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:34:51 +00:00
|
|
|
func (s *appSettings) useDefaultXMLNamespace(useDefaultNamespace bool) {
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.Lock()
|
2023-10-09 12:34:51 +00:00
|
|
|
s.defaultXMLNS = useDefaultNamespace
|
2023-09-08 11:17:14 +00:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) DefaultMaxAge() int {
|
|
|
|
return s.defaultMaxAge
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) NotificatorEnabled() bool {
|
|
|
|
return s.notificatorEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) ResolveZoneList() []string {
|
|
|
|
return s.resolveZoneList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) IsResolveListAllow() bool {
|
|
|
|
return s.isResolveListAllow
|
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
func (s *appSettings) MD5Enabled() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.md5Enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) setMD5Enabled(md5Enabled bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.md5Enabled = md5Enabled
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:04:35 +00:00
|
|
|
func (s *appSettings) setACLEnabled(enableACL bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.aclEnabled = enableACL
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) ACLEnabled() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.aclEnabled
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:10:51 +00:00
|
|
|
func (s *appSettings) NamespaceHeader() string {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.namespaceHeader
|
|
|
|
}
|
|
|
|
|
2023-11-23 07:44:50 +00:00
|
|
|
func (s *appSettings) FormContainerZone(ns string) (zone string, isDefault bool) {
|
2024-02-09 14:38:14 +00:00
|
|
|
if len(ns) == 0 {
|
2023-11-23 07:44:50 +00:00
|
|
|
return v2container.SysAttributeZoneDefault, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns + ".ns", false
|
2023-11-16 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 14:38:14 +00:00
|
|
|
func (s *appSettings) isDefaultNamespace(ns string) bool {
|
2023-11-30 08:25:05 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
namespaces := s.defaultNamespaces
|
|
|
|
s.mu.RUnlock()
|
|
|
|
return slices.Contains(namespaces, ns)
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
func (s *appSettings) FetchRawKeys() [][]byte {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.authorizedControlAPIKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) setAuthorizedControlAPIKeys(keys keys.PublicKeys) {
|
|
|
|
rawPubs := make([][]byte, len(keys))
|
|
|
|
for i := range keys {
|
|
|
|
rawPubs[i] = keys[i].Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
s.authorizedControlAPIKeys = rawPubs
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-12-01 11:16:19 +00:00
|
|
|
func (s *appSettings) ResolveNamespaceAlias(namespace string) string {
|
2024-02-09 14:38:14 +00:00
|
|
|
if s.isDefaultNamespace(namespace) {
|
2023-11-30 08:25:05 +00:00
|
|
|
return defaultNamespace
|
2023-12-01 11:16:19 +00:00
|
|
|
}
|
2023-11-30 08:25:05 +00:00
|
|
|
|
2023-12-01 11:16:19 +00:00
|
|
|
return namespace
|
|
|
|
}
|
|
|
|
|
2023-12-05 12:49:13 +00:00
|
|
|
func (s *appSettings) PolicyDenyByDefault() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.policyDenyByDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) setPolicyDenyByDefault(policyDenyByDefault bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.policyDenyByDefault = policyDenyByDefault
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
func (a *App) initAPI(ctx context.Context) {
|
|
|
|
a.initLayer(ctx)
|
|
|
|
a.initHandler()
|
2022-09-12 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
func (a *App) initControlAPI() {
|
|
|
|
svc := controlSvc.New(
|
2023-11-30 08:25:05 +00:00
|
|
|
controlSvc.WithSettings(a.settings),
|
2023-10-17 14:49:57 +00:00
|
|
|
controlSvc.WithLogger(a.log),
|
2023-12-05 09:12:35 +00:00
|
|
|
controlSvc.WithChainStorage(a.policyStorage.LocalStorage()),
|
2023-10-17 14:49:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
a.controlAPI = grpc.NewServer()
|
|
|
|
|
|
|
|
control.RegisterControlServiceServer(a.controlAPI, svc)
|
|
|
|
}
|
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
func (a *App) initMetrics() {
|
2024-02-26 13:18:34 +00:00
|
|
|
cfg := metrics.AppMetricsConfig{
|
|
|
|
Logger: a.log,
|
|
|
|
PoolStatistics: frostfs.NewPoolStatistic(a.pool),
|
|
|
|
Enabled: a.cfg.GetBool(cfgPrometheusEnabled),
|
|
|
|
}
|
|
|
|
|
|
|
|
a.metrics = metrics.NewAppMetrics(cfg)
|
2023-04-07 14:28:21 +00:00
|
|
|
a.metrics.State().SetHealth(metrics.HealthStatusStarting)
|
2022-09-12 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
func (a *App) initFrostfsID(ctx context.Context) {
|
2024-03-12 08:32:51 +00:00
|
|
|
cli, err := ffidcontract.New(ctx, ffidcontract.Config{
|
2023-12-21 14:57:12 +00:00
|
|
|
RPCAddress: a.cfg.GetString(cfgRPCEndpoint),
|
|
|
|
Contract: a.cfg.GetString(cfgFrostfsIDContract),
|
|
|
|
ProxyContract: a.cfg.GetString(cfgProxyContract),
|
|
|
|
Key: a.key,
|
2023-10-05 13:25:25 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal(logs.InitFrostfsIDContractFailed, zap.Error(err))
|
|
|
|
}
|
2024-03-12 08:32:51 +00:00
|
|
|
|
|
|
|
a.frostfsid, err = frostfsid.NewFrostFSID(frostfsid.Config{
|
|
|
|
Cache: cache.NewFrostfsIDCache(getFrostfsIDCacheConfig(a.cfg, a.log)),
|
|
|
|
FrostFSID: cli,
|
|
|
|
Logger: a.log,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal(logs.InitFrostfsIDContractFailed, zap.Error(err))
|
|
|
|
}
|
2023-10-05 13:25:25 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 09:12:35 +00:00
|
|
|
func (a *App) initPolicyStorage(ctx context.Context) {
|
2024-02-19 08:45:18 +00:00
|
|
|
policyContract, err := contract.New(ctx, contract.Config{
|
|
|
|
RPCAddress: a.cfg.GetString(cfgRPCEndpoint),
|
|
|
|
Contract: a.cfg.GetString(cfgPolicyContract),
|
|
|
|
ProxyContract: a.cfg.GetString(cfgProxyContract),
|
|
|
|
Key: a.key,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal(logs.InitPolicyContractFailed, zap.Error(err))
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
a.policyStorage = policy.NewStorage(policy.StorageConfig{
|
|
|
|
Contract: policyContract,
|
|
|
|
Cache: cache.NewMorphPolicyCache(getMorphPolicyCacheConfig(a.cfg, a.log)),
|
|
|
|
Log: a.log,
|
2023-12-05 09:12:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
func (a *App) initResolver() {
|
|
|
|
var err error
|
2023-10-05 13:25:25 +00:00
|
|
|
a.bucketResolver, err = resolver.NewBucketResolver(a.getResolverOrder(), a.getResolverConfig())
|
2022-09-12 13:46:55 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.FailedToCreateResolver, zap.Error(err))
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
func (a *App) getResolverConfig() *resolver.Config {
|
|
|
|
return &resolver.Config{
|
2022-12-20 08:38:58 +00:00
|
|
|
FrostFS: frostfs.NewResolverFrostFS(a.pool),
|
2022-09-12 13:46:55 +00:00
|
|
|
RPCAddress: a.cfg.GetString(cfgRPCEndpoint),
|
2023-11-16 12:10:51 +00:00
|
|
|
Settings: a.settings,
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
2023-10-05 13:25:25 +00:00
|
|
|
}
|
2022-09-12 13:46:55 +00:00
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
func (a *App) getResolverOrder() []string {
|
2022-09-12 13:46:55 +00:00
|
|
|
order := a.cfg.GetStringSlice(cfgResolveOrder)
|
2023-10-05 13:25:25 +00:00
|
|
|
if a.cfg.GetString(cfgRPCEndpoint) == "" {
|
2022-09-12 13:46:55 +00:00
|
|
|
order = remove(order, resolver.NNSResolver)
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.ResolverNNSWontBeUsedSinceRPCEndpointIsntProvided)
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(order) == 0 {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.ContainerResolverWillBeDisabled)
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
return order
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:20 +00:00
|
|
|
func (a *App) initTracing(ctx context.Context) {
|
|
|
|
instanceID := ""
|
|
|
|
if len(a.servers) > 0 {
|
|
|
|
instanceID = a.servers[0].Address()
|
|
|
|
}
|
|
|
|
cfg := tracing.Config{
|
|
|
|
Enabled: a.cfg.GetBool(cfgTracingEnabled),
|
|
|
|
Exporter: tracing.Exporter(a.cfg.GetString(cfgTracingExporter)),
|
|
|
|
Endpoint: a.cfg.GetString(cfgTracingEndpoint),
|
|
|
|
Service: "frostfs-s3-gw",
|
|
|
|
InstanceID: instanceID,
|
|
|
|
Version: version.Version,
|
|
|
|
}
|
|
|
|
updated, err := tracing.Setup(ctx, cfg)
|
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToInitializeTracing, zap.Error(err))
|
2023-05-31 16:35:20 +00:00
|
|
|
}
|
|
|
|
if updated {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.TracingConfigUpdated)
|
2023-05-31 16:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) shutdownTracing() {
|
|
|
|
const tracingShutdownTimeout = 5 * time.Second
|
|
|
|
shdnCtx, cancel := context.WithTimeout(context.Background(), tracingShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := tracing.Shutdown(shdnCtx); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToShutdownTracing, zap.Error(err))
|
2023-05-31 16:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
func newMaxClients(cfg *viper.Viper) maxClientsConfig {
|
|
|
|
config := maxClientsConfig{}
|
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
config.count = fetchMaxClientsCount(cfg)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
config.deadline = fetchMaxClientsDeadline(cfg)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
return config
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 06:34:04 +00:00
|
|
|
func getPools(ctx context.Context, logger *zap.Logger, cfg *viper.Viper) (*pool.Pool, *treepool.Pool, *keys.PrivateKey) {
|
2022-09-12 14:30:41 +00:00
|
|
|
var prm pool.InitParameters
|
2023-06-09 06:34:04 +00:00
|
|
|
var prmTree treepool.InitParameters
|
2022-09-12 14:30:41 +00:00
|
|
|
|
|
|
|
password := wallet.GetPassword(cfg, cfgWalletPassphrase)
|
|
|
|
key, err := wallet.GetKeyFromPath(cfg.GetString(cfgWalletPath), cfg.GetString(cfgWalletAddress), password)
|
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
logger.Fatal(logs.CouldNotLoadFrostFSPrivateKey, zap.Error(err))
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm.SetKey(&key.PrivateKey)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetKey(key)
|
2023-08-23 11:07:52 +00:00
|
|
|
logger.Info(logs.UsingCredentials, zap.String("FrostFS", hex.EncodeToString(key.PublicKey().Bytes())))
|
2022-09-12 14:30:41 +00:00
|
|
|
|
|
|
|
for _, peer := range fetchPeers(logger, cfg) {
|
|
|
|
prm.AddNode(peer)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.AddNode(peer)
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
connTimeout := fetchConnectTimeout(cfg)
|
2022-09-12 14:30:41 +00:00
|
|
|
prm.SetNodeDialTimeout(connTimeout)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetNodeDialTimeout(connTimeout)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
streamTimeout := fetchStreamTimeout(cfg)
|
2022-11-15 14:19:21 +00:00
|
|
|
prm.SetNodeStreamTimeout(streamTimeout)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetNodeStreamTimeout(streamTimeout)
|
2022-11-15 14:19:21 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
healthCheckTimeout := fetchHealthCheckTimeout(cfg)
|
2022-11-15 14:19:21 +00:00
|
|
|
prm.SetHealthcheckTimeout(healthCheckTimeout)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetHealthcheckTimeout(healthCheckTimeout)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
rebalanceInterval := fetchRebalanceInterval(cfg)
|
2022-09-12 14:30:41 +00:00
|
|
|
prm.SetClientRebalanceInterval(rebalanceInterval)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetClientRebalanceInterval(rebalanceInterval)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
errorThreshold := fetchErrorThreshold(cfg)
|
2022-09-12 14:30:41 +00:00
|
|
|
prm.SetErrorThreshold(errorThreshold)
|
2023-01-23 08:19:06 +00:00
|
|
|
prm.SetLogger(logger)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetLogger(logger)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-10-23 14:06:42 +00:00
|
|
|
prmTree.SetMaxRequestAttempts(cfg.GetInt(cfgTreePoolMaxAttempts))
|
|
|
|
|
2023-06-14 08:50:22 +00:00
|
|
|
var apiGRPCDialOpts []grpc.DialOption
|
2023-07-07 13:36:36 +00:00
|
|
|
var treeGRPCDialOpts []grpc.DialOption
|
2023-06-14 08:50:22 +00:00
|
|
|
if cfg.GetBool(cfgTracingEnabled) {
|
|
|
|
interceptors := []grpc.DialOption{
|
|
|
|
grpc.WithUnaryInterceptor(grpctracing.NewUnaryClientInteceptor()),
|
|
|
|
grpc.WithStreamInterceptor(grpctracing.NewStreamClientInterceptor()),
|
|
|
|
}
|
|
|
|
treeGRPCDialOpts = append(treeGRPCDialOpts, interceptors...)
|
|
|
|
apiGRPCDialOpts = append(apiGRPCDialOpts, interceptors...)
|
|
|
|
}
|
|
|
|
prm.SetGRPCDialOptions(apiGRPCDialOpts...)
|
|
|
|
prmTree.SetGRPCDialOptions(treeGRPCDialOpts...)
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
p, err := pool.NewPool(prm)
|
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
logger.Fatal(logs.FailedToCreateConnectionPool, zap.Error(err))
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.Dial(ctx); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
logger.Fatal(logs.FailedToDialConnectionPool, zap.Error(err))
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 06:34:04 +00:00
|
|
|
treePool, err := treepool.NewPool(prmTree)
|
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
logger.Fatal(logs.FailedToCreateTreePool, zap.Error(err))
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
if err = treePool.Dial(ctx); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
logger.Fatal(logs.FailedToDialTreePool, zap.Error(err))
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p, treePool, key
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
func remove(list []string, element string) []string {
|
|
|
|
for i, item := range list {
|
|
|
|
if item == element {
|
|
|
|
return append(list[:i], list[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Wait waits for an application to finish.
|
2022-03-29 09:39:49 +00:00
|
|
|
//
|
|
|
|
// Pre-logs a message about the launch of the application mentioning its
|
2022-12-20 08:38:58 +00:00
|
|
|
// version (version.Version) and its name (frostfs-s3-gw). At the end, it writes
|
2022-04-13 16:56:58 +00:00
|
|
|
// about the stop to the log.
|
2020-07-12 23:00:47 +00:00
|
|
|
func (a *App) Wait() {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.ApplicationStarted,
|
2022-12-15 14:08:25 +00:00
|
|
|
zap.String("name", "frostfs-s3-gw"),
|
2022-03-29 09:39:49 +00:00
|
|
|
zap.String("version", version.Version),
|
|
|
|
)
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
a.metrics.State().SetVersion(version.Version)
|
2022-09-12 12:30:51 +00:00
|
|
|
a.setHealthStatus()
|
2022-07-21 14:42:24 +00:00
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
<-a.webDone // wait for web-server to be stopped
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.ApplicationFinished)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func (a *App) setHealthStatus() {
|
2023-04-07 14:28:21 +00:00
|
|
|
a.metrics.State().SetHealth(metrics.HealthStatusReady)
|
2022-09-12 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
// Serve runs HTTP server to handle S3 API requests.
|
|
|
|
func (a *App) Serve(ctx context.Context) {
|
2020-07-13 09:51:21 +00:00
|
|
|
// Attach S3 API:
|
2022-08-30 10:52:37 +00:00
|
|
|
domains := a.cfg.GetStringSlice(cfgListenDomains)
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.FetchDomainsPrepareToUseAPI, zap.Strings("domains", domains))
|
2023-07-05 14:05:45 +00:00
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
cfg := api.Config{
|
|
|
|
Throttle: middleware.ThrottleOpts{
|
|
|
|
Limit: a.settings.maxClient.count,
|
|
|
|
BacklogTimeout: a.settings.maxClient.deadline,
|
|
|
|
},
|
|
|
|
Handler: a.api,
|
|
|
|
Center: a.ctr,
|
|
|
|
Log: a.log,
|
|
|
|
Metrics: a.metrics,
|
|
|
|
Domains: domains,
|
2023-11-16 12:10:51 +00:00
|
|
|
|
2023-11-30 08:25:05 +00:00
|
|
|
MiddlewareSettings: a.settings,
|
2023-12-08 07:44:13 +00:00
|
|
|
PolicyChecker: a.policyStorage,
|
2023-10-05 13:25:25 +00:00
|
|
|
|
2023-12-13 14:44:18 +00:00
|
|
|
FrostfsID: a.frostfsid,
|
|
|
|
FrostFSIDValidation: a.settings.frostfsidValidation,
|
2024-04-10 06:41:07 +00:00
|
|
|
|
|
|
|
XMLDecoder: a.settings,
|
|
|
|
Tagging: a.obj,
|
2023-07-05 14:05:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
chiRouter := api.NewRouter(cfg)
|
2020-07-13 09:51:21 +00:00
|
|
|
|
|
|
|
// Use mux.Router as http.Handler
|
2022-09-12 13:46:55 +00:00
|
|
|
srv := new(http.Server)
|
2023-07-05 14:05:45 +00:00
|
|
|
srv.Handler = chiRouter
|
2020-10-23 00:12:37 +00:00
|
|
|
srv.ErrorLog = zap.NewStdLog(a.log)
|
2023-10-26 13:44:40 +00:00
|
|
|
srv.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
|
|
|
srv.ReadHeaderTimeout = a.cfg.GetDuration(cfgWebReadHeaderTimeout)
|
|
|
|
srv.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
|
|
|
srv.IdleTimeout = a.cfg.GetDuration(cfgWebIdleTimeout)
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.startServices()
|
2022-07-26 13:29:07 +00:00
|
|
|
|
2024-02-11 18:00:56 +00:00
|
|
|
servs := a.getServers()
|
|
|
|
|
|
|
|
for i := range servs {
|
2022-11-09 10:07:18 +00:00
|
|
|
go func(i int) {
|
2024-02-11 18:00:56 +00:00
|
|
|
a.log.Info(logs.StartingServer, zap.String("address", servs[i].Address()))
|
2022-09-12 13:46:55 +00:00
|
|
|
|
2024-02-11 18:00:56 +00:00
|
|
|
if err := srv.Serve(servs[i].Listener()); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.metrics.MarkUnhealthy(servs[i].Address())
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.ListenAndServe, zap.Error(err))
|
2020-07-13 15:50:11 +00:00
|
|
|
}
|
2022-11-09 10:07:18 +00:00
|
|
|
}(i)
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2024-02-11 18:00:56 +00:00
|
|
|
if len(a.unbindServers) != 0 {
|
|
|
|
a.scheduleReconnect(ctx, srv)
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
go func() {
|
|
|
|
address := a.cfg.GetString(cfgControlGRPCEndpoint)
|
|
|
|
a.log.Info(logs.StartingControlAPI, zap.String("address", address))
|
|
|
|
if listener, err := net.Listen("tcp", address); err != nil {
|
|
|
|
a.log.Fatal(logs.ListenAndServe, zap.Error(err))
|
|
|
|
} else if err = a.controlAPI.Serve(listener); err != nil {
|
|
|
|
a.log.Fatal(logs.ListenAndServe, zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGHUP)
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break LOOP
|
|
|
|
case <-sigs:
|
2023-05-31 16:35:20 +00:00
|
|
|
a.configReload(ctx)
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
ctx, cancel := shutdownContext()
|
2020-07-12 23:00:47 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.StoppingServer, zap.Error(srv.Shutdown(ctx)))
|
2022-09-12 12:30:51 +00:00
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
a.stopControlAPI()
|
2022-09-12 12:30:51 +00:00
|
|
|
a.metrics.Shutdown()
|
|
|
|
a.stopServices()
|
2023-05-31 16:35:20 +00:00
|
|
|
a.shutdownTracing()
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
close(a.webDone)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func shutdownContext() (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), defaultShutdownTimeout)
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:49:57 +00:00
|
|
|
func (a *App) stopControlAPI() {
|
|
|
|
ctx, cancel := shutdownContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
a.controlAPI.GracefulStop()
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
|
|
|
a.log.Info(logs.ControlAPICannotShutdownGracefully)
|
|
|
|
a.controlAPI.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
a.log.Info(logs.ControlAPIServiceStopped)
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:20 +00:00
|
|
|
func (a *App) configReload(ctx context.Context) {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.SIGHUPConfigReloadStarted)
|
2022-09-12 09:45:54 +00:00
|
|
|
|
2023-02-03 10:00:18 +00:00
|
|
|
if !a.cfg.IsSet(cmdConfig) && !a.cfg.IsSet(cmdConfigDir) {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadConfigBecauseItsMissed)
|
2022-09-12 09:45:54 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-03 10:00:18 +00:00
|
|
|
if err := readInConfig(a.cfg); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadConfig, zap.Error(err))
|
2022-09-12 09:45:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
if err := a.bucketResolver.UpdateResolvers(a.getResolverOrder()); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadResolvers, zap.Error(err))
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
if err := a.updateServers(); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadServerParameters, zap.Error(err))
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 18:24:22 +00:00
|
|
|
a.setRuntimeParameters()
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.stopServices()
|
|
|
|
a.startServices()
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
a.updateSettings()
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.metrics.SetEnabled(a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-05-31 16:35:20 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-12 12:30:51 +00:00
|
|
|
a.setHealthStatus()
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.SIGHUPConfigReloadCompleted)
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) updateSettings() {
|
|
|
|
if lvl, err := getLogLevel(a.cfg); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.LogLevelWontBeUpdated, zap.Error(err))
|
2022-09-12 09:45:54 +00:00
|
|
|
} else {
|
2022-11-14 09:37:47 +00:00
|
|
|
a.settings.logLevel.SetLevel(lvl)
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:21:09 +00:00
|
|
|
a.settings.update(a.cfg, a.log, a.key)
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func (a *App) startServices() {
|
2022-11-09 10:07:18 +00:00
|
|
|
a.services = a.services[:0]
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
pprofService := NewPprofService(a.cfg, a.log)
|
|
|
|
a.services = append(a.services, pprofService)
|
|
|
|
go pprofService.Start()
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
prometheusService := NewPrometheusService(a.cfg, a.log, a.metrics.Handler())
|
2022-09-12 12:30:51 +00:00
|
|
|
a.services = append(a.services, prometheusService)
|
|
|
|
go prometheusService.Start()
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
func (a *App) initServers(ctx context.Context) {
|
2024-02-11 18:00:56 +00:00
|
|
|
serversInfo := fetchServers(a.cfg, a.log)
|
2022-11-09 10:07:18 +00:00
|
|
|
|
2023-01-18 10:07:26 +00:00
|
|
|
a.servers = make([]Server, 0, len(serversInfo))
|
|
|
|
for _, serverInfo := range serversInfo {
|
|
|
|
fields := []zap.Field{
|
2022-11-09 10:07:18 +00:00
|
|
|
zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled),
|
2023-01-18 10:07:26 +00:00
|
|
|
zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile),
|
|
|
|
}
|
|
|
|
srv, err := newServer(ctx, serverInfo)
|
|
|
|
if err != nil {
|
2024-02-11 18:00:56 +00:00
|
|
|
a.unbindServers = append(a.unbindServers, serverInfo)
|
2023-08-29 15:17:56 +00:00
|
|
|
a.metrics.MarkUnhealthy(serverInfo.Address)
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Warn(logs.FailedToAddServer, append(fields, zap.Error(err))...)
|
2023-01-18 10:07:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-08-29 15:17:56 +00:00
|
|
|
a.metrics.MarkHealthy(serverInfo.Address)
|
2023-01-18 10:07:26 +00:00
|
|
|
|
|
|
|
a.servers = append(a.servers, srv)
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Info(logs.AddServer, fields...)
|
2023-01-18 10:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.servers) == 0 {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.NoHealthyServers)
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) updateServers() error {
|
2024-02-11 18:00:56 +00:00
|
|
|
serversInfo := fetchServers(a.cfg, a.log)
|
|
|
|
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2022-11-09 10:07:18 +00:00
|
|
|
|
2023-01-18 10:07:26 +00:00
|
|
|
var found bool
|
|
|
|
for _, serverInfo := range serversInfo {
|
2024-02-11 18:00:56 +00:00
|
|
|
ser := a.getServer(serverInfo.Address)
|
|
|
|
if ser != nil {
|
|
|
|
if serverInfo.TLS.Enabled {
|
|
|
|
if err := ser.UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil {
|
|
|
|
return fmt.Errorf("failed to update tls certs: %w", err)
|
|
|
|
}
|
|
|
|
found = true
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
2024-02-11 18:00:56 +00:00
|
|
|
} else if unbind := a.updateUnbindServerInfo(serverInfo); unbind {
|
|
|
|
found = true
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
2023-01-18 10:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid servers configuration: no known server found")
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func (a *App) stopServices() {
|
|
|
|
ctx, cancel := shutdownContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for _, svc := range a.services {
|
|
|
|
svc.ShutDown(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 10:05:36 +00:00
|
|
|
func getNotificationsOptions(v *viper.Viper, l *zap.Logger) *notifications.Options {
|
|
|
|
cfg := notifications.Options{}
|
|
|
|
cfg.URL = v.GetString(cfgNATSEndpoint)
|
2023-08-17 21:04:52 +00:00
|
|
|
cfg.Timeout = fetchNATSTimeout(v, l)
|
2022-02-04 10:05:36 +00:00
|
|
|
cfg.TLSCertFilepath = v.GetString(cfgNATSTLSCertFile)
|
|
|
|
cfg.TLSAuthPrivateKeyFilePath = v.GetString(cfgNATSAuthPrivateKeyFile)
|
|
|
|
cfg.RootCAFiles = v.GetStringSlice(cfgNATSRootCAFiles)
|
|
|
|
|
|
|
|
return &cfg
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func getCacheOptions(v *viper.Viper, l *zap.Logger) *layer.CachesConfig {
|
2022-06-06 08:01:12 +00:00
|
|
|
cacheCfg := layer.DefaultCachesConfigs(l)
|
2021-09-10 06:56:56 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.Objects.Lifetime = fetchCacheLifetime(v, l, cfgObjectsCacheLifetime, cacheCfg.Objects.Lifetime)
|
|
|
|
cacheCfg.Objects.Size = fetchCacheSize(v, l, cfgObjectsCacheSize, cacheCfg.Objects.Size)
|
2021-09-10 06:56:56 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.ObjectsList.Lifetime = fetchCacheLifetime(v, l, cfgListObjectsCacheLifetime, cacheCfg.ObjectsList.Lifetime)
|
|
|
|
cacheCfg.ObjectsList.Size = fetchCacheSize(v, l, cfgListObjectsCacheSize, cacheCfg.ObjectsList.Size)
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2023-10-16 06:27:21 +00:00
|
|
|
cacheCfg.SessionList.Lifetime = fetchCacheLifetime(v, l, cfgSessionListCacheLifetime, cacheCfg.SessionList.Lifetime)
|
|
|
|
cacheCfg.SessionList.Size = fetchCacheSize(v, l, cfgSessionListCacheSize, cacheCfg.SessionList.Size)
|
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.Buckets.Lifetime = fetchCacheLifetime(v, l, cfgBucketsCacheLifetime, cacheCfg.Buckets.Lifetime)
|
|
|
|
cacheCfg.Buckets.Size = fetchCacheSize(v, l, cfgBucketsCacheSize, cacheCfg.Buckets.Size)
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.Names.Lifetime = fetchCacheLifetime(v, l, cfgNamesCacheLifetime, cacheCfg.Names.Lifetime)
|
|
|
|
cacheCfg.Names.Size = fetchCacheSize(v, l, cfgNamesCacheSize, cacheCfg.Names.Size)
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.System.Lifetime = fetchCacheLifetime(v, l, cfgSystemCacheLifetime, cacheCfg.System.Lifetime)
|
|
|
|
cacheCfg.System.Size = fetchCacheSize(v, l, cfgSystemCacheSize, cacheCfg.System.Size)
|
2021-09-10 06:56:56 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.AccessControl.Lifetime = fetchCacheLifetime(v, l, cfgAccessControlCacheLifetime, cacheCfg.AccessControl.Lifetime)
|
|
|
|
cacheCfg.AccessControl.Size = fetchCacheSize(v, l, cfgAccessControlCacheSize, cacheCfg.AccessControl.Size)
|
2022-10-03 14:33:49 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
return cacheCfg
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func getAccessBoxCacheConfig(v *viper.Viper, l *zap.Logger) *cache.Config {
|
2022-06-06 08:01:12 +00:00
|
|
|
cacheCfg := cache.DefaultAccessBoxConfig(l)
|
2021-09-08 07:08:56 +00:00
|
|
|
|
2023-08-17 21:04:52 +00:00
|
|
|
cacheCfg.Lifetime = fetchCacheLifetime(v, l, cfgAccessBoxCacheLifetime, cacheCfg.Lifetime)
|
|
|
|
cacheCfg.Size = fetchCacheSize(v, l, cfgAccessBoxCacheSize, cacheCfg.Size)
|
2021-09-08 07:08:56 +00:00
|
|
|
|
|
|
|
return cacheCfg
|
2021-08-16 09:58:09 +00:00
|
|
|
}
|
2021-08-23 18:39:15 +00:00
|
|
|
|
2023-12-05 09:12:35 +00:00
|
|
|
func getMorphPolicyCacheConfig(v *viper.Viper, l *zap.Logger) *cache.Config {
|
|
|
|
cacheCfg := cache.DefaultMorphPolicyConfig(l)
|
|
|
|
|
|
|
|
cacheCfg.Lifetime = fetchCacheLifetime(v, l, cfgMorphPolicyCacheLifetime, cacheCfg.Lifetime)
|
|
|
|
cacheCfg.Size = fetchCacheSize(v, l, cfgMorphPolicyCacheSize, cacheCfg.Size)
|
|
|
|
|
|
|
|
return cacheCfg
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:28:24 +00:00
|
|
|
func getFrostfsIDCacheConfig(v *viper.Viper, l *zap.Logger) *cache.Config {
|
|
|
|
cacheCfg := cache.DefaultFrostfsIDConfig(l)
|
|
|
|
|
|
|
|
cacheCfg.Lifetime = fetchCacheLifetime(v, l, cfgFrostfsIDCacheLifetime, cacheCfg.Lifetime)
|
|
|
|
cacheCfg.Size = fetchCacheSize(v, l, cfgFrostfsIDCacheSize, cacheCfg.Size)
|
|
|
|
|
|
|
|
return cacheCfg
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
func (a *App) initHandler() {
|
2024-02-19 08:45:18 +00:00
|
|
|
var err error
|
2023-12-08 07:44:13 +00:00
|
|
|
|
2024-02-19 08:45:18 +00:00
|
|
|
a.api, err = handler.New(a.log, a.obj, a.nc, a.settings, a.policyStorage, a.frostfsid)
|
2022-11-14 09:37:47 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
a.log.Fatal(logs.CouldNotInitializeAPIHandler, zap.Error(err))
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
2023-08-30 18:24:22 +00:00
|
|
|
|
2024-02-11 18:00:56 +00:00
|
|
|
func (a *App) getServer(address string) Server {
|
|
|
|
for i := range a.servers {
|
|
|
|
if a.servers[i].Address() == address {
|
|
|
|
return a.servers[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) updateUnbindServerInfo(info ServerInfo) bool {
|
|
|
|
for i := range a.unbindServers {
|
|
|
|
if a.unbindServers[i].Address == info.Address {
|
|
|
|
a.unbindServers[i] = info
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) getServers() []Server {
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
return a.servers
|
|
|
|
}
|
|
|
|
|
2023-08-30 18:24:22 +00:00
|
|
|
func (a *App) setRuntimeParameters() {
|
|
|
|
if len(os.Getenv("GOMEMLIMIT")) != 0 {
|
|
|
|
// default limit < yaml limit < app env limit < GOMEMLIMIT
|
|
|
|
a.log.Warn(logs.RuntimeSoftMemoryDefinedWithGOMEMLIMIT)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
softMemoryLimit := fetchSoftMemoryLimit(a.cfg)
|
|
|
|
previous := debug.SetMemoryLimit(softMemoryLimit)
|
|
|
|
if softMemoryLimit != previous {
|
|
|
|
a.log.Info(logs.RuntimeSoftMemoryLimitUpdated,
|
|
|
|
zap.Int64("new_value", softMemoryLimit),
|
|
|
|
zap.Int64("old_value", previous))
|
|
|
|
}
|
|
|
|
}
|
2024-02-11 18:00:56 +00:00
|
|
|
|
|
|
|
func (a *App) scheduleReconnect(ctx context.Context, srv *http.Server) {
|
|
|
|
go func() {
|
|
|
|
t := time.NewTicker(a.settings.reconnectInterval)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
if a.tryReconnect(ctx, srv) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Reset(a.settings.reconnectInterval)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) tryReconnect(ctx context.Context, sr *http.Server) bool {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
|
|
|
a.log.Info(logs.ServerReconnecting)
|
|
|
|
var failedServers []ServerInfo
|
|
|
|
|
|
|
|
for _, serverInfo := range a.unbindServers {
|
|
|
|
fields := []zap.Field{
|
|
|
|
zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled),
|
|
|
|
zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile),
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, err := newServer(ctx, serverInfo)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn(logs.ServerReconnectFailed, zap.Error(err))
|
|
|
|
failedServers = append(failedServers, serverInfo)
|
|
|
|
a.metrics.MarkUnhealthy(serverInfo.Address)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
a.log.Info(logs.StartingServer, zap.String("address", srv.Address()))
|
|
|
|
a.metrics.MarkHealthy(serverInfo.Address)
|
|
|
|
if err = sr.Serve(srv.Listener()); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
a.log.Warn(logs.ListenAndServe, zap.Error(err))
|
|
|
|
a.metrics.MarkUnhealthy(serverInfo.Address)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
a.servers = append(a.servers, srv)
|
|
|
|
a.log.Info(logs.ServerReconnectedSuccessfully, fields...)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.unbindServers = failedServers
|
|
|
|
|
|
|
|
return len(a.unbindServers) == 0
|
|
|
|
}
|