Skip to content

Commit

Permalink
fixed label stores for share.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 6, 2020
1 parent 684dcd9 commit 089805e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/middlewares/canary/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func New(ctx context.Context, next http.Handler, cfg dynamic.Canary, name string
}

if c.loadLabels {
c.ls = NewLabelStore(logger, cfg, expiration, cacheCleanDuration)
c.ls = NewLabelStore(logger, cfg, expiration, cacheCleanDuration, name)
}
logger.Debugf("Add canary middleware: %v, %v, %v", cfg, expiration, cacheCleanDuration)
return c, nil
Expand Down Expand Up @@ -137,6 +137,7 @@ func (c *Canary) processRequestID(rw http.ResponseWriter, req *http.Request) {
}

if logData := accesslog.GetLogData(req); logData != nil {
logData.Core["XRealIp"] = req.Header.Get("X-Real-Ip")
logData.Core["XRequestID"] = requestID
logData.Core["UserAgent"] = req.Header.Get(headerUA)
logData.Core["Referer"] = req.Header.Get("Referer")
Expand Down
6 changes: 3 additions & 3 deletions pkg/middlewares/canary/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (l *Label) MatchChannel(channel string) bool {
}

// NewLabelStore ...
func NewLabelStore(logger log.Logger, cfg dynamic.Canary, expiration, cacheCleanDuration time.Duration) *LabelStore {
func NewLabelStore(logger log.Logger, cfg dynamic.Canary, expiration, cacheCleanDuration time.Duration, name string) *LabelStore {
product := cfg.Product
apiURL := cfg.Server
// apiURL ex. https://labelServerHost/api/labels?uid=%s&product=%s
Expand All @@ -86,7 +86,7 @@ func NewLabelStore(logger log.Logger, cfg dynamic.Canary, expiration, cacheClean

storesMu.Lock()
// LabelStores share Store with same apiURL, but always update Store'config to latest
s, ok := stores[apiURL]
s, ok := stores[name]
if !ok {
s = &Store{
maxCacheSize: cfg.MaxCacheSize,
Expand All @@ -95,7 +95,7 @@ func NewLabelStore(logger log.Logger, cfg dynamic.Canary, expiration, cacheClean
liveMap: make(map[string]*entry),
staleMap: make(map[string]*entry),
}
stores[apiURL] = s
stores[name] = s
} else {
s.updateConfig(cfg.MaxCacheSize, cacheCleanDuration)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/middlewares/canary/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestLabelStore(t *testing.T) {
a := assert.New(t)

cfg := dynamic.Canary{MaxCacheSize: 3, Server: "localhost1", Product: "T"}
ls := NewLabelStore(logrus.StandardLogger(), cfg, time.Second, time.Second*2)
ls := NewLabelStore(logrus.StandardLogger(), cfg, time.Second, time.Second*2, "canary-test")
ls.mustFetchLabels = func(ctx context.Context, uid, requestID string) ([]Label, int64) {
return []Label{{Label: requestID}}, time.Now().Unix()
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestLabelStore(t *testing.T) {
a := assert.New(t)

cfg := dynamic.Canary{MaxCacheSize: 3, Server: "localhost2", Product: "T"}
ls := NewLabelStore(logrus.StandardLogger(), cfg, time.Second, time.Second*2)
ls := NewLabelStore(logrus.StandardLogger(), cfg, time.Second, time.Second*2, "canary-test2")
ls.mustFetchLabels = func(ctx context.Context, uid, requestID string) ([]Label, int64) {
return []Label{{Label: requestID}}, time.Now().Unix()
}
Expand Down

0 comments on commit 089805e

Please sign in to comment.