Skip to content

Commit

Permalink
feat: support select ip randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed May 22, 2019
1 parent 34a6aae commit 3014194
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions diamond.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aliacm

import (
"math/rand"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -63,6 +64,7 @@ type Diamond struct {
c *cast.Cast
units []Unit
errHook Hook
r *rand.Rand
}

// New 产生Diamond实例
Expand All @@ -85,9 +87,14 @@ func New(addr, tenant, accessKey, secretKey string) (*Diamond, error) {
if err != nil {
return nil, err
}

s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)

d := &Diamond{
option: option,
c: c,
r: r,
}
return d, nil
}
Expand Down
9 changes: 6 additions & 3 deletions query_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func (d *Diamond) QueryIP() (string, error) {
if !response.Success() {
return "", errors.New(response.String())
}
ips := strings.Split(response.String(), "\n")
// TODO: randomly select one from ip list?
return ips[0], nil
ips := strings.Split(strings.TrimSpace(response.String()), "\n")
if len(ips) > 0 {
idx := d.r.Intn(len(ips))
return ips[idx], nil
}
return "", nil
}
4 changes: 3 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aliacm

import "time"
import (
"time"
)

const (
apiTimeout = 3 * time.Second
Expand Down

0 comments on commit 3014194

Please sign in to comment.