Skip to content

Commit

Permalink
Merge pull request #33 from influxdata/rs/wirey-fixes
Browse files Browse the repository at this point in the history
update(timeouts): increases timeouts
  • Loading branch information
Rohan Sreerama authored Aug 21, 2024
2 parents ec76a43 + d01087a commit 15b9d32
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
7 changes: 6 additions & 1 deletion backend/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ type EtcdBackend struct {

// NewEtcdBackend ...
func NewEtcdBackend(endpoints []string) (*EtcdBackend, error) {
defaultDialTimeout := 5 * time.Second

dialTimeout := getEnvAsDuration("ETCD_DIAL_TIMEOUT", defaultDialTimeout)

cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
DialTimeout: dialTimeout,
})
if err != nil {
return nil, err
}

return &EtcdBackend{
client: cli,
}, nil
Expand Down
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: 5 * time.Second,
Timeout: dialTimeout,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
TLSHandshakeTimeout: tlsHandshakeTimeout,
}

return &HTTPBackend{
client: &http.Client{
Timeout: time.Second * 10,
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
38 changes: 33 additions & 5 deletions backend/plumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (

log "github.com/sirupsen/logrus"

"wirey/pkg/wireguard"

"github.com/cenkalti/backoff/v4"
"github.com/vishvananda/netlink"
"wirey/pkg/wireguard"
)

const (
Expand All @@ -36,13 +37,40 @@ const (
errIntConversionPort = "error during port conversion to int: %s"
)

var (
defaultMaxElapsedTime = 30 * time.Minute
defaultMaxInterval = 120 * time.Second
defaultJitterRange = 5
)

// values used for exponentialBackoff
const (
MaxElapsedTime = 15 * time.Minute
MaxInterval = 120 * time.Second
JitterRange = 5
var (
MaxElapsedTime time.Duration
MaxInterval time.Duration
JitterRange int
)

func init() {
MaxElapsedTime = getEnvAsDuration("MAX_ELAPSED_TIME", defaultMaxElapsedTime)
MaxInterval = getEnvAsDuration("MAX_INTERVAL", defaultMaxInterval)
JitterRange = getEnvAsInt("JITTER_RANGE", defaultJitterRange)
}

func getEnvAsInt(envVar string, defaultValue int) int {
valueStr := os.Getenv(envVar)
if valueStr == "" {
return defaultValue
}

parsedValue, err := strconv.Atoi(valueStr)
if err != nil {
log.Printf("Invalid integer format for %s: %s, using default value %d\n", envVar, valueStr, defaultValue)
return defaultValue
}

return parsedValue
}

// Peer ...
type Peer struct {
PublicKey []byte
Expand Down

0 comments on commit 15b9d32

Please sign in to comment.