Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring the porkbun's client timeout #75

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"time"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -30,6 +31,7 @@ type configuration struct {
UpdateIntervalMinutes int `yaml:"update_interval_minutes"`
PorkbunCredentials map[string]PorkbunCredentials `yaml:"credentials"`
Metrics MetricsConfig `yaml:"metrics"`
Timeout time.Duration `yaml:"timeout"`
}

func getConfig(configFile string) (configuration, error) {
Expand Down Expand Up @@ -58,6 +60,14 @@ func getConfig(configFile string) (configuration, error) {
}
}

if c.Timeout == 0 {
c.Timeout = 10 * time.Second
}

if c.UpdateIntervalMinutes == 0 {
c.UpdateIntervalMinutes = 5
}

return c, nil
}

Expand Down
1 change: 1 addition & 0 deletions example.config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
update_interval_minutes: 5
timeout: 20s

credentials:
prod:
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func configureLogs(logLevel string) error {
}

func updateRecords(c configuration) {
clients, err := getPorkbunClients(c.PorkbunCredentials)
clients, err := getPorkbunClients(c.PorkbunCredentials, c.Timeout)
if err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 5 additions & 4 deletions porkbun.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strconv"
"time"

"github.com/nrdcg/porkbun"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -68,11 +69,11 @@ func getRecords(ctx context.Context, domain, host string, client *porkbun.Client
return &ipv4Record, &ipv6Record, nil
}

func getPorkbunClients(credentials map[string]PorkbunCredentials) (map[string]*porkbun.Client, error) {
func getPorkbunClients(credentials map[string]PorkbunCredentials, timeout time.Duration) (map[string]*porkbun.Client, error) {
clients := make(map[string]*porkbun.Client)

for key, credential := range credentials {
client, err := getPorkbunClient(credential, key)
client, err := getPorkbunClient(credential, key, timeout)
if err != nil {
connectionErrorsTotal.Inc()
log.Errorf("Error getting client for credentials '%s': %v", key, err)
Expand All @@ -83,9 +84,9 @@ func getPorkbunClients(credentials map[string]PorkbunCredentials) (map[string]*p
return clients, nil
}

func getPorkbunClient(credentials PorkbunCredentials, credentialsName string) (*porkbun.Client, error) {
func getPorkbunClient(credentials PorkbunCredentials, credentialsName string, timeout time.Duration) (*porkbun.Client, error) {
client := porkbun.New(credentials.PorkbunSecretKey, credentials.PorkbunAPIKey)

client.HTTPClient.Timeout = timeout
ctx := context.Background()

yourIP, err := client.Ping(ctx)
Expand Down
Loading