Skip to content

Commit

Permalink
fix: long pull rate
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Jun 27, 2019
1 parent be29f44 commit 7aba7ac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
acmDeleteConfig URL = "/diamond-server/datum.do?method=deleteAllDatums"
acmPublishConfig URL = "/diamond-server/basestone.do?method=syncUpdateAll"
acmAllConfig URL = "/diamond-server/basestone.do?method=getAllConfigByTenant"
acmLongPoll URL = "/diamond-server/config.co"
acmLongPull URL = "/diamond-server/config.co"
)

func (u URL) String(addr string) string {
Expand Down
33 changes: 23 additions & 10 deletions diamond.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math/rand"
"time"

"go.uber.org/ratelimit"

"github.com/sirupsen/logrus"

"github.com/xiaojiaoyu100/cast"
Expand Down Expand Up @@ -60,15 +62,17 @@ type Config struct {

// Diamond 提供了操作阿里云ACM的能力
type Diamond struct {
option Option
c *cast.Cast
units []Unit
errHook Hook
r *rand.Rand
option Option
c *cast.Cast
units []Unit
errHook Hook
r *rand.Rand
longPullRate int
longPullRateLimiter ratelimit.Limiter
}

// New 产生Diamond实例
func New(addr, tenant, accessKey, secretKey string) (*Diamond, error) {
func New(addr, tenant, accessKey, secretKey string, setters ...Setter) (*Diamond, error) {
option := Option{
addr: addr,
tenant: tenant,
Expand All @@ -87,15 +91,24 @@ 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,
option: option,
c: c,
r: r,
longPullRate: 6,
}

for _, setter := range setters {
if err := setter(d); err != nil {
return nil, err
}
}

d.longPullRateLimiter = ratelimit.New(d.longPullRate)

return d, nil
}

Expand Down
3 changes: 2 additions & 1 deletion long_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (

// LongPull 监听配置
func (d *Diamond) LongPull(unit Unit, contentMD5 string) (string, error) {
d.longPullRateLimiter.Take()
ip, err := d.QueryIP()
if err != nil {
return "", err
Expand All @@ -33,7 +34,7 @@ func (d *Diamond) LongPull(unit Unit, contentMD5 string) (string, error) {
}
longPollRequest.ProbeModifyRequest = strings.Join([]string{unit.DataID, unit.Group, contentMD5, d.option.tenant}, wordSeparator) + lineSeparator
request := d.c.NewRequest().
WithPath(acmLongPoll.String(ip)).
WithPath(acmLongPull.String(ip)).
WithFormURLEncodedBody(longPollRequest).
WithHeader(header).
Post()
Expand Down
12 changes: 12 additions & 0 deletions setter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aliacm

// Setter configures the diamond.
type Setter func(d *Diamond) error

// WithLongPullRate sets long pull rate.
func WithLongPullRate(rate int) Setter {
return func(d *Diamond) error {
d.longPullRate = rate
return nil
}
}

0 comments on commit 7aba7ac

Please sign in to comment.