-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache for workflow specific in memory data (#5594)
What changed? Adding a struct for storing in memory workflow specific data. It will first be used for storing workflow specific rate limiters, so we can rate limit too many requests to the same workflowID. In the future it could store e.g. the current state of the workflow (running, terminated), query answers etc. Why? This is part of the general workflow specific rate limits project How did you test it? Unit tests Potential risks No risk, just adding the struct Release notes Documentation Changes
- Loading branch information
Showing
6 changed files
with
394 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,107 @@ | ||
// The MIT License (MIT) | ||
|
||
// Copyright (c) 2017-2020 Uber Technologies Inc. | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package workflowcache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/uber/cadence/common/cache" | ||
"github.com/uber/cadence/common/quotas" | ||
) | ||
|
||
// WFCache is a per workflow cache used for workflow specific in memory data | ||
type WFCache interface { | ||
AllowExternal(domainID string, workflowID string) bool | ||
AllowInternal(domainID string, workflowID string) bool | ||
} | ||
|
||
type wfCache struct { | ||
lru cache.Cache | ||
externalLimiterFactory quotas.LimiterFactory | ||
internalLimiterFactory quotas.LimiterFactory | ||
} | ||
|
||
type cacheKey struct { | ||
domainID string | ||
workflowID string | ||
} | ||
|
||
type cacheValue struct { | ||
externalRateLimiter quotas.Limiter | ||
internalRateLimiter quotas.Limiter | ||
} | ||
|
||
// Params is the parameters for a new WFCache | ||
type Params struct { | ||
TTL time.Duration | ||
MaxCount int | ||
ExternalLimiterFactory quotas.LimiterFactory | ||
InternalLimiterFactory quotas.LimiterFactory | ||
} | ||
|
||
// New creates a new WFCache | ||
func New(params Params) WFCache { | ||
return &wfCache{ | ||
lru: cache.New(&cache.Options{ | ||
TTL: params.TTL, | ||
Pin: true, | ||
MaxCount: params.MaxCount, | ||
}), | ||
externalLimiterFactory: params.ExternalLimiterFactory, | ||
internalLimiterFactory: params.InternalLimiterFactory, | ||
} | ||
} | ||
|
||
// AllowExternal returns true if the rate limiter for this domain/workflow allows an external request | ||
func (c *wfCache) AllowExternal(domainID string, workflowID string) bool { | ||
// Locking is not needed because both getCacheItem and the rate limiter are thread safe | ||
value := c.getCacheItem(domainID, workflowID) | ||
return value.externalRateLimiter.Allow() | ||
} | ||
|
||
// AllowInternal returns true if the rate limiter for this domain/workflow allows an internal request | ||
func (c *wfCache) AllowInternal(domainID string, workflowID string) bool { | ||
// Locking is not needed because both getCacheItem and the rate limiter are thread safe | ||
value := c.getCacheItem(domainID, workflowID) | ||
return value.internalRateLimiter.Allow() | ||
} | ||
|
||
func (c *wfCache) getCacheItem(domainID string, workflowID string) *cacheValue { | ||
// The underlying lru cache is thread safe, so there is no need to lock | ||
key := cacheKey{ | ||
domainID: domainID, | ||
workflowID: workflowID, | ||
} | ||
|
||
value, ok := c.lru.Get(key).(*cacheValue) | ||
|
||
if !ok { | ||
value = &cacheValue{ | ||
externalRateLimiter: c.externalLimiterFactory.GetLimiter(domainID), | ||
internalRateLimiter: c.internalLimiterFactory.GetLimiter(domainID), | ||
} | ||
c.lru.PutIfNotExist(key, value) | ||
} | ||
|
||
return value | ||
} |
Oops, something went wrong.