Skip to content

Commit

Permalink
fix races in web and logger (micro#1576)
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
  • Loading branch information
vtolstov committed Apr 26, 2020
1 parent a22da39 commit 980b772
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion logger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion web/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down

0 comments on commit 980b772

Please sign in to comment.