forked from kuskoman/logstash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add environment variable to configure http timeout
- Loading branch information
Jakub Surdej
committed
Jan 11, 2024
1 parent
a8f242b
commit 03f1f4d
Showing
9 changed files
with
99 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
package config | ||
|
||
import "time" | ||
import ( | ||
"os" | ||
"time" | ||
) | ||
|
||
// HttpTimeout is the timeout for http requests utilized by multiple contexts | ||
const HttpTimeout = time.Second * 2 | ||
const ( | ||
defaultHttpTimeout = time.Second * 2 | ||
httpTimeoutEnvVar = "HTTP_TIMEOUT" | ||
) | ||
|
||
func GetHttpTimeout() (time.Duration, error) { | ||
userDefinedTimeout := os.Getenv(httpTimeoutEnvVar) | ||
if userDefinedTimeout == "" { | ||
return defaultHttpTimeout, nil | ||
} | ||
|
||
timeout, err := time.ParseDuration(userDefinedTimeout) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return timeout, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetHttpTimeout(t *testing.T) { | ||
t.Run("DefaultTimeout", func(t *testing.T) { | ||
t.Parallel() | ||
os.Unsetenv(httpTimeoutEnvVar) | ||
timeout, err := GetHttpTimeout() | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if timeout != defaultHttpTimeout { | ||
t.Errorf("Expected default timeout of %v, got %v", defaultHttpTimeout, timeout) | ||
} | ||
}) | ||
|
||
t.Run("CustomTimeout", func(t *testing.T) { | ||
t.Parallel() | ||
expectedTimeout := "5s" | ||
os.Setenv(httpTimeoutEnvVar, expectedTimeout) | ||
defer os.Unsetenv(httpTimeoutEnvVar) | ||
timeout, err := GetHttpTimeout() | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
parsedTimeout, _ := time.ParseDuration(expectedTimeout) | ||
if timeout != parsedTimeout { | ||
t.Errorf("Expected timeout of %v, got %v", parsedTimeout, timeout) | ||
} | ||
}) | ||
|
||
t.Run("InvalidTimeout", func(t *testing.T) { | ||
t.Parallel() | ||
os.Setenv(httpTimeoutEnvVar, "invalid") | ||
defer os.Unsetenv(httpTimeoutEnvVar) | ||
_, err := GetHttpTimeout() | ||
if err == nil { | ||
t.Error("Expected an error for invalid timeout, but got nil") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters