Skip to content

Commit

Permalink
Optimize common middleware initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Nov 28, 2024
1 parent 17a8be7 commit fe3313f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
9 changes: 6 additions & 3 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func InstallRoutes(r *mux.Router, sdkRouter *sdkrouter.Router, opts *RoutesOptio
legacyProvider := auth.NewIAPIProvider(sdkRouter, config.GetInternalAPIHost())
sentryHandler := sentryhttp.New(sentryhttp.Options{})

allMiddlewares := defaultMiddlewares(oauthAuther, legacyProvider, sdkRouter)

r.Use(methodTimer, sentryHandler.Handle)

r.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
Expand All @@ -77,7 +79,7 @@ func InstallRoutes(r *mux.Router, sdkRouter *sdkrouter.Router, opts *RoutesOptio
r.HandleFunc("", emptyHandler)

v1Router := r.PathPrefix("/api/v1").Subrouter()
v1Router.Use(defaultMiddlewares(oauthAuther, legacyProvider, sdkRouter))
v1Router.Use(allMiddlewares)

v1Router.HandleFunc("/proxy", upHandler.Handle).MatcherFunc(publish.CanHandle)
v1Router.HandleFunc("/proxy", proxy.Handle).Methods(http.MethodPost)
Expand Down Expand Up @@ -108,7 +110,7 @@ func InstallRoutes(r *mux.Router, sdkRouter *sdkrouter.Router, opts *RoutesOptio
}

v2Router := r.PathPrefix("/api/v2").Subrouter()
v2Router.Use(defaultMiddlewares(oauthAuther, legacyProvider, sdkRouter))
v2Router.Use(allMiddlewares)
status.InstallRoutes(v2Router)

composer := tusd.NewStoreComposer()
Expand Down Expand Up @@ -153,7 +155,7 @@ func InstallRoutes(r *mux.Router, sdkRouter *sdkrouter.Router, opts *RoutesOptio
gpl := zapadapter.NewNamedKV("geopublish", config.GetLoggingOpts())
if opts.EnableV3Publish {
v3Router := r.PathPrefix("/api/v3").Subrouter()
v3Router.Use(defaultMiddlewares(oauthAuther, legacyProvider, sdkRouter))
v3Router.Use(allMiddlewares)
ug := auth.NewUniversalUserGetter(oauthAuther, legacyProvider, gpl)
gPath := config.GetGeoPublishSourceDir()
v3Handler, err = geopublish.InstallRoutes(v3Router.PathPrefix("/publish").Subrouter(), ug, gPath, "/api/v3/publish/", gpl)
Expand Down Expand Up @@ -212,6 +214,7 @@ func defaultMiddlewares(oauthAuther auth.Authenticator, legacyProvider auth.Prov
if err != nil {
panic(err)
}

cache, err := query.NewQueryCacheWithInvalidator(lstore)
if err != nil {
panic(err)
Expand Down
13 changes: 7 additions & 6 deletions app/query/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ func NewQueryCacheWithInvalidator(baseCache cache.CacheInterface[any]) (*QueryCa
return nil, fmt.Errorf("failed to get current height: %w", err)
}
qc.height = height

go func() {
ticker := time.NewTicker(invalidationInterval)
for {
select {
case <-ticker.C:
err := qc.runInvalidator()
if err != nil {
logger.Log().Warn(err.Error())
}
qc.runInvalidator()
case <-qc.stopChan:
return
}
Expand Down Expand Up @@ -153,14 +149,18 @@ func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*Cached
}

func (c *QueryCache) runInvalidator() error {
log := logger.Log()
height, err := chainquery.GetHeight()
if err != nil {
QueryCacheErrorCount.WithLabelValues(CacheAreaChainquery).Inc()
return fmt.Errorf("failed to get current height: %w", err)
}
if c.height >= height {
log.Infof("block height unchanged (%v = %v), cache invalidation skipped", height, c.height)
return nil
}

log.Infof("new block height (%v > %v), running invalidation", height, c.height)
c.height = height

ctx, cancel := context.WithTimeout(context.Background(), invalidationInterval)
Expand All @@ -170,6 +170,7 @@ func (c *QueryCache) runInvalidator() error {
))
if err != nil {
QueryCacheErrorCount.WithLabelValues(CacheAreaInvalidateCall).Inc()
log.Warnf("failed to invalidate %s entries: %s", MethodClaimSearch, err)
return fmt.Errorf("failed to invalidate %s entries: %w", MethodClaimSearch, err)
}

Expand All @@ -181,7 +182,7 @@ func (r CacheRequest) Expiration() time.Duration {
case MethodResolve:
return 600 * time.Second
case MethodClaimSearch:
return 180 * time.Second
return 300 * time.Second
default:
return 60 * time.Second
}
Expand Down
2 changes: 1 addition & 1 deletion app/query/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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)
logger.Log().Debugf("got %s response from %s hook", q.Method(), hook.name)
return res, nil
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sturdycache/sturdycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ReplicatedCache struct {
masterCache *cache.Cache[any]
replicaCaches []*cache.Cache[any]
readCaches []*cache.Cache[any]
compression bool

Check failure on line 22 in pkg/sturdycache/sturdycache.go

View workflow job for this annotation

GitHub Actions / lint

field `compression` is unused (unused)
}

// NewReplicatedCache creates a new gocache store instance for redis master-replica setups.
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func NewServer(address string, sdkRouter *sdkrouter.Router, rOpts *api.RoutesOpt
api.InstallRoutes(r, sdkRouter, rOpts)
r.Use(monitor.ErrorLoggingMiddleware)
r.Use(defaultHeadersMiddleware(map[string]string{
"Server": "api.lbry.tv",
"Server": "api.odysee.com",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "content-type", // Needed this to get any request to work
"Access-Control-Allow-Headers": "content-type",
}))

return &Server{
Expand Down

0 comments on commit fe3313f

Please sign in to comment.