aboutsummaryrefslogtreecommitdiff
path: root/roomserver
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-04-22 13:00:05 +0100
committerGitHub <noreply@github.com>2020-04-22 13:00:05 +0100
commita466e9e9cc86e04b4d28de45cd44199916664768 (patch)
tree70e234ac80ade783acdccadb24f27395712b5748 /roomserver
parent71f9d35b7c2f1531b383f115ad3ab23e8d1ed0a5 (diff)
LRU cache for room versions in RS query API (#976)
* Experimental LRU cache for room versions * Don't accidentally try to type-assert nil * Also reduce hits on query API * Use hashicorp implementation which mutexes for us * Define const for max cache entries * Rename to be specifically immutable, panic if we try to mutate a cache entry * Review comments * Remove nil guards, give roomserver integration test a cache * go mod tidy
Diffstat (limited to 'roomserver')
-rw-r--r--roomserver/api/query.go21
-rw-r--r--roomserver/query/query.go10
-rw-r--r--roomserver/roomserver.go5
3 files changed, 29 insertions, 7 deletions
diff --git a/roomserver/api/query.go b/roomserver/api/query.go
index 5f024d26..b272b1eb 100644
--- a/roomserver/api/query.go
+++ b/roomserver/api/query.go
@@ -21,6 +21,7 @@ import (
"errors"
"net/http"
+ "github.com/matrix-org/dendrite/common/caching"
commonHTTP "github.com/matrix-org/dendrite/common/http"
"github.com/matrix-org/gomatrixserverlib"
opentracing "github.com/opentracing/opentracing-go"
@@ -411,16 +412,17 @@ const RoomserverQueryRoomVersionForRoomPath = "/api/roomserver/queryRoomVersionF
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
// If httpClient is nil an error is returned
-func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) (RoomserverQueryAPI, error) {
+func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client, cache caching.ImmutableCache) (RoomserverQueryAPI, error) {
if httpClient == nil {
return nil, errors.New("NewRoomserverQueryAPIHTTP: httpClient is <nil>")
}
- return &httpRoomserverQueryAPI{roomserverURL, httpClient}, nil
+ return &httpRoomserverQueryAPI{roomserverURL, httpClient, cache}, nil
}
type httpRoomserverQueryAPI struct {
- roomserverURL string
- httpClient *http.Client
+ roomserverURL string
+ httpClient *http.Client
+ immutableCache caching.ImmutableCache
}
// QueryLatestEventsAndState implements RoomserverQueryAPI
@@ -585,9 +587,18 @@ func (h *httpRoomserverQueryAPI) QueryRoomVersionForRoom(
request *QueryRoomVersionForRoomRequest,
response *QueryRoomVersionForRoomResponse,
) error {
+ if roomVersion, ok := h.immutableCache.GetRoomVersion(request.RoomID); ok {
+ response.RoomVersion = roomVersion
+ return nil
+ }
+
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoom")
defer span.Finish()
apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath
- return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
+ err := commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
+ if err == nil {
+ h.immutableCache.StoreRoomVersion(request.RoomID, response.RoomVersion)
+ }
+ return err
}
diff --git a/roomserver/query/query.go b/roomserver/query/query.go
index 12d8436e..224d9fa2 100644
--- a/roomserver/query/query.go
+++ b/roomserver/query/query.go
@@ -22,6 +22,7 @@ import (
"net/http"
"github.com/matrix-org/dendrite/common"
+ "github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/auth"
"github.com/matrix-org/dendrite/roomserver/state"
@@ -97,7 +98,8 @@ type RoomserverQueryAPIDatabase interface {
// RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI
type RoomserverQueryAPI struct {
- DB RoomserverQueryAPIDatabase
+ DB RoomserverQueryAPIDatabase
+ ImmutableCache caching.ImmutableCache
}
// QueryLatestEventsAndState implements api.RoomserverQueryAPI
@@ -896,11 +898,17 @@ func (r *RoomserverQueryAPI) QueryRoomVersionForRoom(
request *api.QueryRoomVersionForRoomRequest,
response *api.QueryRoomVersionForRoomResponse,
) error {
+ if roomVersion, ok := r.ImmutableCache.GetRoomVersion(request.RoomID); ok {
+ response.RoomVersion = roomVersion
+ return nil
+ }
+
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
if err != nil {
return err
}
response.RoomVersion = roomVersion
+ r.ImmutableCache.StoreRoomVersion(request.RoomID, response.RoomVersion)
return nil
}
diff --git a/roomserver/roomserver.go b/roomserver/roomserver.go
index 2ffbf67d..fa4f2062 100644
--- a/roomserver/roomserver.go
+++ b/roomserver/roomserver.go
@@ -48,7 +48,10 @@ func SetupRoomServerComponent(
inputAPI.SetupHTTP(http.DefaultServeMux)
- queryAPI := query.RoomserverQueryAPI{DB: roomserverDB}
+ queryAPI := query.RoomserverQueryAPI{
+ DB: roomserverDB,
+ ImmutableCache: base.ImmutableCache,
+ }
queryAPI.SetupHTTP(http.DefaultServeMux)