Skip to content

Commit

Permalink
Fix cache attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Nov 14, 2024
1 parent f9048a8 commit 11f4f61
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func defaultMiddlewares(oauthAuther auth.Authenticator, legacyProvider auth.Prov
panic(err)
}
cache := query.NewQueryCache(store)
logger.Log().Infof("cache configured: master=%s", config.GetSturdyCacheMaster())
logger.Log().Infof("cache configured: master=%s, replicas=%s", config.GetSturdyCacheMaster(), config.GetSturdyCacheReplicas())

defaultHeaders := []string{
wallet.LegacyTokenHeader, wallet.AuthorizationHeader, "X-Requested-With", "Content-Type", "Accept",
Expand Down
7 changes: 3 additions & 4 deletions app/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

"github.com/OdyseeTeam/odysee-api/app/auth"
"github.com/OdyseeTeam/odysee-api/app/query"
"github.com/OdyseeTeam/odysee-api/app/query/cache"
"github.com/OdyseeTeam/odysee-api/app/sdkrouter"
"github.com/OdyseeTeam/odysee-api/internal/audit"
"github.com/OdyseeTeam/odysee-api/internal/errors"
Expand Down Expand Up @@ -72,7 +71,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
return
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
writeResponse(w, rpcerrors.NewJSONParseError(errors.Err("error reading request body")).JSON())
Expand Down Expand Up @@ -130,7 +129,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
return nil, nil
}, "")

if cache.HasCache(r) {
if query.HasCache(r) {
c.Cache = query.CacheFromRequest(r)
}

Expand Down
3 changes: 1 addition & 2 deletions app/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/OdyseeTeam/odysee-api/app/auth"
"github.com/OdyseeTeam/odysee-api/app/proxy"
"github.com/OdyseeTeam/odysee-api/app/query"
"github.com/OdyseeTeam/odysee-api/app/query/cache"
"github.com/OdyseeTeam/odysee-api/app/sdkrouter"
"github.com/OdyseeTeam/odysee-api/internal/errors"
"github.com/OdyseeTeam/odysee-api/internal/metrics"
Expand Down Expand Up @@ -161,7 +160,7 @@ retry:
}()

var qCache *query.QueryCache
if cache.HasCache(r) {
if query.HasCache(r) {
qCache = query.CacheFromRequest(r)
}

Expand Down
3 changes: 1 addition & 2 deletions app/publish/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/OdyseeTeam/odysee-api/app/auth"
"github.com/OdyseeTeam/odysee-api/app/proxy"
"github.com/OdyseeTeam/odysee-api/app/query"
"github.com/OdyseeTeam/odysee-api/app/query/cache"
"github.com/OdyseeTeam/odysee-api/app/sdkrouter"
"github.com/OdyseeTeam/odysee-api/app/wallet"
"github.com/OdyseeTeam/odysee-api/internal/errors"
Expand Down Expand Up @@ -265,7 +264,7 @@ func (h TusHandler) Notify(w http.ResponseWriter, r *http.Request) {

// upload is completed, notify lbrynet server
var qCache *query.QueryCache
if cache.HasCache(r) {
if query.HasCache(r) {
qCache = query.CacheFromRequest(r)
}

Expand Down
16 changes: 11 additions & 5 deletions app/query/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewQueryCache(store cache.CacheInterface[any]) *QueryCache {
}

func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*CachedResponse, error) {
log := logger.Log()
cacheReq := CacheRequest{
Method: query.Method(),
Params: query.Params(),
Expand All @@ -59,10 +60,12 @@ func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*Cached
}
metrics.SturdyQueryCacheHitCount.WithLabelValues(cacheReq.Method).Inc()
if getter == nil {
log.Warnf("nil getter provided for %s", query.Method())
metrics.SturdyQueryCacheErrorCount.WithLabelValues(cacheReq.Method).Inc()
return nil, errors.New("cache miss with no object getter provided")
}

log.Infof("cold cache retrieval for %s", query.Method())
obj, err, _ := c.singleflight.Do(cacheReq.GetCacheKey(), getter)
if err != nil {
metrics.SturdyQueryCacheErrorCount.WithLabelValues(cacheReq.Method).Inc()
Expand Down Expand Up @@ -144,17 +147,20 @@ func (r *CachedResponse) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, r)
}

func preflightCacheHook(c *Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) {
if c.Cache == nil {
func preflightCacheHook(caller *Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) {
log := logger.Log()
if caller.Cache == nil {
log.Warn("no cache present on caller")
return nil, nil
}
query := QueryFromContext(ctx)
cachedResp, err := c.Cache.Retrieve(query, func() (any, error) {
return c.SendQuery(ctx, query)
cachedResp, err := caller.Cache.Retrieve(query, func() (any, error) {
log.Debugf("cache miss, calling %s", query.Method())
return caller.SendQuery(ctx, query)
})
if err != nil {
return nil, rpcerrors.NewSDKError(err)
}
logger.Log().Debugf("FFS")
log.Debugf("cache hit for %s", query.Method())
return cachedResp.RPCResponse(query.Request.ID), nil
}
1 change: 1 addition & 0 deletions app/query/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (c *Caller) call(ctx context.Context, req *jsonrpc.RPCRequest) (*jsonrpc.RP
return nil, rpcerrors.NewSDKError(err)
}
if res != nil {
logger.Log().Infof("got %s response from %s hook", q.Method(), hook.name)
return res, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/lbrytv/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func GetSturdyCacheReplicas() []string {
}

func GetSturdyCachePassword() string {
return Config.Viper.GetString("sturdycache.replicas")
return Config.Viper.GetString("sturdycache.password")
}

// GetDatabase returns postgresql database server connection config.
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Make sure you have recent enough Docker and `docker compose` installed.

This will pull and launch SDK and postgres images, which Odysee API requires to operate.

`docker compose -f docker-compose.yml -f docker compose.app.yml up -d`
`docker compose -f docker-compose.yml -f docker-compose.app.yml up -d`

*Note: if you're running a LBRY desktop app or lbrynet instance, you will have to either shut it down or change ports*

Expand Down

0 comments on commit 11f4f61

Please sign in to comment.