-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 554 Added compiled glob matching using LRU Cache (#615)
* consul: refactor service monitor Refactor the set of functions which watch the consul state and generate the route commands into a set of objects to make them testable and extendable. * consul: move build route command logic to separate object ... and finally add some tests. * consul: fetch route updates concurrently The code which updates the routing table from consul was using a single go routine to fetch data from consul. This can be a slow process if consul has lots of registered services. This patch adds an option `registry.consul.serviceMonitors` to increase the concurrency for the route updates. * issue - 558 updated to include custom http.Transport * added global polling interval for issue 558 * issue 558 updates and doco adds * added compiled glob matching cache using LRU cache * updated glob_cache to use sync.Map * issue #554 refactored to use sync.Map instead of RWmutex locks * updated glob_cache to support type casting of glob.Glob Interface * added type assertion of glob.Glob interface instead of type switch * rebased to master and fixed issue with load_test * added Glob to grpc proxy * removing polling interval * updated tests and remove PollingInterval Co-authored-by: Frank Schröder <frank.schroeder@gmail.com> Co-authored-by: Aaron Hurt <ahurt@anbcs.com>
- Loading branch information
1 parent
dfee47a
commit 828d81f
Showing
18 changed files
with
216 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,4 +103,6 @@ var defaultConfig = &Config{ | |
SpanHost: "localhost:9998", | ||
TraceID128Bit: true, | ||
}, | ||
|
||
GlobCacheSize: 1000, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: "glob.cache.size" | ||
--- | ||
|
||
`glob.cache.size` Sets the globCache size used for matching on route lookups. | ||
|
||
The default is | ||
|
||
glob.cache.size = 1000 |
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package route | ||
|
||
import ( | ||
"github.com/gobwas/glob" | ||
"sync" | ||
) | ||
|
||
// GlobCache implements an LRU cache for compiled glob patterns. | ||
type GlobCache struct { | ||
// m maps patterns to compiled glob matchers. | ||
m sync.Map | ||
|
||
// l contains the added patterns and serves as an LRU cache. | ||
// l has a fixed size and is initialized in the constructor. | ||
l []string | ||
|
||
// h is the first element in l. | ||
h int | ||
|
||
// n is the number of elements in l. | ||
n int | ||
} | ||
|
||
func NewGlobCache(size int) *GlobCache { | ||
return &GlobCache{ | ||
l: make([]string, size), | ||
} | ||
} | ||
|
||
// Get returns the compiled glob pattern if it compiled without | ||
// error. Otherwise, the function returns nil. If the pattern | ||
// is not in the cache it will be added. | ||
func (c *GlobCache) Get(pattern string) (glob.Glob, error) { | ||
// fast path | ||
if glb, ok := c.m.Load(pattern); ok { | ||
//Type Assert the returned interface{} | ||
return glb.(glob.Glob), nil | ||
} | ||
|
||
// try to compile pattern | ||
glbCompiled, err := glob.Compile(pattern) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// if the LRU buffer is not full just append | ||
// the element to the buffer. | ||
if c.n < len(c.l) { | ||
c.m.Store(pattern, glbCompiled) | ||
c.l[c.n] = pattern | ||
c.n++ | ||
return glbCompiled, nil | ||
} | ||
|
||
// otherwise, remove the oldest element and move | ||
// the head. Note that once the buffer is full | ||
// (c.n == len(c.l)) it will never become smaller | ||
// again. | ||
// TODO add logging for cache full - How will this impact performance | ||
c.m.Delete(c.l[c.h]) | ||
c.m.Store(pattern, glbCompiled) | ||
c.l[c.h] = pattern | ||
c.h = (c.h + 1) % c.n | ||
return glbCompiled, nil | ||
} |
Oops, something went wrong.