Skip to content

Commit 4342c67

Browse files
committed
use duration string instead of ms
1 parent 9f597f8 commit 4342c67

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

const.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package vxrouter
22

3-
// usefule constants for the whole project
3+
import (
4+
"time"
5+
)
6+
7+
// useful constants for the whole project
48
const (
5-
Version = "0.0.4"
6-
EnvPrefix = "VXR_"
7-
NetworkDriver = "vxrNet"
8-
IpamDriver = "vxrIpam"
9-
DefaultReqAddrSleepTimeMS = 100
10-
DefaultRouteProto = 192
9+
Version = "0.0.5"
10+
EnvPrefix = "VXR_"
11+
NetworkDriver = "vxrNet"
12+
IpamDriver = "vxrIpam"
13+
DefaultReqAddrSleepTime = 100 * time.Millisecond
14+
DefaultRouteProto = 192
1115
)

funcs.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ package vxrouter
33
import (
44
"os"
55
"strconv"
6+
"time"
67

78
log "github.com/Sirupsen/logrus"
89
)
910

10-
// GetEnvIntWithDefault gets value, prioritizing first opt, if it is not empty, then the environment variable specified by val, and lastly the default.
11-
func GetEnvIntWithDefault(val, opt string, def int) int { //nolint: unparam
11+
func getEnvOpt(val, opt string) string { //nolint: unparam
1212
e := os.Getenv(val)
1313
if e == "" {
1414
e = opt
1515
}
16+
return e
17+
}
18+
19+
// GetEnvIntWithDefault gets value, prioritizing first opt, if it is not empty, then the environment variable specified by val, and lastly the default.
20+
func GetEnvIntWithDefault(val, opt string, def int) int { //nolint: unparam
21+
e := getEnvOpt(val, opt)
1622
if e == "" {
1723
return def
1824
}
@@ -23,3 +29,17 @@ func GetEnvIntWithDefault(val, opt string, def int) int { //nolint: unparam
2329
}
2430
return ei
2531
}
32+
33+
// GetEnvDurWithDefault gets value, prioritizing first opt, if it is not empty, then the environment variable specified by val, and lastly the default.
34+
func GetEnvDurWithDefault(val, opt string, def time.Duration) time.Duration { //nolint: unparam
35+
e := getEnvOpt(val, opt)
36+
if e == "" {
37+
return def
38+
}
39+
ei, err := time.ParseDuration(e)
40+
if err != nil {
41+
log.WithField("string", e).WithError(err).Warnf("failed to convert string to duration, using default")
42+
return def
43+
}
44+
return ei
45+
}

host/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
rwm = make(map[string]*sync.RWMutex)
2020
rwmLock sync.Mutex
2121
routeProto = vxrouter.GetEnvIntWithDefault(vxrouter.EnvPrefix+"ROUTE_PROTO", "", vxrouter.DefaultRouteProto)
22-
reqAddrSleepTime = time.Duration(vxrouter.GetEnvIntWithDefault(vxrouter.EnvPrefix+"REQ_ADDR_SLEEP", "", vxrouter.DefaultReqAddrSleepTimeMS)) * time.Millisecond
22+
reqAddrSleepTime = vxrouter.GetEnvDurWithDefault(vxrouter.EnvPrefix+"REQ_ADDR_SLEEP", "", vxrouter.DefaultReqAddrSleepTime)
2323
)
2424

2525
// Interface holds a vxlan and a host macvlan interface used for the gateway interface on a container network

0 commit comments

Comments
 (0)