aboutsummaryrefslogtreecommitdiff
path: root/internal/caching
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-06-12 11:07:26 +0100
committerGitHub <noreply@github.com>2020-06-12 11:07:26 +0100
commit079d8fe8fb521f76fee3bff5b47482d5fb911257 (patch)
tree964aacf3bdf2182a61eac14cd0e5e8e75187f7b7 /internal/caching
parentec7718e7f842fa0fc5198489c904de21003db4c2 (diff)
More key tweaks (#1116)
Diffstat (limited to 'internal/caching')
-rw-r--r--internal/caching/cache_serverkeys.go9
-rw-r--r--internal/caching/caches.go1
-rw-r--r--internal/caching/impl_inmemorylru.go7
3 files changed, 17 insertions, 0 deletions
diff --git a/internal/caching/cache_serverkeys.go b/internal/caching/cache_serverkeys.go
index 8c71ffbd..b5e31575 100644
--- a/internal/caching/cache_serverkeys.go
+++ b/internal/caching/cache_serverkeys.go
@@ -2,6 +2,7 @@ package caching
import (
"fmt"
+ "time"
"github.com/matrix-org/gomatrixserverlib"
)
@@ -23,9 +24,17 @@ func (c Caches) GetServerKey(
request gomatrixserverlib.PublicKeyLookupRequest,
) (gomatrixserverlib.PublicKeyLookupResult, bool) {
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
+ now := gomatrixserverlib.AsTimestamp(time.Now())
val, found := c.ServerKeys.Get(key)
if found && val != nil {
if keyLookupResult, ok := val.(gomatrixserverlib.PublicKeyLookupResult); ok {
+ if !keyLookupResult.WasValidAt(now, true) {
+ // We appear to be past the key validity so don't return this
+ // with the results. This ensures that the cache doesn't return
+ // values that are not useful to us.
+ c.ServerKeys.Unset(key)
+ return gomatrixserverlib.PublicKeyLookupResult{}, false
+ }
return keyLookupResult, true
}
}
diff --git a/internal/caching/caches.go b/internal/caching/caches.go
index 70f380ba..419623e2 100644
--- a/internal/caching/caches.go
+++ b/internal/caching/caches.go
@@ -12,4 +12,5 @@ type Caches struct {
type Cache interface {
Get(key string) (value interface{}, ok bool)
Set(key string, value interface{})
+ Unset(key string)
}
diff --git a/internal/caching/impl_inmemorylru.go b/internal/caching/impl_inmemorylru.go
index f7901d2e..158deca4 100644
--- a/internal/caching/impl_inmemorylru.go
+++ b/internal/caching/impl_inmemorylru.go
@@ -68,6 +68,13 @@ func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
c.lru.Add(key, value)
}
+func (c *InMemoryLRUCachePartition) Unset(key string) {
+ if !c.mutable {
+ panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %q", key))
+ }
+ c.lru.Remove(key)
+}
+
func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool) {
return c.lru.Get(key)
}