frostfs-http-gw/main.go

156 lines
3.2 KiB
Go
Raw Normal View History

2019-11-06 12:33:46 +00:00
package main
import (
"context"
"crypto/ecdsa"
2020-02-14 10:07:52 +00:00
"errors"
2019-11-06 12:33:46 +00:00
"time"
"github.com/fasthttp/router"
"github.com/prometheus/client_golang/prometheus"
http "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp"
2019-11-06 12:33:46 +00:00
"go.uber.org/zap"
"google.golang.org/grpc/grpclog"
2019-11-06 12:33:46 +00:00
)
type app struct {
pool *Pool
2019-11-06 12:33:46 +00:00
log *zap.Logger
timeout time.Duration
key *ecdsa.PrivateKey
2019-11-06 12:33:46 +00:00
}
const (
defaultHealthyMsg = "NeoFS HTTP Gateway is "
defaultContentType = "text/plain; charset=utf-8"
)
2019-11-06 12:33:46 +00:00
func main() {
var (
err error
v = settings()
l = newLogger(v)
z = gRPCLogger(l)
g = newGracefulContext(l)
2020-02-27 10:05:37 +00:00
d = v.GetDuration("rebalance_timer")
2019-11-06 12:33:46 +00:00
)
if v.GetBool("verbose") {
grpclog.SetLoggerV2(z)
}
a := &app{
log: l,
pool: newPool(l, v),
key: fetchKey(l, v),
timeout: v.GetDuration("request_timeout"),
2019-11-06 12:33:46 +00:00
}
r := router.New()
r.RedirectTrailingSlash = true
r.GET("/get/:cid/:oid", a.receiveFile)
2019-11-06 12:33:46 +00:00
r.GET("/-/ready", func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBodyString("NeoFS HTTP Gateway is ready")
})
r.GET("/-/healthy", func(c *fasthttp.RequestCtx) {
code := fasthttp.StatusOK
msg := "healthy"
if err := a.pool.unhealthy.Load(); err != nil {
msg = "unhealthy: " + err.Error()
code = fasthttp.StatusBadRequest
}
2019-11-06 12:33:46 +00:00
c.Response.Reset()
c.SetStatusCode(code)
c.SetContentType(defaultContentType)
c.SetBodyString(defaultHealthyMsg + msg)
})
// enable metrics
if v.GetBool("metrics") {
l.Info("enabled /metrics")
r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, http.HandlerOpts{
ErrorLog: z.(http.Logger),
//ErrorHandling: 0,
//Registry: nil,
//DisableCompression: false,
//MaxRequestsInFlight: 0,
//Timeout: 0,
//EnableOpenMetrics: false,
}))
}
// enable pprof
if v.GetBool("pprof") {
l.Info("enabled /debug/pprof")
r.GET("/debug/pprof/", pprofHandler())
r.GET("/debug/pprof/:name", pprofHandler())
}
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-11-06 12:33:46 +00:00
go func() {
bind := v.GetString("listen_address")
l.Info("run gateway server",
zap.String("address", bind))
if err := en.ListenAndServe(bind); err != nil {
l.Panic("could not start server", zap.Error(err))
2019-11-06 12:33:46 +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
<-g.Done()
2019-11-06 12:33:46 +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-11-06 12:33:46 +00:00
loop:
for {
select {
case <-ctx.Done():
break loop
case <-tick.C:
p.reBalance(ctx)
2020-02-14 11:35:35 +00:00
tick.Reset(dur)
2019-11-06 12:33:46 +00:00
}
}
p.close()
2019-11-06 12:33:46 +00:00
tick.Stop()
p.log.Info("connection worker stopped")
2019-11-06 12:33:46 +00:00
}