Skip to content

Commit

Permalink
Merge branch 'develop' into urbs
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Apr 9, 2020
2 parents 52aad6e + b0c2c8c commit 92204b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 5 additions & 3 deletions pkg/middlewares/canary/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func (s *LabelStore) mustLoadEntry(key string, now time.Time) *entry {
s.mu.RLock()
e, ok := s.liveMap[key]
s.mu.RUnlock()
if ok && len(s.liveMap) <= s.maxCacheSize {

shouldRound := len(s.liveMap) > s.maxCacheSize || s.shouldRound.Before(now)
if ok && !shouldRound {
return e
}

Expand All @@ -127,12 +129,12 @@ func (s *LabelStore) mustLoadEntry(key string, now time.Time) *entry {
}
}

if !ok {
if !ok || e == nil {
e = &entry{}
s.liveMap[key] = e
}

if len(s.liveMap) > s.maxCacheSize || s.shouldRound.Before(now) {
if shouldRound {
s.logger.Infof("Round cache, stale cache size: %d, live cache size: %d", len(s.staleMap), len(s.liveMap))
s.shouldRound = now.Add(s.cacheCleanDuration)
// make a round: drop staleMap and create new liveMap
Expand Down
11 changes: 6 additions & 5 deletions pkg/middlewares/canary/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"runtime"
"sync/atomic"
Expand Down Expand Up @@ -88,12 +89,12 @@ type labelsRes struct {
Result []Label `json:"result"` // 空数组也保留
}

func getUserLabels(ctx context.Context, url, xRequestID string) (*labelsRes, error) {
func getUserLabels(ctx context.Context, api, xRequestID string) (*labelsRes, error) {
if ctx.Err() != nil {
return nil, nil
}

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
req, err := http.NewRequestWithContext(ctx, "GET", api, nil)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +116,7 @@ func getUserLabels(ctx context.Context, url, xRequestID string) (*labelsRes, err
req.Header.Set(headerXRequestID, xRequestID)
resp, err := client.Do(req)
if err != nil {
if err == context.Canceled {
if err.(*url.Error).Unwrap() == context.Canceled {
return nil, nil
}

Expand Down Expand Up @@ -144,12 +145,12 @@ func getUserLabels(ctx context.Context, url, xRequestID string) (*labelsRes, err
}

// MustGetUserLabels returns labels and timestamp
func MustGetUserLabels(ctx context.Context, url, xRequestID string, logger log.Logger) ([]Label, int64) {
func MustGetUserLabels(ctx context.Context, api, xRequestID string, logger log.Logger) ([]Label, int64) {
ts := time.Now().UTC().Unix()
rs := []Label{}

if hc.MaybeHealthy() {
if res, err := getUserLabels(ctx, url, xRequestID); err != nil {
if res, err := getUserLabels(ctx, api, xRequestID); err != nil {
logger.Error(err)
} else if res != nil {
rs = res.Result
Expand Down

0 comments on commit 92204b8

Please sign in to comment.