2019-11-06 12:33:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-13 16:02:48 +00:00
|
|
|
"crypto/ecdsa"
|
2020-02-14 10:07:52 +00:00
|
|
|
"errors"
|
2019-11-06 12:33:46 +00:00
|
|
|
"time"
|
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
"github.com/fasthttp/router"
|
|
|
|
"github.com/valyala/fasthttp"
|
2019-11-06 12:33:46 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-13 16:02:48 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2019-11-06 12:33:46 +00:00
|
|
|
)
|
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
type app struct {
|
2019-12-21 10:26:14 +00:00
|
|
|
pool *Pool
|
2019-11-06 12:33:46 +00:00
|
|
|
log *zap.Logger
|
|
|
|
timeout time.Duration
|
2019-12-13 16:02:48 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
2020-02-28 17:07:50 +00:00
|
|
|
err error
|
2020-02-25 15:35:46 +00:00
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
v = settings()
|
|
|
|
l = newLogger(v)
|
2020-02-28 17:07:50 +00:00
|
|
|
z = gRPCLogger(l)
|
2019-12-21 10:26:14 +00:00
|
|
|
g = newGracefulContext(l)
|
2020-02-27 10:05:37 +00:00
|
|
|
d = v.GetDuration("rebalance_timer")
|
2019-11-06 12:33:46 +00:00
|
|
|
)
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if v.GetBool("verbose") {
|
2020-02-28 17:07:50 +00:00
|
|
|
grpclog.SetLoggerV2(z)
|
2020-02-25 15:35:46 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
a := &app{
|
2019-12-21 10:26:14 +00:00
|
|
|
log: l,
|
2020-02-28 17:07:50 +00:00
|
|
|
pool: newPool(l, v),
|
2019-12-21 10:26:14 +00:00
|
|
|
key: fetchKey(l, v),
|
|
|
|
timeout: v.GetDuration("request_timeout"),
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
r := router.New()
|
|
|
|
r.RedirectTrailingSlash = true
|
|
|
|
r.GET("/get/:cid/:oid", a.receiveFile)
|
2019-11-06 12:33:46 +00:00
|
|
|
|
2020-03-03 10:36:52 +00:00
|
|
|
// attaching /-/(ready,healthy)
|
|
|
|
attachHealthy(r, a.pool.unhealthy)
|
2019-12-21 10:26:14 +00:00
|
|
|
|
|
|
|
// enable metrics
|
|
|
|
if v.GetBool("metrics") {
|
|
|
|
l.Info("enabled /metrics")
|
2020-03-03 10:36:52 +00:00
|
|
|
attachMetrics(r, z)
|
2019-12-21 10:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enable pprof
|
|
|
|
if v.GetBool("pprof") {
|
|
|
|
l.Info("enabled /debug/pprof")
|
2020-03-03 10:36:52 +00:00
|
|
|
attachProfiler(r)
|
2020-02-28 17:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
en := &fasthttp.Server{
|
|
|
|
Name: "neofs-http-gate",
|
|
|
|
Handler: r.Handler,
|
|
|
|
ReadBufferSize: 4096,
|
|
|
|
ReadTimeout: time.Second * 15,
|
|
|
|
GetOnly: true,
|
|
|
|
DisableHeaderNamesNormalizing: true,
|
|
|
|
NoDefaultServerHeader: true,
|
|
|
|
NoDefaultContentType: true,
|
2019-12-21 10:26:14 +00:00
|
|
|
}
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
go func() {
|
2020-02-28 17:07:50 +00:00
|
|
|
bind := v.GetString("listen_address")
|
2019-12-21 10:26:14 +00:00
|
|
|
l.Info("run gateway server",
|
2020-02-28 17:07:50 +00:00
|
|
|
zap.String("address", bind))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
if err := en.ListenAndServe(bind); err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
l.Panic("could not start server", zap.Error(err))
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
go checkConnection(g, d, a.pool)
|
|
|
|
|
|
|
|
a.pool.reBalance(g)
|
|
|
|
|
|
|
|
switch _, err = a.pool.getConnection(g); {
|
|
|
|
case err == nil:
|
|
|
|
// ignore
|
|
|
|
case errors.Is(err, context.Canceled):
|
|
|
|
// ignore
|
|
|
|
// l.Info("context canceled")
|
|
|
|
default:
|
|
|
|
l.Error("could get connection", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2019-11-06 12:33:46 +00:00
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
<-g.Done()
|
2019-11-06 12:33:46 +00:00
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
l.Info("web server stopped", zap.Error(en.Shutdown()))
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 10:05:37 +00:00
|
|
|
func checkConnection(ctx context.Context, dur time.Duration, p *Pool) {
|
2020-02-14 11:35:35 +00:00
|
|
|
tick := time.NewTimer(dur)
|
2019-12-21 10:26:14 +00:00
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break loop
|
|
|
|
case <-tick.C:
|
2019-12-21 10:26:14 +00:00
|
|
|
p.reBalance(ctx)
|
2020-02-14 11:35:35 +00:00
|
|
|
tick.Reset(dur)
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
p.close()
|
2019-11-06 12:33:46 +00:00
|
|
|
tick.Stop()
|
2019-12-21 10:26:14 +00:00
|
|
|
|
2020-02-28 17:07:50 +00:00
|
|
|
p.log.Info("connection worker stopped")
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|