Skip to content

Commit

Permalink
Add config options for http read and write timeouts (#345)
Browse files Browse the repository at this point in the history
Signed-off-by: John Kjell <[email protected]>
  • Loading branch information
jkjell authored Aug 1, 2024
1 parent 3b8f3e8 commit b2ace76
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions cmd/archivista/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/archivistactl/cmd/retrieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/archivistactl/cmd/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit b2ace76

Please sign in to comment.