Skip to content

Commit

Permalink
let user define max request timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Sep 2, 2024
1 parent 3a14380 commit fbbde7f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
63 changes: 46 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
defaultBackupsToRetain = 2
defaultGitLabMinimumProjectAccessLevel = 20
defaultEarlyErrorBackOffSeconds = 5
defaultHTTPClientRequestTimeout = 300 * time.Second

pathSep = string(os.PathSeparator)

Expand All @@ -35,20 +36,20 @@ const (
idleConnTimeout = 30 * time.Second

// env vars
envPath = "PATH"
envSobaLogLevel = "SOBA_LOG"
envSobaWebHookURL = "SOBA_WEBHOOK_URL"
envSobaWebHookFormat = "SOBA_WEBHOOK_FORMAT"
envSobaEarlyErrorBackOff = "SOBA_EARLY_ERROR_BACKOFF"
envGitBackupInterval = "GIT_BACKUP_INTERVAL"
envGitBackupDir = "GIT_BACKUP_DIR"
envGitHubAPIURL = "GITHUB_APIURL"
envGitHubBackups = "GITHUB_BACKUPS"
envAzureDevOpsOrgs = "AZURE_DEVOPS_ORGS"
envAzureDevOpsUserName = "AZURE_DEVOPS_USERNAME"
envAzureDevOpsPAT = "AZURE_DEVOPS_PAT"
envAzureDevOpsCompare = "AZURE_DEVOPS_COMPARE"
envAzureDevOpsBackups = "AZURE_DEVOPS_BACKUPS"
envPath = "PATH"
envSobaLogLevel = "SOBA_LOG"
envSobaWebHookURL = "SOBA_WEBHOOK_URL"
envSobaWebHookFormat = "SOBA_WEBHOOK_FORMAT"
envGitBackupInterval = "GIT_BACKUP_INTERVAL"
envGitBackupDir = "GIT_BACKUP_DIR"
envGitRequestTimeout = "GIT_REQUEST_TIMEOUT"
envGitHubAPIURL = "GITHUB_APIURL"
envGitHubBackups = "GITHUB_BACKUPS"
envAzureDevOpsOrgs = "AZURE_DEVOPS_ORGS"
envAzureDevOpsUserName = "AZURE_DEVOPS_USERNAME"
envAzureDevOpsPAT = "AZURE_DEVOPS_PAT"
envAzureDevOpsCompare = "AZURE_DEVOPS_COMPARE"
envAzureDevOpsBackups = "AZURE_DEVOPS_BACKUPS"
// nolint:gosec
envGitHubToken = "GITHUB_TOKEN"
envGitHubOrgs = "GITHUB_ORGS"
Expand Down Expand Up @@ -330,7 +331,6 @@ func displayStartupConfig() {
}

// output azure devops config
// output github config
if azureDevOpsUserName := os.Getenv(envAzureDevOpsUserName); azureDevOpsUserName != "" {
if ghOrgs := strings.ToLower(os.Getenv(envAzureDevOpsOrgs)); ghOrgs != "" {
logger.Printf("Azure DevOps Organistations: %s", ghOrgs)
Expand All @@ -354,6 +354,17 @@ func run() error {

logger.Println("using git executable:", gitExecPath)

ok, reqTimeout, err := getRequestTimeout()
if err != nil {
return err
}

if ok {
logger.Printf("using defined request timeout: %s", reqTimeout.String())
} else {
logger.Printf("using default request timeout: %s", reqTimeout.String())
}

backupDIR, backupDIRKeyExists := os.LookupEnv(envGitBackupDir)
if !backupDIRKeyExists || backupDIR == "" {
return fmt.Errorf("environment variable %s must be set", envGitBackupDir)
Expand All @@ -367,7 +378,7 @@ func run() error {

backupDIR = strings.TrimSuffix(backupDIR, "\n")

_, err := os.Stat(backupDIR)
_, err = os.Stat(backupDIR)
if os.IsNotExist(err) {
return errors.Wrap(err, fmt.Sprintf("specified backup directory \"%s\" does not exist", backupDIR))
}
Expand Down Expand Up @@ -415,6 +426,21 @@ func formatIntervalDuration(m int) string {
return time.Duration(int64(m) * int64(time.Minute)).String()
}

func getRequestTimeout() (bool, time.Duration, error) {
eReqTimeout := os.Getenv(envGitRequestTimeout)

if eReqTimeout != "" {
reqTimeoutInt, err := strconv.Atoi(eReqTimeout)
if err != nil {
return false, defaultHTTPClientRequestTimeout, fmt.Errorf("%s value \"%s\" should be the maximum seconds to wait for a response, defined as an integer", envGitRequestTimeout, eReqTimeout)
}

return true, time.Duration(reqTimeoutInt) * time.Second, nil
}

return false, defaultHTTPClientRequestTimeout, nil
}

func getOrgsListFromEnvVar(envVar string) []string {
orgsList := os.Getenv(envVar)

Expand Down Expand Up @@ -446,9 +472,12 @@ func getHTTPClient(logLevel string) *retryablehttp.Client {
}

rc := retryablehttp.NewClient()

_, reqTimeout, _ := getRequestTimeout()

rc.HTTPClient = &http.Client{
Transport: tr,
Timeout: 300 * time.Second,
Timeout: reqTimeout,
}

if !strings.EqualFold(logLevel, "debug") {
Expand Down
20 changes: 20 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"slices"
"strings"
"testing"
"time"

"github.com/jonhadfield/githosts-utils"

Expand Down Expand Up @@ -311,6 +312,25 @@ func TestAzureDevOpsRepositoryBackupWithBackupsToKeepAsOne(t *testing.T) {
require.NoError(t, run())
}

func TestGetRequestTimeout(t *testing.T) {
t.Setenv(envGitRequestTimeout, "600")
ok, timeout, err := getRequestTimeout()
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, 600*time.Second, timeout)

t.Setenv(envGitRequestTimeout, "invalid")
ok, timeout, err = getRequestTimeout()

Check failure on line 323 in main_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to timeout (ineffassign)
require.False(t, ok)
require.Error(t, err)

t.Setenv(envGitRequestTimeout, "")
ok, timeout, err = getRequestTimeout()
require.NoError(t, err)
require.False(t, ok)
require.Equal(t, defaultHTTPClientRequestTimeout, timeout)
}

func TestPublicGithubRepositoryBackupWithBackupsToKeepAsOne(t *testing.T) {
if os.Getenv(envGitHubToken) == "" {
t.Skipf(skipGitHubTestMissingToken, envGitHubToken)
Expand Down
3 changes: 2 additions & 1 deletion notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"github.com/slack-go/slack"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/slack-go/slack"

"github.com/hashicorp/go-retryablehttp"
"gitlab.com/tozd/go/errors"
)
Expand Down

0 comments on commit fbbde7f

Please sign in to comment.