-
Notifications
You must be signed in to change notification settings - Fork 224
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 #1004 from Permify/refactor/switch-to-gobreaker-ad…
…d-singleflight feat: gobreaker add singleflight
- Loading branch information
Showing
18 changed files
with
479 additions
and
773 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
47 changes: 0 additions & 47 deletions
47
internal/storage/decorators/bundleReaderWithCircuitBreaker.go
This file was deleted.
Oops, something went wrong.
71 changes: 0 additions & 71 deletions
71
internal/storage/decorators/bundleWriterWithCircuitBreaker.go
This file was deleted.
Oops, something went wrong.
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
32 changes: 32 additions & 0 deletions
32
internal/storage/decorators/circuitBreaker/bundleReader.go
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,32 @@ | ||
package circuitBreaker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sony/gobreaker" | ||
|
||
"github.com/Permify/permify/internal/storage" | ||
base "github.com/Permify/permify/pkg/pb/base/v1" | ||
) | ||
|
||
// BundleReader - Add circuit breaker behaviour to bundle reader | ||
type BundleReader struct { | ||
delegate storage.BundleReader | ||
cb *gobreaker.CircuitBreaker | ||
} | ||
|
||
// NewBundleReader - Add circuit breaker behaviour to new bundle reader | ||
func NewBundleReader(delegate storage.BundleReader, cb *gobreaker.CircuitBreaker) *BundleReader { | ||
return &BundleReader{delegate: delegate, cb: cb} | ||
} | ||
|
||
// Read - Reads bundles from the repository | ||
func (r *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle *base.DataBundle, err error) { | ||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
return r.delegate.Read(ctx, tenantID, name) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response.(*base.DataBundle), nil | ||
} |
151 changes: 151 additions & 0 deletions
151
internal/storage/decorators/circuitBreaker/dataReader.go
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,151 @@ | ||
package circuitBreaker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sony/gobreaker" | ||
|
||
"github.com/Permify/permify/internal/storage" | ||
"github.com/Permify/permify/pkg/database" | ||
base "github.com/Permify/permify/pkg/pb/base/v1" | ||
"github.com/Permify/permify/pkg/token" | ||
) | ||
|
||
// DataReader - Add circuit breaker behaviour to data reader | ||
type DataReader struct { | ||
delegate storage.DataReader | ||
cb *gobreaker.CircuitBreaker | ||
} | ||
|
||
// NewDataReader - Add circuit breaker behaviour to new data reader | ||
func NewDataReader(delegate storage.DataReader, cb *gobreaker.CircuitBreaker) *DataReader { | ||
return &DataReader{delegate: delegate, cb: cb} | ||
} | ||
|
||
// QueryRelationships - Reads relation tuples from the repository | ||
func (r *DataReader) QueryRelationships(ctx context.Context, tenantID string, filter *base.TupleFilter, token string) (*database.TupleIterator, error) { | ||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
return r.delegate.QueryRelationships(ctx, tenantID, filter, token) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response.(*database.TupleIterator), nil | ||
} | ||
|
||
// ReadRelationships - Reads relation tuples from the repository with different options. | ||
func (r *DataReader) ReadRelationships(ctx context.Context, tenantID string, filter *base.TupleFilter, token string, pagination database.Pagination) (collection *database.TupleCollection, ct database.EncodedContinuousToken, err error) { | ||
type circuitBreakerResponse struct { | ||
Collection *database.TupleCollection | ||
ContinuousToken database.EncodedContinuousToken | ||
} | ||
|
||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
var err error | ||
var resp circuitBreakerResponse | ||
resp.Collection, resp.ContinuousToken, err = r.delegate.ReadRelationships(ctx, tenantID, filter, token, pagination) | ||
return resp, err | ||
}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
resp := response.(circuitBreakerResponse) | ||
return resp.Collection, resp.ContinuousToken, nil | ||
} | ||
|
||
// QuerySingleAttribute - Reads a single attribute from the repository. | ||
func (r *DataReader) QuerySingleAttribute(ctx context.Context, tenantID string, filter *base.AttributeFilter, token string) (*base.Attribute, error) { | ||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
return r.delegate.QuerySingleAttribute(ctx, tenantID, filter, token) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response.(*base.Attribute), nil | ||
} | ||
|
||
// QueryAttributes - Reads multiple attributes from the repository. | ||
func (r *DataReader) QueryAttributes(ctx context.Context, tenantID string, filter *base.AttributeFilter, token string) (*database.AttributeIterator, error) { | ||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
return r.delegate.QueryAttributes(ctx, tenantID, filter, token) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response.(*database.AttributeIterator), nil | ||
} | ||
|
||
// ReadAttributes - Reads multiple attributes from the repository with different options. | ||
func (r *DataReader) ReadAttributes(ctx context.Context, tenantID string, filter *base.AttributeFilter, token string, pagination database.Pagination) (collection *database.AttributeCollection, ct database.EncodedContinuousToken, err error) { | ||
type circuitBreakerResponse struct { | ||
Collection *database.AttributeCollection | ||
ContinuousToken database.EncodedContinuousToken | ||
} | ||
|
||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
var err error | ||
var resp circuitBreakerResponse | ||
resp.Collection, resp.ContinuousToken, err = r.delegate.ReadAttributes(ctx, tenantID, filter, token, pagination) | ||
return resp, err | ||
}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
resp := response.(circuitBreakerResponse) | ||
return resp.Collection, resp.ContinuousToken, nil | ||
} | ||
|
||
// QueryUniqueEntities - Reads unique entities from the repository with different options. | ||
func (r *DataReader) QueryUniqueEntities(ctx context.Context, tenantID, name, token string, pagination database.Pagination) (ids []string, ct database.EncodedContinuousToken, err error) { | ||
type circuitBreakerResponse struct { | ||
IDs []string | ||
ContinuousToken database.EncodedContinuousToken | ||
} | ||
|
||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
var err error | ||
var resp circuitBreakerResponse | ||
resp.IDs, resp.ContinuousToken, err = r.delegate.QueryUniqueEntities(ctx, tenantID, name, token, pagination) | ||
return resp, err | ||
}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
resp := response.(circuitBreakerResponse) | ||
return resp.IDs, resp.ContinuousToken, nil | ||
} | ||
|
||
// QueryUniqueSubjectReferences - Reads unique subject references from the repository with different options. | ||
func (r *DataReader) QueryUniqueSubjectReferences(ctx context.Context, tenantID string, subjectReference *base.RelationReference, token string, pagination database.Pagination) (ids []string, ct database.EncodedContinuousToken, err error) { | ||
type circuitBreakerResponse struct { | ||
IDs []string | ||
ContinuousToken database.EncodedContinuousToken | ||
} | ||
|
||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
var err error | ||
var resp circuitBreakerResponse | ||
resp.IDs, resp.ContinuousToken, err = r.delegate.QueryUniqueSubjectReferences(ctx, tenantID, subjectReference, token, pagination) | ||
return resp, err | ||
}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
resp := response.(circuitBreakerResponse) | ||
return resp.IDs, resp.ContinuousToken, nil | ||
} | ||
|
||
// HeadSnapshot - Reads the latest version of the snapshot from the repository. | ||
func (r *DataReader) HeadSnapshot(ctx context.Context, tenantID string) (token.SnapToken, error) { | ||
response, err := r.cb.Execute(func() (interface{}, error) { | ||
return r.delegate.HeadSnapshot(ctx, tenantID) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response.(token.SnapToken), nil | ||
} |
Oops, something went wrong.