Skip to content

Commit 5915e98

Browse files
authored
Allow configuring the porkbun's client timeout (#75)
1 parent 2057b55 commit 5915e98

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

config.go

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
"gopkg.in/yaml.v3"
89
)
@@ -30,6 +31,7 @@ type configuration struct {
3031
UpdateIntervalMinutes int `yaml:"update_interval_minutes"`
3132
PorkbunCredentials map[string]PorkbunCredentials `yaml:"credentials"`
3233
Metrics MetricsConfig `yaml:"metrics"`
34+
Timeout time.Duration `yaml:"timeout"`
3335
}
3436

3537
func getConfig(configFile string) (configuration, error) {
@@ -58,6 +60,14 @@ func getConfig(configFile string) (configuration, error) {
5860
}
5961
}
6062

63+
if c.Timeout == 0 {
64+
c.Timeout = 10 * time.Second
65+
}
66+
67+
if c.UpdateIntervalMinutes == 0 {
68+
c.UpdateIntervalMinutes = 5
69+
}
70+
6171
return c, nil
6272
}
6373

example.config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
update_interval_minutes: 5
2+
timeout: 20s
23

34
credentials:
45
prod:

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func configureLogs(logLevel string) error {
7575
}
7676

7777
func updateRecords(c configuration) {
78-
clients, err := getPorkbunClients(c.PorkbunCredentials)
78+
clients, err := getPorkbunClients(c.PorkbunCredentials, c.Timeout)
7979
if err != nil {
8080
log.Fatal(err)
8181
}

porkbun.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"strconv"
8+
"time"
89

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

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

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

86-
func getPorkbunClient(credentials PorkbunCredentials, credentialsName string) (*porkbun.Client, error) {
87+
func getPorkbunClient(credentials PorkbunCredentials, credentialsName string, timeout time.Duration) (*porkbun.Client, error) {
8788
client := porkbun.New(credentials.PorkbunSecretKey, credentials.PorkbunAPIKey)
88-
89+
client.HTTPClient.Timeout = timeout
8990
ctx := context.Background()
9091

9192
yourIP, err := client.Ping(ctx)

0 commit comments

Comments
 (0)