fix races in web and logger (#1576)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-04-26 17:41:36 +03:00 committed by GitHub
parent a22da39e1c
commit 980b772801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -136,7 +136,12 @@ func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) {
}
func (n *defaultLogger) Options() Options {
return n.opts
// not guard against options Context values
n.RLock()
opts := n.opts
opts.Fields = copyFields(n.opts.Fields)
n.RUnlock()
return opts
}
// NewLogger builds a new logger based on options

View File

@ -29,7 +29,7 @@ type service struct {
mux *http.ServeMux
srv *registry.Service
sync.Mutex
sync.RWMutex
running bool
static bool
exit chan chan error
@ -90,11 +90,14 @@ func (s *service) genSrv() *registry.Service {
}
func (s *service) run(exit chan bool) {
s.RLock()
if s.opts.RegisterInterval <= time.Duration(0) {
s.RUnlock()
return
}
t := time.NewTicker(s.opts.RegisterInterval)
s.RUnlock()
for {
select {
@ -327,6 +330,9 @@ func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *
}
func (s *service) Init(opts ...Option) error {
s.Lock()
defer s.Unlock()
for _, o := range opts {
o(&s.opts)
}
@ -342,6 +348,9 @@ func (s *service) Init(opts ...Option) error {
}
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
s.Lock()
defer s.Unlock()
if ttl := ctx.Int("register_ttl"); ttl > 0 {
s.opts.RegisterTTL = time.Duration(ttl) * time.Second
}