Skip to content

Commit

Permalink
Sync Set Cache (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon authored Nov 27, 2023
1 parent fdaab2d commit a537bfd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
35 changes: 14 additions & 21 deletions quotacontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,11 @@ func (q qcHandler) GetAccessQuota(ctx context.Context, accessKey string, now tim
Cycle: cycle,
AccessKey: access,
}
go func() {
// NOTE: we pass a fresh context here, as otherwise requests/other contexts which cancel
// above will cancel this goroutine and the AccessQuota will never be saved into cache.
err := q.quotaCache.SetAccessQuota(context.Background(), &record)
if err != nil {
q.log.With("error", err).Error("quotacontrol: failed to set access quota in cache")
}
}()

if err := q.quotaCache.SetAccessQuota(ctx, &record); err != nil {
q.log.With("error", err).Error("quotacontrol: failed to set access quota in cache")
}

return &record, nil
}

Expand Down Expand Up @@ -401,23 +398,19 @@ func (q qcHandler) DisableAccessKey(ctx context.Context, accessKey string) (bool
}

func (q qcHandler) GetUserPermission(ctx context.Context, projectID uint64, userID string) (*proto.UserPermission, map[string]interface{}, error) {
userPerm, resourceAccess, err := q.permStore.GetUserPermission(ctx, projectID, userID)
perm, access, err := q.permStore.GetUserPermission(ctx, projectID, userID)
if err != nil {
return userPerm, resourceAccess, proto.ErrUnauthorizedUser
return perm, access, proto.ErrUnauthorizedUser
}
if userPerm != nil && *userPerm == proto.UserPermission_UNAUTHORIZED {
return userPerm, resourceAccess, proto.ErrUnauthorizedUser
if perm != nil && *perm == proto.UserPermission_UNAUTHORIZED {
return perm, access, proto.ErrUnauthorizedUser
}

if err := q.permCache.SetUserPermission(ctx, projectID, userID, perm, access); err != nil {
q.log.With("error", err).Error("quotacontrol: failed to set user perm in cache")
}
go func() {
// NOTE: we pass a fresh context here, as otherwise requests/other contexts which cancel
// above will cancel this goroutine and the AccessQuota will never be saved into cache.
err := q.permCache.SetUserPermission(context.Background(), projectID, userID, userPerm, resourceAccess)
if err != nil {
q.log.With("error", err).Error("quotacontrol: failed to set user perm in cache")
}
}()

return userPerm, resourceAccess, nil
return perm, access, nil
}

func (q qcHandler) updateAccessKey(ctx context.Context, access *proto.AccessKey) (*proto.AccessKey, error) {
Expand Down
5 changes: 0 additions & 5 deletions quotacontrol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func TestMiddlewareUseAccessKey(t *testing.T) {
}
go func() { require.ErrorIs(t, server.ListenAndServe(), http.ErrServerClosed) }()
defer server.Close()
time.Sleep(1 * time.Second)

router := chi.NewRouter()

Expand All @@ -124,7 +123,6 @@ func TestMiddlewareUseAccessKey(t *testing.T) {

t.Run("WithAccessKey", func(t *testing.T) {
go client.Run(context.Background())
time.Sleep(1 * time.Second)

ctx := middleware.WithTime(context.Background(), _Now)
qc.notifications = make(map[uint64][]proto.EventType)
Expand Down Expand Up @@ -204,7 +202,6 @@ func TestMiddlewareUseAccessKey(t *testing.T) {
assert.NoError(t, err)

go client.Run(context.Background())
time.Sleep(1 * time.Second)

ctx := middleware.WithTime(context.Background(), _Now)
qc.notifications = make(map[uint64][]proto.EventType)
Expand All @@ -223,7 +220,6 @@ func TestMiddlewareUseAccessKey(t *testing.T) {

t.Run("PublicRateLimit", func(t *testing.T) {
go client.Run(context.Background())
time.Sleep(1 * time.Second)

ctx := middleware.WithTime(context.Background(), _Now)

Expand Down Expand Up @@ -336,7 +332,6 @@ func TestDefaultKey(t *testing.T) {
}
go func() { require.ErrorIs(t, server.ListenAndServe(), http.ErrServerClosed) }()
defer server.Close()
time.Sleep(1 * time.Second)

client := NewClient(wlog.With("client", "client"), *service, cfg)

Expand Down

0 comments on commit a537bfd

Please sign in to comment.