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 <ben@micro.mu>
This commit is contained in:
parent
56af826230
commit
378d03eb66
@ -130,6 +130,6 @@ func ContextWithAccount(ctx context.Context, account *Account) (context.Context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ContextWithToken sets the auth token in the context
|
// ContextWithToken sets the auth token in the context
|
||||||
func ContextWithToken(ctx context.Context, token string) (context.Context, error) {
|
func ContextWithToken(ctx context.Context, token string) context.Context {
|
||||||
return metadata.Set(ctx, "Authorization", fmt.Sprintf("%v%v", BearerScheme, token)), nil
|
return metadata.Set(ctx, "Authorization", fmt.Sprintf("%v%v", BearerScheme, token))
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/micro/go-micro/v2/auth/token/jwt"
|
"github.com/micro/go-micro/v2/auth/token/jwt"
|
||||||
"github.com/micro/go-micro/v2/client"
|
"github.com/micro/go-micro/v2/client"
|
||||||
log "github.com/micro/go-micro/v2/logger"
|
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
|
// 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
|
// load rules periodically from the auth service
|
||||||
timer := time.NewTicker(time.Second * 30)
|
timer := time.NewTicker(time.Second * 30)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
// load rules immediately on startup
|
||||||
s.loadRules()
|
s.loadRules()
|
||||||
|
|
||||||
|
for {
|
||||||
<-timer.C
|
<-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 {
|
if err != nil {
|
||||||
log.Errorf("Error listing rules: %v", err)
|
log.Errorf("Error listing rules: %v", err)
|
||||||
s.rules = []*pb.Rule{}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user