Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from stampede to self-rolled-mutex-locked-channels-solution #394

Merged
merged 6 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/resolvers/default/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ var defaultTooltip = template.Must(template.New("default_tooltip").Parse(default
func Initialize(ctx context.Context, cfg config.APIConfig, pool db.Pool, router *chi.Mux, helixClient *helix.Client) {
defaultLinkResolver := New(ctx, cfg, pool, helixClient)

cached := stampede.Handler(512, 10*time.Second)
// cached := stampede.Handler(512, 10*time.Second)
imageCached := stampede.Handler(256, 2*time.Second)
generatedValuesCached := stampede.Handler(256, 2*time.Second)

router.With(cached).Get("/link_resolver/{url}", defaultLinkResolver.HandleRequest)
router.Get("/link_resolver/{url}", defaultLinkResolver.HandleRequest)
router.With(imageCached).Get("/thumbnail/{url}", defaultLinkResolver.HandleThumbnailRequest)
router.With(generatedValuesCached).Get("/generated/{url}", defaultLinkResolver.HandleGeneratedValueRequest)
}
42 changes: 41 additions & 1 deletion pkg/cache/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"net/http"
"sync"
"time"

"github.com/Chatterino/api/internal/db"
Expand Down Expand Up @@ -49,6 +50,9 @@ type PostgreSQLCache struct {
pool db.Pool

dependentCaches []DependentCache

requestsMutex sync.Mutex
requests map[string][]chan *Response
}

// TODO: Make the "internal error" tooltip an actual tooltip
Expand Down Expand Up @@ -189,7 +193,42 @@ func (c *PostgreSQLCache) Get(ctx context.Context, key string, r *http.Request)

cacheMisses.Inc()
log.Debugw("DB Get cache miss", "cacheKey", cacheKey)
return c.load(ctx, key, r)
responseChannel := make(chan *Response)

c.requestsMutex.Lock()

c.requests[key] = append(c.requests[key], responseChannel)

first := len(c.requests[key]) == 1

c.requestsMutex.Unlock()

if first {
log.Debugw("DB Get cache miss!!", "cacheKey", cacheKey)
go func() {
response, err := c.load(ctx, key, r)
if err != nil {
log.Warnw("Error loading DB request", "error", err)
response = &Response{
Payload: []byte(`{"status":500,"message":"Internal server error (PSQL) loading cache"}`),
StatusCode: 500,
ContentType: "application/json",
}
}
c.requestsMutex.Lock()
for _, ch := range c.requests[key] {
ch <- response
}
delete(c.requests, key)
c.requestsMutex.Unlock()
}()
}

// If key is not in cache, sign up as a listener and ensure loader is only called once
// Wait for loader to complete, then return value from loader
log.Debugw("DB Waiting for response channel", "cacheKey", cacheKey)
response := <-responseChannel
return response, nil
}

func (c *PostgreSQLCache) GetOnly(ctx context.Context, key string) *Response {
Expand Down Expand Up @@ -280,6 +319,7 @@ func NewPostgreSQLCache(ctx context.Context, cfg config.APIConfig, pool db.Pool,
loader: loader,
cacheDuration: cacheDuration,
pool: pool,
requests: make(map[string][]chan *Response),
}
}

Expand Down