Skip to content

Commit 089805e

Browse files
committed
fixed label stores for share.
1 parent 684dcd9 commit 089805e

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

pkg/middlewares/canary/canary.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func New(ctx context.Context, next http.Handler, cfg dynamic.Canary, name string
9898
}
9999

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

139139
if logData := accesslog.GetLogData(req); logData != nil {
140+
logData.Core["XRealIp"] = req.Header.Get("X-Real-Ip")
140141
logData.Core["XRequestID"] = requestID
141142
logData.Core["UserAgent"] = req.Header.Get(headerUA)
142143
logData.Core["Referer"] = req.Header.Get("Referer")

pkg/middlewares/canary/label.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (l *Label) MatchChannel(channel string) bool {
7373
}
7474

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

8787
storesMu.Lock()
8888
// LabelStores share Store with same apiURL, but always update Store'config to latest
89-
s, ok := stores[apiURL]
89+
s, ok := stores[name]
9090
if !ok {
9191
s = &Store{
9292
maxCacheSize: cfg.MaxCacheSize,
@@ -95,7 +95,7 @@ func NewLabelStore(logger log.Logger, cfg dynamic.Canary, expiration, cacheClean
9595
liveMap: make(map[string]*entry),
9696
staleMap: make(map[string]*entry),
9797
}
98-
stores[apiURL] = s
98+
stores[name] = s
9999
} else {
100100
s.updateConfig(cfg.MaxCacheSize, cacheCleanDuration)
101101
}

pkg/middlewares/canary/label_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestLabelStore(t *testing.T) {
3939
a := assert.New(t)
4040

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

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

0 commit comments

Comments
 (0)