From 1f70ced8e5d8bd72882525ebbcdf35ea1fa9ead5 Mon Sep 17 00:00:00 2001 From: Geoff Stuart Date: Fri, 25 Nov 2022 17:32:42 -0500 Subject: [PATCH] Make http server timeouts configurable for cli commands --- cmd/lotus-fountain/main.go | 11 ++++++++++- cmd/lotus-pcr/main.go | 11 ++++++++++- cmd/lotus-shed/itestd.go | 11 ++++++++++- cmd/lotus-shed/mempool-stats.go | 17 +++++++++++++++-- cmd/lotus-stats/main.go | 13 ++++++++++++- cmd/lotus-wallet/main.go | 11 ++++++++++- cmd/lotus-worker/main.go | 11 ++++++++++- 7 files changed, 77 insertions(+), 8 deletions(-) diff --git a/cmd/lotus-fountain/main.go b/cmd/lotus-fountain/main.go index 53fd288f7..b3ad32fd2 100644 --- a/cmd/lotus-fountain/main.go +++ b/cmd/lotus-fountain/main.go @@ -74,6 +74,10 @@ var runCmd = &cli.Command{ Name: "captcha-threshold", Value: 0.5, }, + &cli.StringFlag{ + Name: "http-server-timeout", + Value: "3s", + }, }, Action: func(cctx *cli.Context) error { sendPerRequest, err := types.ParseFIL(cctx.String("amount")) @@ -127,9 +131,14 @@ var runCmd = &cli.Command{ 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{ Addr: cctx.String("front"), - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: timeout, } return server.ListenAndServe() diff --git a/cmd/lotus-pcr/main.go b/cmd/lotus-pcr/main.go index c98829bd2..0bbdec3a5 100644 --- a/cmd/lotus-pcr/main.go +++ b/cmd/lotus-pcr/main.go @@ -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", Value: "0.000000001", }, + &cli.StringFlag{ + Name: "http-server-timeout", + Value: "3s", + }, }, 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() { server := &http.Server{ Addr: ":6060", - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: timeout, } _ = server.ListenAndServe() diff --git a/cmd/lotus-shed/itestd.go b/cmd/lotus-shed/itestd.go index 2e70a9fcd..424e15768 100644 --- a/cmd/lotus-shed/itestd.go +++ b/cmd/lotus-shed/itestd.go @@ -26,6 +26,10 @@ var itestdCmd = &cli.Command{ Name: "listen", Value: "127.0.0.1:5674", }, + &cli.StringFlag{ + Name: "http-server-timeout", + Value: "3s", + }, }, Action: func(cctx *cli.Context) error { var nodes []kit.ItestdNotif @@ -45,9 +49,14 @@ var itestdCmd = &cli.Command{ if err != nil { 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{ Listener: l, - Config: &http.Server{Handler: m, ReadHeaderTimeout: 3 * time.Second}, + Config: &http.Server{Handler: m, ReadHeaderTimeout: timeout}, } 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) diff --git a/cmd/lotus-shed/mempool-stats.go b/cmd/lotus-shed/mempool-stats.go index f03ec3ee5..e8bc44388 100644 --- a/cmd/lotus-shed/mempool-stats.go +++ b/cmd/lotus-shed/mempool-stats.go @@ -6,6 +6,8 @@ import ( "sort" "time" + "golang.org/x/xerrors" + "contrib.go.opencensus.io/exporter/prometheus" "github.com/ipfs/go-cid" logging "github.com/ipfs/go-log/v2" @@ -76,8 +78,14 @@ type msgInfo struct { var mpoolStatsCmd = &cli.Command{ Name: "mpool-stats", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "http-server-timeout", + Value: "3s", + }, + }, 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 { return err @@ -92,10 +100,15 @@ var mpoolStatsCmd = &cli.Command{ 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() { server := &http.Server{ Addr: ":10555", - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: timeout, } if err := server.ListenAndServe(); err != nil { diff --git a/cmd/lotus-stats/main.go b/cmd/lotus-stats/main.go index 58096f90a..d82a6078f 100644 --- a/cmd/lotus-stats/main.go +++ b/cmd/lotus-stats/main.go @@ -7,6 +7,8 @@ import ( "os" "time" + "golang.org/x/xerrors" + "contrib.go.opencensus.io/exporter/prometheus" logging "github.com/ipfs/go-log/v2" "github.com/urfave/cli/v2" @@ -128,6 +130,10 @@ var runCmd = &cli.Command{ EnvVars: []string{"LOTUS_STATS_IPLD_STORE_CACHE_SIZE"}, Value: 2 << 15, }, + &cli.StringFlag{ + Name: "http-server-timeout", + Value: "3s", + }, }, Action: func(cctx *cli.Context) error { ctx := context.Background() @@ -158,11 +164,16 @@ var runCmd = &cli.Command{ 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() { http.Handle("/metrics", exporter) server := &http.Server{ Addr: ":6688", - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: timeout, } if err := server.ListenAndServe(); err != nil { log.Errorw("failed to start http server", "err", err) diff --git a/cmd/lotus-wallet/main.go b/cmd/lotus-wallet/main.go index a6662f75f..893c8b2c6 100644 --- a/cmd/lotus-wallet/main.go +++ b/cmd/lotus-wallet/main.go @@ -143,6 +143,10 @@ var runCmd = &cli.Command{ Usage: "(insecure) disable api auth", 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)", 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{ Handler: handler, - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: timeout, BaseContext: func(listener net.Listener) context.Context { ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-wallet")) return ctx diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index e4e80cad0..75c449833 100644 --- a/cmd/lotus-worker/main.go +++ b/cmd/lotus-worker/main.go @@ -272,6 +272,10 @@ var runCmd = &cli.Command{ Value: "30m", EnvVars: []string{"LOTUS_WORKER_TIMEOUT"}, }, + &cli.StringFlag{ + Name: "http-server-timeout", + Value: "3s", + }, }, Before: func(cctx *cli.Context) error { if cctx.IsSet("address") { @@ -562,9 +566,14 @@ var runCmd = &cli.Command{ 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{ Handler: sealworker.WorkerHandler(nodeApi.AuthVerify, remoteHandler, workerApi, true), - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: timeout, BaseContext: func(listener net.Listener) context.Context { ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-worker")) return ctx