Skip to content

Commit

Permalink
Merge pull request #215 from josephschorr/cleanup-dispatcher-cache
Browse files Browse the repository at this point in the history
Cleanup the CachingDispatcher when binary shuts down
  • Loading branch information
josephschorr committed Oct 26, 2021
2 parents eff4d2f + 1458362 commit bc40650
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/spicedb/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ func serveRun(cmd *cobra.Command, args []string) {
log.Fatal().Err(err).Msg("failed while shutting down namespace manager")
}

if err := cachingRedispatch.Close(); err != nil {
log.Fatal().Err(err).Msg("failed while shutting down dispatcher")
}

if err := ds.Close(); err != nil {
log.Fatal().Err(err).Msg("failed while shutting down datastore")
}
Expand Down
9 changes: 9 additions & 0 deletions internal/dispatch/caching/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ func (cd *cachingDispatcher) DispatchLookup(ctx context.Context, req *v1.Dispatc
return cd.d.DispatchLookup(ctx, req)
}

func (cd *cachingDispatcher) Close() error {
cache := cd.c
if cache != nil {
cache.Close()
}

return nil
}

func requestToKey(req *v1.DispatchCheckRequest) string {
return fmt.Sprintf("%s@%s@%s", tuple.StringONR(req.ObjectAndRelation), tuple.StringONR(req.Subject), req.Metadata.AtRevision)
}
5 changes: 5 additions & 0 deletions internal/dispatch/caching/cachingdispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestMaxDepthCaching(t *testing.T) {

dispatch, err := NewCachingDispatcher(delegate, nil, "")
require.NoError(err)
defer dispatch.Close()

for _, step := range tc.script {
resp, err := dispatch.DispatchCheck(context.Background(), &v1.DispatchCheckRequest{
Expand Down Expand Up @@ -140,3 +141,7 @@ func (ddm delegateDispatchMock) DispatchExpand(ctx context.Context, req *v1.Disp
func (ddm delegateDispatchMock) DispatchLookup(ctx context.Context, req *v1.DispatchLookupRequest) (*v1.DispatchLookupResponse, error) {
return &v1.DispatchLookupResponse{}, nil
}

func (ddm delegateDispatchMock) Close() error {
return nil
}
3 changes: 3 additions & 0 deletions internal/dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Dispatcher interface {
Check
Expand
Lookup

// Close closes the dispatcher.
Close() error
}

// Check interface describes just the methods required to dispatch check requests.
Expand Down
4 changes: 4 additions & 0 deletions internal/dispatch/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (ld *localDispatcher) DispatchLookup(ctx context.Context, req *v1.DispatchL
return ld.lookupHandler.Lookup(ctx, req)
}

func (ld *localDispatcher) Close() error {
return nil
}

func rewriteError(original error) error {
nsNotFound := datastore.ErrNamespaceNotFound{}

Expand Down
4 changes: 4 additions & 0 deletions internal/dispatch/remote/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (cr *clusterDispatcher) DispatchLookup(ctx context.Context, req *v1.Dispatc
return resp, nil
}

func (cr *clusterDispatcher) Close() error {
return nil
}

// Always verify that we implement the interface
var _ dispatch.Dispatcher = &clusterDispatcher{}

Expand Down

0 comments on commit bc40650

Please sign in to comment.