Make http server timeouts configurable for cli commands

This commit is contained in:
Geoff Stuart 2022-11-25 17:32:42 -05:00
parent e6e50ee9f7
commit 1f70ced8e5
7 changed files with 77 additions and 8 deletions

View File

@ -74,6 +74,10 @@ var runCmd = &cli.Command{
Name: "captcha-threshold", Name: "captcha-threshold",
Value: 0.5, Value: 0.5,
}, },
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
sendPerRequest, err := types.ParseFIL(cctx.String("amount")) sendPerRequest, err := types.ParseFIL(cctx.String("amount"))
@ -127,9 +131,14 @@ var runCmd = &cli.Command{
os.Exit(0) os.Exit(0)
}() }()
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
server := &http.Server{ server := &http.Server{
Addr: cctx.String("front"), Addr: cctx.String("front"),
ReadHeaderTimeout: 3 * time.Second, ReadHeaderTimeout: timeout,
} }
return server.ListenAndServe() return server.ListenAndServe()

View File

@ -418,12 +418,21 @@ var runCmd = &cli.Command{
Usage: "messages with a prove cap larger than this will be skipped when processing pre commit messages", Usage: "messages with a prove cap larger than this will be skipped when processing pre commit messages",
Value: "0.000000001", Value: "0.000000001",
}, },
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
go func() { go func() {
server := &http.Server{ server := &http.Server{
Addr: ":6060", Addr: ":6060",
ReadHeaderTimeout: 3 * time.Second, ReadHeaderTimeout: timeout,
} }
_ = server.ListenAndServe() _ = server.ListenAndServe()

View File

@ -26,6 +26,10 @@ var itestdCmd = &cli.Command{
Name: "listen", Name: "listen",
Value: "127.0.0.1:5674", Value: "127.0.0.1:5674",
}, },
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
var nodes []kit.ItestdNotif var nodes []kit.ItestdNotif
@ -45,9 +49,14 @@ var itestdCmd = &cli.Command{
if err != nil { if err != nil {
return xerrors.Errorf("net listen: %w", err) return xerrors.Errorf("net listen: %w", err)
} }
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
s := &httptest.Server{ s := &httptest.Server{
Listener: l, Listener: l,
Config: &http.Server{Handler: m, ReadHeaderTimeout: 3 * time.Second}, Config: &http.Server{Handler: m, ReadHeaderTimeout: timeout},
} }
s.Start() s.Start()
fmt.Printf("ITest env:\n\nLOTUS_ITESTD=%s\n\nSay 'sh' to spawn a shell connected to test nodes\n--- waiting for clients\n", s.URL) fmt.Printf("ITest env:\n\nLOTUS_ITESTD=%s\n\nSay 'sh' to spawn a shell connected to test nodes\n--- waiting for clients\n", s.URL)

View File

@ -6,6 +6,8 @@ import (
"sort" "sort"
"time" "time"
"golang.org/x/xerrors"
"contrib.go.opencensus.io/exporter/prometheus" "contrib.go.opencensus.io/exporter/prometheus"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
@ -76,8 +78,14 @@ type msgInfo struct {
var mpoolStatsCmd = &cli.Command{ var mpoolStatsCmd = &cli.Command{
Name: "mpool-stats", Name: "mpool-stats",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
},
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
logging.SetLogLevel("rpc", "ERROR") _ = logging.SetLogLevel("rpc", "ERROR")
if err := view.Register(AgeView, SizeView, InboundRate, InclusionRate, MsgWait); err != nil { if err := view.Register(AgeView, SizeView, InboundRate, InclusionRate, MsgWait); err != nil {
return err return err
@ -92,10 +100,15 @@ var mpoolStatsCmd = &cli.Command{
http.Handle("/debug/metrics", expo) http.Handle("/debug/metrics", expo)
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
go func() { go func() {
server := &http.Server{ server := &http.Server{
Addr: ":10555", Addr: ":10555",
ReadHeaderTimeout: 3 * time.Second, ReadHeaderTimeout: timeout,
} }
if err := server.ListenAndServe(); err != nil { if err := server.ListenAndServe(); err != nil {

View File

@ -7,6 +7,8 @@ import (
"os" "os"
"time" "time"
"golang.org/x/xerrors"
"contrib.go.opencensus.io/exporter/prometheus" "contrib.go.opencensus.io/exporter/prometheus"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -128,6 +130,10 @@ var runCmd = &cli.Command{
EnvVars: []string{"LOTUS_STATS_IPLD_STORE_CACHE_SIZE"}, EnvVars: []string{"LOTUS_STATS_IPLD_STORE_CACHE_SIZE"},
Value: 2 << 15, Value: 2 << 15,
}, },
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
ctx := context.Background() ctx := context.Background()
@ -158,11 +164,16 @@ var runCmd = &cli.Command{
return err return err
} }
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
go func() { go func() {
http.Handle("/metrics", exporter) http.Handle("/metrics", exporter)
server := &http.Server{ server := &http.Server{
Addr: ":6688", Addr: ":6688",
ReadHeaderTimeout: 3 * time.Second, ReadHeaderTimeout: timeout,
} }
if err := server.ListenAndServe(); err != nil { if err := server.ListenAndServe(); err != nil {
log.Errorw("failed to start http server", "err", err) log.Errorw("failed to start http server", "err", err)

View File

@ -143,6 +143,10 @@ var runCmd = &cli.Command{
Usage: "(insecure) disable api auth", Usage: "(insecure) disable api auth",
Hidden: true, Hidden: true,
}, },
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
}, },
Description: "Needs FULLNODE_API_INFO env-var to be set before running (see lotus-wallet --help for setup instructions)", Description: "Needs FULLNODE_API_INFO env-var to be set before running (see lotus-wallet --help for setup instructions)",
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
@ -240,9 +244,14 @@ var runCmd = &cli.Command{
} }
} }
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
srv := &http.Server{ srv := &http.Server{
Handler: handler, Handler: handler,
ReadHeaderTimeout: 3 * time.Second, ReadHeaderTimeout: timeout,
BaseContext: func(listener net.Listener) context.Context { BaseContext: func(listener net.Listener) context.Context {
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-wallet")) ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-wallet"))
return ctx return ctx

View File

@ -272,6 +272,10 @@ var runCmd = &cli.Command{
Value: "30m", Value: "30m",
EnvVars: []string{"LOTUS_WORKER_TIMEOUT"}, EnvVars: []string{"LOTUS_WORKER_TIMEOUT"},
}, },
&cli.StringFlag{
Name: "http-server-timeout",
Value: "3s",
},
}, },
Before: func(cctx *cli.Context) error { Before: func(cctx *cli.Context) error {
if cctx.IsSet("address") { if cctx.IsSet("address") {
@ -562,9 +566,14 @@ var runCmd = &cli.Command{
log.Info("Setting up control endpoint at " + address) log.Info("Setting up control endpoint at " + address)
timeout, err := time.ParseDuration(cctx.String("http-server-timeout"))
if err != nil {
return xerrors.Errorf("invalid time string %s: %x", cctx.String("http-server-timeout"), err)
}
srv := &http.Server{ srv := &http.Server{
Handler: sealworker.WorkerHandler(nodeApi.AuthVerify, remoteHandler, workerApi, true), Handler: sealworker.WorkerHandler(nodeApi.AuthVerify, remoteHandler, workerApi, true),
ReadHeaderTimeout: 3 * time.Second, ReadHeaderTimeout: timeout,
BaseContext: func(listener net.Listener) context.Context { BaseContext: func(listener net.Listener) context.Context {
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-worker")) ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-worker"))
return ctx return ctx