1
// Copyright (c) 2016-2019 Uber Technologies, Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
23
// _supportedInterfaces is an ordered list of ip interfaces from which
24
// host ip is determined.
25
var _supportedInterfaces = []string{"eth0", "ib0"}
27
func min(a, b time.Duration) time.Duration {
34
// WithRetry executes f maxRetries times until it returns non-nil error, sleeping
35
// for the given delay between retries with exponential backoff until maxDelay is
37
func WithRetry(maxRetries uint, delay time.Duration, maxDelay time.Duration, f func() error) error {
44
if retries > maxRetries {
47
time.Sleep(min(delay*(1<<retries), maxDelay))
52
// GetIP looks up the ip of host.
53
func GetIP(host string) (net.IP, error) {
54
ips, err := net.LookupIP(host)
56
return nil, fmt.Errorf("net: %s", err)
58
for _, ip := range ips {
59
if ip == nil || ip.IsLoopback() {
64
return nil, errors.New("no ips found")
67
// GetLocalIP returns the ip address of the local machine.
68
func GetLocalIP() (string, error) {
69
ifaces, err := net.Interfaces()
71
return "", fmt.Errorf("interfaces: %s", err)
73
ips := map[string]string{}
74
for _, i := range ifaces {
75
addrs, err := i.Addrs()
77
return "", fmt.Errorf("addrs: %s", err)
79
for _, addr := range addrs {
81
switch v := addr.(type) {
87
if ip == nil || ip.IsLoopback() {
94
ips[i.Name] = ip.String()
98
for _, i := range _supportedInterfaces {
99
if ip, ok := ips[i]; ok {
103
return "", errors.New("no ip found")