Skip to content

Commit 09d0807

Browse files
committed
Updates Sigv4 signer to only cache up to 50 service clients at a time
1 parent d535f96 commit 09d0807

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/signers/v4.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ var inherit = AWS.util.inherit;
66
*/
77
var cachedSecret = {};
88

9+
/**
10+
* @api private
11+
*/
12+
var cacheQueue = [];
13+
14+
/**
15+
* @api private
16+
*/
17+
var maxCacheEntries = 50;
18+
919
/**
1020
* @api private
1121
*/
@@ -100,6 +110,15 @@ AWS.Signers.V4 = inherit(AWS.Signers.RequestSigner, {
100110
var cacheIdentifier = this.serviceName + (this.getServiceClientId() ? '_' + this.getServiceClientId() : '');
101111
if (this.signatureCache) {
102112
var cache = cachedSecret[cacheIdentifier];
113+
// If there isn't already a cache entry, we'll be adding one
114+
if (!cache) {
115+
cacheQueue.push(cacheIdentifier);
116+
if (cacheQueue.length > maxCacheEntries) {
117+
// remove the oldest entry (may not be last one used)
118+
delete cachedSecret[cacheQueue.shift()];
119+
}
120+
}
121+
103122
}
104123
var date = datetime.substr(0, 8);
105124

test/signers/v4.spec.coffee

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,25 @@ describe 'AWS.Signers.V4', ->
8484
calls = AWS.util.crypto.hmac.calls
8585
callCount = calls.length
8686

87-
87+
it 'will cache a maximum of 50 clients', (done) ->
88+
maxCacheEntries = 50
89+
clientSigners = (buildSignerFromService() for i in [0..maxCacheEntries-1])
90+
callCount = calls.length
91+
#Get signature for all clients to store them in cache
92+
(clientSigners[i].signature(creds, datetime) for i in [0..clientSigners.length-1])
93+
expect(calls.length).to.equal(callCount + (5 * maxCacheEntries))
94+
#Signer should use cache
95+
callCount = calls.length
96+
clientSigners[0].signature(creds, datetime)
97+
expect(calls.length).to.equal(callCount + 1)
98+
#add a new signer, pushing past cache limit
99+
newestSigner = buildSignerFromService()
100+
#old signer shouldn't be using cache anymore
101+
callCount = calls.length
102+
clientSigners[0].signature(creds, datetime)
103+
expect(calls.length).to.equal(callCount + 5)
104+
done()
105+
88106
#Calling signer.signature should call hmac 1 time when caching, and 5 times when not caching
89107
it 'caches subsequent requests', ->
90108
signer.signature(creds, datetime)

0 commit comments

Comments
 (0)