Skip to content

Commit

Permalink
datastore/crdb: singleflight features
Browse files Browse the repository at this point in the history
Only singleflight features internal to CRDB so that
it can be deduplicated in calls to Watch.

No other datastore has an expensive Features impl.
  • Loading branch information
jzelinskie committed Oct 26, 2023
1 parent b923051 commit 6497bb2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 10 additions & 0 deletions internal/datastore/crdb/crdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/shopspring/decimal"
"go.opentelemetry.io/otel"
"golang.org/x/sync/errgroup"
"resenje.org/singleflight"

datastoreinternal "github.com/authzed/spicedb/internal/datastore"
"github.com/authzed/spicedb/internal/datastore/common"
Expand Down Expand Up @@ -257,6 +258,8 @@ type crdbDatastore struct {

beginChangefeedQuery string

featureGroup singleflight.Group[string, *datastore.Features]

pruneGroup *errgroup.Group
ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -419,6 +422,13 @@ func (cds *crdbDatastore) headRevisionInternal(ctx context.Context) (revision.De
}

func (cds *crdbDatastore) Features(ctx context.Context) (*datastore.Features, error) {
features, _, err := cds.featureGroup.Do(ctx, "", func(ictx context.Context) (*datastore.Features, error) {
return cds.features(ictx)
})
return features, err
}

func (cds *crdbDatastore) features(ctx context.Context) (*datastore.Features, error) {
var features datastore.Features

head, err := cds.HeadRevision(ctx)
Expand Down
12 changes: 4 additions & 8 deletions internal/datastore/proxy/singleflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type singleflightProxy struct {
optRevGroup singleflight.Group[string, datastore.Revision]
checkRevGroup singleflight.Group[string, string]
statsGroup singleflight.Group[string, datastore.Stats]
featureGroup singleflight.Group[string, *datastore.Features]
delegate datastore.Datastore
}

Expand Down Expand Up @@ -63,20 +62,17 @@ func (p *singleflightProxy) Watch(ctx context.Context, afterRevision datastore.R
return p.delegate.Watch(ctx, afterRevision)
}

func (p *singleflightProxy) Features(ctx context.Context) (*datastore.Features, error) {
features, _, err := p.featureGroup.Do(ctx, "", func(ctx context.Context) (*datastore.Features, error) {
return p.delegate.Features(ctx)
})
return features, err
}

func (p *singleflightProxy) Statistics(ctx context.Context) (datastore.Stats, error) {
stats, _, err := p.statsGroup.Do(ctx, "", func(ctx context.Context) (datastore.Stats, error) {
return p.delegate.Statistics(ctx)
})
return stats, err
}

func (p *singleflightProxy) Features(ctx context.Context) (*datastore.Features, error) {
return p.delegate.Features(ctx)
}

func (p *singleflightProxy) ReadyState(ctx context.Context) (datastore.ReadyState, error) {
return p.delegate.ReadyState(ctx)
}
Expand Down

0 comments on commit 6497bb2

Please sign in to comment.