-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1607 from authzed/add-singleflight-check-dispatch
add singleflight check dispatch
- Loading branch information
Showing
7 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package singleflight | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"strconv" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"resenje.org/singleflight" | ||
|
||
"github.com/authzed/spicedb/internal/dispatch" | ||
"github.com/authzed/spicedb/internal/dispatch/keys" | ||
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" | ||
) | ||
|
||
var singleFlightCount = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: "spicedb", | ||
Subsystem: "dispatch", | ||
Name: "single_flight_total", | ||
Help: "total number of dispatch requests that were single flighted", | ||
}, []string{"method", "shared"}) | ||
|
||
func New(delegate dispatch.Dispatcher, handler keys.Handler) dispatch.Dispatcher { | ||
return &Dispatcher{delegate: delegate, keyHandler: handler} | ||
} | ||
|
||
type Dispatcher struct { | ||
delegate dispatch.Dispatcher | ||
keyHandler keys.Handler | ||
checkGroup singleflight.Group[string, *v1.DispatchCheckResponse] | ||
} | ||
|
||
func (d *Dispatcher) DispatchCheck(ctx context.Context, req *v1.DispatchCheckRequest) (*v1.DispatchCheckResponse, error) { | ||
key, err := d.keyHandler.CheckDispatchKey(ctx, req) | ||
if err != nil { | ||
return &v1.DispatchCheckResponse{Metadata: &v1.ResponseMeta{ | ||
DispatchCount: 1, | ||
}}, status.Error(codes.Internal, "unexpected DispatchCheck error") | ||
} | ||
|
||
keyString := hex.EncodeToString(key) | ||
v, isShared, err := d.checkGroup.Do(ctx, keyString, func(innerCtx context.Context) (*v1.DispatchCheckResponse, error) { | ||
return d.delegate.DispatchCheck(innerCtx, req) | ||
}) | ||
|
||
singleFlightCount.WithLabelValues("DispatchCheck", strconv.FormatBool(isShared)).Inc() | ||
if err != nil { | ||
return &v1.DispatchCheckResponse{Metadata: &v1.ResponseMeta{ | ||
DispatchCount: 1, | ||
}}, err | ||
} | ||
|
||
return v, err | ||
} | ||
|
||
func (d *Dispatcher) DispatchExpand(ctx context.Context, req *v1.DispatchExpandRequest) (*v1.DispatchExpandResponse, error) { | ||
return d.delegate.DispatchExpand(ctx, req) | ||
} | ||
|
||
func (d *Dispatcher) DispatchReachableResources(req *v1.DispatchReachableResourcesRequest, stream dispatch.ReachableResourcesStream) error { | ||
return d.delegate.DispatchReachableResources(req, stream) | ||
} | ||
|
||
func (d *Dispatcher) DispatchLookupResources(req *v1.DispatchLookupResourcesRequest, stream dispatch.LookupResourcesStream) error { | ||
return d.delegate.DispatchLookupResources(req, stream) | ||
} | ||
|
||
func (d *Dispatcher) DispatchLookupSubjects(req *v1.DispatchLookupSubjectsRequest, stream dispatch.LookupSubjectsStream) error { | ||
return d.delegate.DispatchLookupSubjects(req, stream) | ||
} | ||
|
||
func (d *Dispatcher) Close() error { | ||
return d.delegate.Close() | ||
} | ||
|
||
func (d *Dispatcher) ReadyState() dispatch.ReadyState { | ||
return d.delegate.ReadyState() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package singleflight | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/authzed/spicedb/internal/dispatch" | ||
"github.com/authzed/spicedb/internal/dispatch/keys" | ||
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" | ||
"github.com/authzed/spicedb/pkg/tuple" | ||
) | ||
|
||
func TestSingleFlightDispatcher(t *testing.T) { | ||
var called atomic.Uint64 | ||
f := func() { | ||
time.Sleep(100 * time.Millisecond) | ||
called.Add(1) | ||
} | ||
disp := New(mockDispatcher{f: f}, &keys.DirectKeyHandler{}) | ||
|
||
req := &v1.DispatchCheckRequest{ | ||
ResourceRelation: tuple.RelationReference("document", "view"), | ||
ResourceIds: []string{"foo", "bar"}, | ||
Subject: tuple.ObjectAndRelation("user", "tom", "..."), | ||
Metadata: &v1.ResolverMeta{ | ||
AtRevision: "1234", | ||
}, | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(4) | ||
go func() { | ||
_, _ = disp.DispatchCheck(context.Background(), req) | ||
wg.Done() | ||
}() | ||
go func() { | ||
_, _ = disp.DispatchCheck(context.Background(), req) | ||
wg.Done() | ||
}() | ||
go func() { | ||
_, _ = disp.DispatchCheck(context.Background(), req) | ||
|
||
wg.Done() | ||
}() | ||
go func() { | ||
_, _ = disp.DispatchCheck(context.Background(), &v1.DispatchCheckRequest{ | ||
ResourceRelation: tuple.RelationReference("document", "view"), | ||
ResourceIds: []string{"foo", "baz"}, | ||
Subject: tuple.ObjectAndRelation("user", "tom", "..."), | ||
Metadata: &v1.ResolverMeta{ | ||
AtRevision: "1234", | ||
}, | ||
}) | ||
wg.Done() | ||
}() | ||
|
||
wg.Wait() | ||
|
||
require.Equal(t, uint64(2), called.Load()) | ||
} | ||
|
||
func TestSingleFlightDispatcherCancelation(t *testing.T) { | ||
var called atomic.Uint64 | ||
run := make(chan struct{}, 1) | ||
f := func() { | ||
time.Sleep(100 * time.Millisecond) | ||
called.Add(1) | ||
run <- struct{}{} | ||
} | ||
disp := New(mockDispatcher{f: f}, &keys.DirectKeyHandler{}) | ||
|
||
req := &v1.DispatchCheckRequest{ | ||
ResourceRelation: tuple.RelationReference("document", "view"), | ||
ResourceIds: []string{"foo", "bar"}, | ||
Subject: tuple.ObjectAndRelation("user", "tom", "..."), | ||
Metadata: &v1.ResolverMeta{ | ||
AtRevision: "1234", | ||
}, | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(3) | ||
go func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50) | ||
defer cancel() | ||
_, err := disp.DispatchCheck(ctx, req) | ||
wg.Done() | ||
require.ErrorIs(t, err, context.DeadlineExceeded) | ||
}() | ||
go func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50) | ||
defer cancel() | ||
_, err := disp.DispatchCheck(ctx, req) | ||
wg.Done() | ||
require.ErrorIs(t, err, context.DeadlineExceeded) | ||
}() | ||
go func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50) | ||
defer cancel() | ||
_, err := disp.DispatchCheck(ctx, req) | ||
wg.Done() | ||
require.ErrorIs(t, err, context.DeadlineExceeded) | ||
}() | ||
|
||
wg.Wait() | ||
<-run | ||
require.Equal(t, uint64(1), called.Load()) | ||
} | ||
|
||
type mockDispatcher struct { | ||
f func() | ||
} | ||
|
||
func (m mockDispatcher) DispatchCheck(_ context.Context, _ *v1.DispatchCheckRequest) (*v1.DispatchCheckResponse, error) { | ||
m.f() | ||
return &v1.DispatchCheckResponse{}, nil | ||
} | ||
|
||
func (m mockDispatcher) DispatchExpand(_ context.Context, _ *v1.DispatchExpandRequest) (*v1.DispatchExpandResponse, error) { | ||
return &v1.DispatchExpandResponse{}, nil | ||
} | ||
|
||
func (m mockDispatcher) DispatchReachableResources(_ *v1.DispatchReachableResourcesRequest, _ dispatch.ReachableResourcesStream) error { | ||
return nil | ||
} | ||
|
||
func (m mockDispatcher) DispatchLookupResources(_ *v1.DispatchLookupResourcesRequest, _ dispatch.LookupResourcesStream) error { | ||
return nil | ||
} | ||
|
||
func (m mockDispatcher) DispatchLookupSubjects(_ *v1.DispatchLookupSubjectsRequest, _ dispatch.LookupSubjectsStream) error { | ||
return nil | ||
} | ||
|
||
func (m mockDispatcher) Close() error { | ||
return nil | ||
} | ||
|
||
func (m mockDispatcher) ReadyState() dispatch.ReadyState { | ||
return dispatch.ReadyState{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters