Skip to content

Commit

Permalink
Better error handling on enrich cache
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelreiswildlife committed Sep 28, 2023
1 parent a18fe55 commit 187e002
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enrichment:
webhook_timeout: 500ms
cache:
ttl: 24h
addr: "localhost:6739"
addr: "localhost:6379"
password: ""
cloud_save:
url: "localhost:8888/"
Expand Down
29 changes: 19 additions & 10 deletions leaderboard/enriching/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/topfreegames/podium/leaderboard/v2/enriching"
Expand Down Expand Up @@ -64,20 +65,28 @@ func (e *enricherRedisCache) Set(
ttl time.Duration,
) error {
keys := getKeysFromMemberArray(tenantID, members)
pipe := e.redis.TxPipeline()
for i, member := range members {
if member.Metadata != nil {
marshaled, err := json.Marshal(member.Metadata)
if err != nil {
return fmt.Errorf("failed to marshal metadata: %w", err)
cmds, err := e.redis.Pipelined(ctx, func(pipe redis.Pipeliner) error {
for i, member := range members {
if member.Metadata != nil {
marshaled, err := json.Marshal(member.Metadata)
if err != nil {
return fmt.Errorf("failed to marshal metadata: %w", err)
}
pipe.Set(ctx, keys[i], marshaled, ttl)
}
pipe.Set(ctx, keys[i], marshaled, ttl)
}
}
return nil
})

_, err := pipe.Exec(ctx)
if err != nil {
return fmt.Errorf("failed to set members in cacheConfig: %w", err)
cmdErrors := []error{}
for _, cmd := range cmds {
if cmd.Err() != nil {
cmdErrors = append(cmdErrors, cmd.Err())
}
}
cmdError := errors.Join(cmdErrors...)
return fmt.Errorf("failed to set members in cache: %w", errors.Join(err, cmdError))
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions leaderboard/enriching/cache/cache_instrumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *instrumentedCache) Get(
) (map[string]map[string]string, bool, error) {
start := time.Now()

span, ctx := opentracing.StartSpanFromContext(ctx, "podium.enriching_cache", opentracing.Tags{
span, ctx := opentracing.StartSpanFromContext(ctx, "podium.enriching_cache.get", opentracing.Tags{
"tenant_id": tenantID,
})
defer span.Finish()
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c *instrumentedCache) Set(
) error {
start := time.Now()

span, ctx := opentracing.StartSpanFromContext(ctx, "podium.enriching_cache", opentracing.Tags{
span, ctx := opentracing.StartSpanFromContext(ctx, "podium.enriching_cache.set", opentracing.Tags{
"tenant_id": tenantID,
"ttl": ttl,
})
Expand Down

0 comments on commit 187e002

Please sign in to comment.