Skip to content

Commit

Permalink
Make timeouts and intervals configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Jan 21, 2025
1 parent b33709e commit 69ed28d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 25 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log/slog"
"net/http"
"os"
Expand All @@ -12,28 +11,44 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
fetchInterval = 120 * time.Second // time to sleep after every metrics fetch
updateTimeout = 90 * time.Second // maximum time for metal-api to respond to all our requests
)

var (
client metalgo.Client
)

func main() {
envOrDefault := func(key, fallback string) string {
if val := os.Getenv(key); val != "" {
return val
}
return fallback
}

var (
log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))

url = os.Getenv("METAL_API_URL")
hmac = os.Getenv("METAL_API_HMAC")
url = os.Getenv("METAL_API_URL")
hmac = os.Getenv("METAL_API_HMAC")
fetchIntervalEnv = envOrDefault("FETCH_INTERVAL", "90s") // time to sleep after every metrics fetch
updateTimeoutEnv = envOrDefault("UPDATE_TIMEOUT", "60s") // maximum time for metal-api to respond to all our requests until context gets cancelled

err error
)

client, err = metalgo.NewDriver(url, "", hmac)
if err != nil {
fmt.Print(err)
log.Error("error creating client", "error", err)
os.Exit(1)
}

fetchInterval, err := time.ParseDuration(fetchIntervalEnv)
if err != nil {
log.Error("error parsing fetch interval", "error", err)
os.Exit(1)
}

updateTimeout, err := time.ParseDuration(updateTimeoutEnv)
if err != nil {
log.Error("error parsing update timeout", "error", err)
os.Exit(1)
}

Expand All @@ -47,7 +62,7 @@ func main() {
log.Info("updating metrics...")
start := time.Now()

err = update()
err = update(updateTimeout)
if err != nil {
if !initialUpdateSuccess {
log.Error("error during initial update", "error", err)
Expand Down
2 changes: 1 addition & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"golang.org/x/sync/errgroup"
)

func update() error {
func update(updateTimeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), updateTimeout)
defer cancel()

Expand Down

0 comments on commit 69ed28d

Please sign in to comment.