-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use sync.Map in the endpointregistry #2795
Merged
+254
−99
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package routing | |
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/zalando/skipper/eskip" | ||
|
@@ -13,32 +14,65 @@ const defaultLastSeenTimeout = 1 * time.Minute | |
// used to perform better load balancing, fadeIn, etc. | ||
type Metrics interface { | ||
DetectedTime() time.Time | ||
SetDetected(detected time.Time) | ||
|
||
LastSeen() time.Time | ||
SetLastSeen(lastSeen time.Time) | ||
|
||
InflightRequests() int64 | ||
IncInflightRequest() | ||
DecInflightRequest() | ||
} | ||
|
||
type entry struct { | ||
detected time.Time | ||
inflightRequests int64 | ||
detected atomic.Value // time.Time | ||
lastSeen atomic.Value // time.Time | ||
inflightRequests atomic.Int64 | ||
} | ||
|
||
var _ Metrics = &entry{} | ||
|
||
func (e *entry) DetectedTime() time.Time { | ||
return e.detected | ||
return e.detected.Load().(time.Time) | ||
AlexanderYastrebov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (e *entry) LastSeen() time.Time { | ||
return e.lastSeen.Load().(time.Time) | ||
} | ||
|
||
func (e *entry) InflightRequests() int64 { | ||
return e.inflightRequests | ||
return e.inflightRequests.Load() | ||
} | ||
|
||
func (e *entry) IncInflightRequest() { | ||
e.inflightRequests.Add(1) | ||
} | ||
|
||
func (e *entry) DecInflightRequest() { | ||
e.inflightRequests.Add(-1) | ||
} | ||
|
||
func (e *entry) SetDetected(detected time.Time) { | ||
e.detected.Store(detected) | ||
} | ||
|
||
func (e *entry) SetLastSeen(ts time.Time) { | ||
e.lastSeen.Store(ts) | ||
} | ||
|
||
func newEntry() (result *entry) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why named return value?
|
||
result = &entry{} | ||
result.SetDetected(time.Time{}) | ||
result.SetLastSeen(time.Time{}) | ||
return | ||
} | ||
|
||
type EndpointRegistry struct { | ||
lastSeen map[string]time.Time | ||
lastSeenTimeout time.Duration | ||
now func() time.Time | ||
|
||
mu sync.Mutex | ||
|
||
data map[string]*entry | ||
// map[string]*entry | ||
data sync.Map | ||
} | ||
|
||
var _ PostProcessor = &EndpointRegistry{} | ||
|
@@ -55,24 +89,23 @@ func (r *EndpointRegistry) Do(routes []*Route) []*Route { | |
for _, epi := range route.LBEndpoints { | ||
metrics := r.GetMetrics(epi.Host) | ||
if metrics.DetectedTime().IsZero() { | ||
r.SetDetectedTime(epi.Host, now) | ||
metrics.SetDetected(now) | ||
RomanZavodskikh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
r.lastSeen[epi.Host] = now | ||
metrics.SetLastSeen(now) | ||
} | ||
} | ||
} | ||
|
||
for host, ts := range r.lastSeen { | ||
if ts.Add(r.lastSeenTimeout).Before(now) { | ||
r.mu.Lock() | ||
if r.data[host].inflightRequests == 0 { | ||
delete(r.lastSeen, host) | ||
delete(r.data, host) | ||
} | ||
r.mu.Unlock() | ||
removeOlder := now.Add(-r.lastSeenTimeout) | ||
r.data.Range(func(key, value any) bool { | ||
e := value.(*entry) | ||
if e.LastSeen().Before(removeOlder) { | ||
r.data.Delete(key) | ||
szuecs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return true | ||
}) | ||
|
||
return routes | ||
} | ||
|
@@ -82,58 +115,16 @@ func NewEndpointRegistry(o RegistryOptions) *EndpointRegistry { | |
o.LastSeenTimeout = defaultLastSeenTimeout | ||
} | ||
|
||
return &EndpointRegistry{ | ||
data: map[string]*entry{}, | ||
lastSeen: map[string]time.Time{}, | ||
result := &EndpointRegistry{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why store |
||
data: sync.Map{}, | ||
lastSeenTimeout: o.LastSeenTimeout, | ||
now: time.Now, | ||
} | ||
} | ||
|
||
func (r *EndpointRegistry) GetMetrics(key string) Metrics { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
e := r.getOrInitEntryLocked(key) | ||
copy := &entry{} | ||
*copy = *e | ||
return copy | ||
} | ||
|
||
func (r *EndpointRegistry) SetDetectedTime(key string, detected time.Time) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
e := r.getOrInitEntryLocked(key) | ||
e.detected = detected | ||
return result | ||
} | ||
|
||
func (r *EndpointRegistry) IncInflightRequest(key string) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
e := r.getOrInitEntryLocked(key) | ||
e.inflightRequests++ | ||
} | ||
|
||
func (r *EndpointRegistry) DecInflightRequest(key string) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
e := r.getOrInitEntryLocked(key) | ||
e.inflightRequests-- | ||
} | ||
|
||
// getOrInitEntryLocked returns pointer to endpoint registry entry | ||
// which contains the information about endpoint representing the | ||
// following key. r.mu must be held while calling this function and | ||
// using of the entry returned. In general, key represents the "host:port" | ||
// string | ||
func (r *EndpointRegistry) getOrInitEntryLocked(key string) *entry { | ||
e, ok := r.data[key] | ||
if !ok { | ||
e = &entry{} | ||
r.data[key] = e | ||
} | ||
return e | ||
func (r *EndpointRegistry) GetMetrics(key string) Metrics { | ||
e, _ := r.data.LoadOrStore(key, newEntry()) | ||
szuecs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return e.(*entry) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes no sense as this registry instance will not be visible outside of this postprocessor.
The code should call GetMetrics only when registry is not nil instead.