authorize: fix device synchronization (#3482)

This commit is contained in:
Caleb Doxsey 2022-07-15 17:27:06 -06:00 committed by GitHub
parent bc078f8bd2
commit fe61a74e1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package storage
import (
"context"
"strconv"
"sync"
"github.com/google/uuid"
@ -19,12 +20,15 @@ import (
// A Querier is a read-only subset of the client methods
type Querier interface {
InvalidateCache(ctx context.Context, in *databroker.QueryRequest)
Query(ctx context.Context, in *databroker.QueryRequest, opts ...grpc.CallOption) (*databroker.QueryResponse, error)
}
// nilQuerier always returns NotFound.
type nilQuerier struct{}
func (nilQuerier) InvalidateCache(ctx context.Context, in *databroker.QueryRequest) {}
func (nilQuerier) Query(ctx context.Context, in *databroker.QueryRequest, opts ...grpc.CallOption) (*databroker.QueryResponse, error) {
return nil, status.Error(codes.NotFound, "not found")
}
@ -63,11 +67,18 @@ func NewStaticQuerier(msgs ...proto.Message) Querier {
if hasID, ok := msg.(interface{ GetId() string }); ok {
record.Id = hasID.GetId()
}
if hasVersion, ok := msg.(interface{ GetVersion() string }); ok {
if v, err := strconv.ParseUint(hasVersion.GetVersion(), 10, 64); err == nil {
record.Version = v
}
}
getter.records = append(getter.records, record)
}
return getter
}
func (q *staticQuerier) InvalidateCache(ctx context.Context, in *databroker.QueryRequest) {}
// Query queries for records.
func (q *staticQuerier) Query(ctx context.Context, in *databroker.QueryRequest, opts ...grpc.CallOption) (*databroker.QueryResponse, error) {
expr, err := FilterExpressionFromStruct(in.GetFilter())
@ -116,6 +127,8 @@ func NewQuerier(client databroker.DataBrokerServiceClient) Querier {
return &clientQuerier{client: client}
}
func (q *clientQuerier) InvalidateCache(ctx context.Context, in *databroker.QueryRequest) {}
// Query queries for records.
func (q *clientQuerier) Query(ctx context.Context, in *databroker.QueryRequest, opts ...grpc.CallOption) (*databroker.QueryResponse, error) {
return q.client.Query(ctx, in, opts...)
@ -145,6 +158,11 @@ func NewTracingQuerier(q Querier) *TracingQuerier {
}
}
// InvalidateCache invalidates the cache.
func (q *TracingQuerier) InvalidateCache(ctx context.Context, in *databroker.QueryRequest) {
q.underlying.InvalidateCache(ctx, in)
}
// Query queries for records.
func (q *TracingQuerier) Query(ctx context.Context, in *databroker.QueryRequest, opts ...grpc.CallOption) (*databroker.QueryResponse, error) {
res, err := q.underlying.Query(ctx, in, opts...)
@ -182,6 +200,17 @@ func NewCachingQuerier(q Querier, cache Cache) Querier {
}
}
func (q *cachingQuerier) InvalidateCache(ctx context.Context, in *databroker.QueryRequest) {
key, err := (&proto.MarshalOptions{
Deterministic: true,
}).Marshal(in)
if err != nil {
return
}
q.cache.Invalidate(key)
q.q.InvalidateCache(ctx, in)
}
func (q *cachingQuerier) Query(ctx context.Context, in *databroker.QueryRequest, opts ...grpc.CallOption) (*databroker.QueryResponse, error) {
key, err := (&proto.MarshalOptions{
Deterministic: true,