From b2ace767c84612ec25573a50616ce6fa74d8f987 Mon Sep 17 00:00:00 2001 From: John Kjell Date: Thu, 1 Aug 2024 02:39:23 -0500 Subject: [PATCH] Add config options for http read and write timeouts (#345) Signed-off-by: John Kjell --- README.md | 2 ++ cmd/archivista/main.go | 4 ++-- cmd/archivistactl/cmd/retrieve_test.go | 4 ++-- cmd/archivistactl/cmd/search_test.go | 2 +- pkg/config/config.go | 2 ++ pkg/config/config_test.go | 6 ++++++ 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a4d56ba6..74896e84 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,8 @@ Archivista is configured through environment variables currently. | Variable | Default Value | Description | | ------------------------------------------ | ----------------------------------------- | --------------------------------------------------------------------------------------------- | | ARCHIVISTA_LISTEN_ON | tcp://127.0.0.1:8082 | URL endpoint for Archivista to listen on | +| ARCHIVISTA_READ_TIMEOUT | 120 | HTTP server read timeout | +| ARCHIVISTA_WRITE_TIMEOUT | 120 | HTTP server write timeout | | ARCHIVISTA_LOG_LEVEL | INFO | Log level. Options are DEBUG, INFO, WARN, ERROR | | ARCHIVISTA_CORS_ALLOW_ORIGINS | | Comma separated list of origins to allow CORS requests from | | ARCHIVISTA_SQL_STORE_BACKEND | | Backend to use for SQL. Options are MYSQL or PSQL | diff --git a/cmd/archivista/main.go b/cmd/archivista/main.go index 7a5448a6..fc84a10c 100644 --- a/cmd/archivista/main.go +++ b/cmd/archivista/main.go @@ -83,8 +83,8 @@ func main() { handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}), handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}), )(server.Router()), - ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, + ReadTimeout: time.Duration(archivistaService.Cfg.ReadTimeout) * time.Second, + WriteTimeout: time.Duration(archivistaService.Cfg.WriteTimeout) * time.Second, } go func() { if err := srv.Serve(listener); err != nil { diff --git a/cmd/archivistactl/cmd/retrieve_test.go b/cmd/archivistactl/cmd/retrieve_test.go index 1c3c587a..dd79d831 100644 --- a/cmd/archivistactl/cmd/retrieve_test.go +++ b/cmd/archivistactl/cmd/retrieve_test.go @@ -62,7 +62,7 @@ func (ut *UTRetrieveSuite) Test_RetrieveEnvelope_NoDB() { rootCmd.SetArgs([]string{"retrieve", "envelope", "test"}) err := rootCmd.Execute() if err != nil { - ut.ErrorContains(err, "connection refused") + ut.ErrorContains(err, "connection re") } else { ut.FailNow("Expected: error") } @@ -88,7 +88,7 @@ func (ut *UTRetrieveSuite) Test_RetrieveSubjectsNoDB() { rootCmd.SetArgs([]string{"retrieve", "subjects", "test"}) err := rootCmd.Execute() if err != nil { - ut.ErrorContains(err, "connection refused") + ut.ErrorContains(err, "connection re") } else { ut.FailNow("Expected: error") } diff --git a/cmd/archivistactl/cmd/search_test.go b/cmd/archivistactl/cmd/search_test.go index 02fd9f8c..6b2e7523 100644 --- a/cmd/archivistactl/cmd/search_test.go +++ b/cmd/archivistactl/cmd/search_test.go @@ -63,7 +63,7 @@ func (ut *UTSearchSuite) Test_NoDB() { rootCmd.SetArgs([]string{"search", "sha256:test"}) err := rootCmd.Execute() if err != nil { - ut.ErrorContains(err, "connection refused") + ut.ErrorContains(err, "connection re") } else { ut.FailNow("Expected: error") } diff --git a/pkg/config/config.go b/pkg/config/config.go index dd708fcb..69d581eb 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -26,6 +26,8 @@ import ( type Config struct { ListenOn string `default:"tcp://127.0.0.1:8082" desc:"URL endpoint for Archivista to listen on" split_words:"true"` + ReadTimeout int `default:"120" desc:"HTTP read timeout in seconds" split_words:"true"` + WriteTimeout int `default:"120" desc:"HTTP write timeout in seconds" split_words:"true"` LogLevel string `default:"INFO" desc:"Log level" split_words:"true"` CORSAllowOrigins []string `default:"" desc:"Comma separated list of origins to allow CORS requests from" split_words:"true"` diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 2df0120f..7d13d0a8 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -26,6 +26,8 @@ import ( func TestConfig_Process(t *testing.T) { // Set up test environment variables os.Setenv("ARCHIVISTA_LISTEN_ON", "tcp://0.0.0.0:8082") + os.Setenv("ARCHIVISTA_READ_TIMEOUT", "300") + os.Setenv("ARCHIVISTA_WRITE_TIMEOUT", "300") os.Setenv("ARCHIVISTA_LOG_LEVEL", "DEBUG") os.Setenv("ARCHIVISTA_CORS_ALLOW_ORIGINS", "http://localhost,https://example.com") os.Setenv("ARCHIVISTA_ENABLE_SPIFFE", "FALSE") @@ -44,6 +46,8 @@ func TestConfig_Process(t *testing.T) { // Check that the expected values were read from environment variables require.Equal(t, "tcp://0.0.0.0:8082", c.ListenOn) + require.Equal(t, 300, c.ReadTimeout) + require.Equal(t, 300, c.WriteTimeout) require.Equal(t, "DEBUG", c.LogLevel) require.Equal(t, []string{"http://localhost", "https://example.com"}, c.CORSAllowOrigins) require.False(t, c.EnableSPIFFE) @@ -57,6 +61,8 @@ func TestConfig_Process(t *testing.T) { // Clean up environment variables os.Unsetenv("ARCHIVISTA_LISTEN_ON") + os.Unsetenv("ARCHIVISTA_READ_TIMEOUT") + os.Unsetenv("ARCHIVISTA_WRITE_TIMEOUT") os.Unsetenv("ARCHIVISTA_LOG_LEVEL") os.Unsetenv("ARCHIVISTA_CORS_ALLOW_ORIGINS") os.Unsetenv("ARCHIVISTA_ENABLE_SPIFFE")