From 378d03eb66cf9bae611eeb615c072378a613cfcd Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Wed, 25 Mar 2020 18:34:13 +0000 Subject: [PATCH] Tidying up auth (#1410) * Don't clear auth rules if request fails * Add jitter to auth service loading rules * Remove unused error from ContextWithToken result Co-authored-by: Ben Toogood --- auth/auth.go | 4 ++-- auth/service/service.go | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 268f29ba..837ace66 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -130,6 +130,6 @@ func ContextWithAccount(ctx context.Context, account *Account) (context.Context, } // ContextWithToken sets the auth token in the context -func ContextWithToken(ctx context.Context, token string) (context.Context, error) { - return metadata.Set(ctx, "Authorization", fmt.Sprintf("%v%v", BearerScheme, token)), nil +func ContextWithToken(ctx context.Context, token string) context.Context { + return metadata.Set(ctx, "Authorization", fmt.Sprintf("%v%v", BearerScheme, token)) } diff --git a/auth/service/service.go b/auth/service/service.go index e544e3f1..190551f9 100644 --- a/auth/service/service.go +++ b/auth/service/service.go @@ -13,6 +13,7 @@ import ( "github.com/micro/go-micro/v2/auth/token/jwt" "github.com/micro/go-micro/v2/client" log "github.com/micro/go-micro/v2/logger" + "github.com/micro/go-micro/v2/util/jitter" ) // NewAuth returns a new instance of the Auth service @@ -54,9 +55,17 @@ func (s *svc) Init(opts ...auth.Option) { // load rules periodically from the auth service timer := time.NewTicker(time.Second * 30) go func() { + // load rules immediately on startup + s.loadRules() + for { - s.loadRules() <-timer.C + + // jitter for up to 5 seconds, this stops + // all the services calling the auth service + // at the exact same time + time.Sleep(jitter.Do(time.Second * 5)) + s.loadRules() } }() } @@ -227,7 +236,6 @@ func (s *svc) loadRules() { if err != nil { log.Errorf("Error listing rules: %v", err) - s.rules = []*pb.Rule{} return }