Skip to content

Commit

Permalink
update(wirey): set http timeouts via env
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan Sreerama committed Aug 20, 2024
1 parent 3a3d965 commit 0d8a406
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions backend/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"time"

"wirey/pkg/utils"
Expand All @@ -29,22 +31,46 @@ type HTTPBackend struct {

// NewHTTPBackend ...
func NewHTTPBackend(baseurl, wireyVersion string) (*HTTPBackend, error) {
defaultDialTimeout := 30 * time.Second
defaultTLSHandshakeTimeout := 30 * time.Second
defaultClientTimeout := 60 * time.Second

dialTimeout := getEnvAsDuration("DIAL_TIMEOUT", defaultDialTimeout)
tlsHandshakeTimeout := getEnvAsDuration("TLS_HANDSHAKE_TIMEOUT", defaultTLSHandshakeTimeout)
clientTimeout := getEnvAsDuration("CLIENT_TIMEOUT", defaultClientTimeout)

var transportWithTimeout = &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
Timeout: dialTimeout,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
TLSHandshakeTimeout: tlsHandshakeTimeout,
}

return &HTTPBackend{
client: &http.Client{
Timeout: time.Second * 60,
Timeout: clientTimeout,
Transport: transportWithTimeout,
},
baseurl: baseurl,
wireyVersion: wireyVersion,
}, nil
}

func getEnvAsDuration(envVar string, defaultValue time.Duration) time.Duration {
valueStr := os.Getenv(envVar)
if valueStr == "" {
return defaultValue
}

parsedValue, err := time.ParseDuration(valueStr)
if err != nil {
log.Printf("Invalid duration format for %s: %s, using default value %v\n", envVar, valueStr, defaultValue)
return defaultValue
}

return parsedValue
}

// Join ...
func (b *HTTPBackend) Join(ifname string, p Peer) error {
joinURL := fmt.Sprintf("%s/%s/%s", b.baseurl, ifname, utils.PublicKeySHA256(p.PublicKey))
Expand Down

0 comments on commit 0d8a406

Please sign in to comment.