diff --git a/premium/byguild.go b/premium/byguild.go index e018dc6..8e311ad 100644 --- a/premium/byguild.go +++ b/premium/byguild.go @@ -16,10 +16,10 @@ func (p *PremiumLookupClient) GetTierByGuild(ctx context.Context, guild guild.Gu _src = "" defer func() { - // cache result - if _err == nil { + // a query cut short by ctx returns no rows and no error + if _err == nil && ctx.Err() == nil { go func() { - err := p.SetCachedTier(ctx, guild.Id, CachedTier{ + err := p.cacheTier(guild.Id, CachedTier{ Tier: int8(_tier), Source: _src, }) diff --git a/premium/byuser.go b/premium/byuser.go index aaeeee8..6024894 100644 --- a/premium/byuser.go +++ b/premium/byuser.go @@ -33,9 +33,9 @@ func (p *PremiumLookupClient) GetTierByUserWithSource(ctx context.Context, userI } defer func() { - // cache result - if _err == nil { - go p.SetCachedTier(ctx, userId, CachedTier{ + // a query cut short by ctx returns no rows and no error + if _err == nil && ctx.Err() == nil { + go p.cacheTier(userId, CachedTier{ Tier: int8(_tier), Source: _src, }) diff --git a/premium/cache.go b/premium/cache.go index d3c380f..c44e51b 100644 --- a/premium/cache.go +++ b/premium/cache.go @@ -16,6 +16,7 @@ type CachedTier struct { } const timeout = time.Minute * 5 +const cacheWriteTimeout = 3 * time.Second // Functions can take a user ID or guild ID @@ -50,6 +51,13 @@ func (p *PremiumLookupClient) SetCachedTier(ctx context.Context, id uint64, data }) } +func (p *PremiumLookupClient) cacheTier(id uint64, data CachedTier) error { + ctx, cancel := context.WithTimeout(context.Background(), cacheWriteTimeout) + defer cancel() + + return p.SetCachedTier(ctx, id, data) +} + func (p *PremiumLookupClient) DeleteCachedTier(ctx context.Context, id uint64) error { return sentry.WithSpan1(ctx, "DeleteCachedTier", func(span *sentry.Span) error { key := fmt.Sprintf("premium:%d", id)