From 64401efa20de26cb0b69236af2c4761766281a31 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 14 May 2020 18:47:45 +0100 Subject: [PATCH] Implement key caching wrapper --- common/caching/immutablecache.go | 3 +- common/keydb/cache/keydb.go | 69 ++++++++++++++++++++++++++++++++ common/keydb/postgres/keydb.go | 2 +- common/keydb/sqlite3/keydb.go | 2 +- go.mod | 2 +- go.sum | 2 - 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 common/keydb/cache/keydb.go diff --git a/common/caching/immutablecache.go b/common/caching/immutablecache.go index 2d0149639..362e4349a 100644 --- a/common/caching/immutablecache.go +++ b/common/caching/immutablecache.go @@ -8,7 +8,8 @@ const ( ) type ImmutableCache interface { - gomatrixserverlib.KeyCache GetRoomVersion(roomId string) (gomatrixserverlib.RoomVersion, bool) StoreRoomVersion(roomId string, roomVersion gomatrixserverlib.RoomVersion) + GetServerKey(request gomatrixserverlib.PublicKeyLookupRequest) (gomatrixserverlib.PublicKeyLookupResult, bool) + StoreServerKey(request gomatrixserverlib.PublicKeyLookupRequest, response gomatrixserverlib.PublicKeyLookupResult) } diff --git a/common/keydb/cache/keydb.go b/common/keydb/cache/keydb.go new file mode 100644 index 000000000..4213dc079 --- /dev/null +++ b/common/keydb/cache/keydb.go @@ -0,0 +1,69 @@ +package cache + +import ( + "context" + "errors" + + "github.com/matrix-org/dendrite/common/caching" + "github.com/matrix-org/dendrite/common/keydb" + "github.com/matrix-org/gomatrixserverlib" +) + +// A Database implements gomatrixserverlib.KeyDatabase and is used to store +// the public keys for other matrix servers. +type Database struct { + inner keydb.Database + cache caching.ImmutableCache +} + +func NewDatabase(inner keydb.Database, cache caching.ImmutableCache) (*Database, error) { + if inner == nil { + return nil, errors.New("inner database can't be nil") + } + if cache == nil { + return nil, errors.New("cache can't be nil") + } + return &Database{ + inner: inner, + cache: cache, + }, nil +} + +// FetcherName implements KeyFetcher +func (d Database) FetcherName() string { + return "InMemoryKeyCache" +} + +// FetchKeys implements gomatrixserverlib.KeyDatabase +func (d *Database) FetchKeys( + ctx context.Context, + requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp, +) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) { + results := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult) + for req := range requests { + if res, cached := d.cache.GetServerKey(req); cached { + results[req] = res + delete(requests, req) + } + } + fromDB, err := d.inner.FetchKeys(ctx, requests) + if err != nil { + return results, err + } + for req, res := range fromDB { + results[req] = res + d.cache.StoreServerKey(req, res) + } + return results, nil +} + +// StoreKeys implements gomatrixserverlib.KeyDatabase +func (d *Database) StoreKeys( + ctx context.Context, + keyMap map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, +) error { + for req, res := range keyMap { + d.cache.StoreServerKey(req, res) + } + return d.inner.StoreKeys(ctx, keyMap) +} diff --git a/common/keydb/postgres/keydb.go b/common/keydb/postgres/keydb.go index 6149d8778..706ca0052 100644 --- a/common/keydb/postgres/keydb.go +++ b/common/keydb/postgres/keydb.go @@ -79,7 +79,7 @@ func NewDatabase( // FetcherName implements KeyFetcher func (d Database) FetcherName() string { - return "KeyDatabase" + return "PostgresKeyDatabase" } // FetchKeys implements gomatrixserverlib.KeyDatabase diff --git a/common/keydb/sqlite3/keydb.go b/common/keydb/sqlite3/keydb.go index 1405836a4..94a32e29f 100644 --- a/common/keydb/sqlite3/keydb.go +++ b/common/keydb/sqlite3/keydb.go @@ -80,7 +80,7 @@ func NewDatabase( // FetcherName implements KeyFetcher func (d Database) FetcherName() string { - return "KeyDatabase" + return "SqliteKeyDatabase" } // FetchKeys implements gomatrixserverlib.KeyDatabase diff --git a/go.mod b/go.mod index beb1ff020..8e6d63cd5 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/matrix-org/go-http-js-libp2p v0.0.0-20200318135427-31631a9ef51f github.com/matrix-org/go-sqlite3-js v0.0.0-20200325174927-327088cdef10 github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 - github.com/matrix-org/gomatrixserverlib v0.0.0-20200514163156-a69606a3855c + github.com/matrix-org/gomatrixserverlib v0.0.0-20200511154227-5cc71d36632b github.com/matrix-org/naffka v0.0.0-20200422140631-181f1ee7401f github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7 github.com/mattn/go-sqlite3 v2.0.2+incompatible diff --git a/go.sum b/go.sum index dd2f30326..53aefed37 100644 --- a/go.sum +++ b/go.sum @@ -369,8 +369,6 @@ github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5 h1:km github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= github.com/matrix-org/gomatrixserverlib v0.0.0-20200511154227-5cc71d36632b h1:nAmSc1KvQOumoRTz/LD68KyrB6Q5/6q7CmQ5Bswc2nM= github.com/matrix-org/gomatrixserverlib v0.0.0-20200511154227-5cc71d36632b/go.mod h1:JsAzE1Ll3+gDWS9JSUHPJiiyAksvOOnGWF2nXdg4ZzU= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200514163156-a69606a3855c h1:S0+PWkxoUl1/wA1o6OcCXQGpjc64E/sQB+QU/6VCGMc= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200514163156-a69606a3855c/go.mod h1:JsAzE1Ll3+gDWS9JSUHPJiiyAksvOOnGWF2nXdg4ZzU= github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1 h1:osLoFdOy+ChQqVUn2PeTDETFftVkl4w9t/OW18g3lnk= github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1/go.mod h1:cXoYQIENbdWIQHt1SyCo6Bl3C3raHwJ0wgVrXHSqf+A= github.com/matrix-org/naffka v0.0.0-20200422140631-181f1ee7401f h1:pRz4VTiRCO4zPlEMc3ESdUOcW4PXHH4Kj+YDz1XyE+Y=