Merge pull request #1984 from Ace-Tang/fix-monitor-hang

monitor: enlarge watch buffer
This commit is contained in:
Fupan Li 2019-08-22 14:20:06 +08:00 committed by GitHub
commit 9a6e299827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,10 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const defaultCheckInterval = 1 * time.Second const (
defaultCheckInterval = 1 * time.Second
watcherChannelSize = 128
)
type monitor struct { type monitor struct {
sync.Mutex sync.Mutex
@ -37,7 +40,7 @@ func (m *monitor) newWatcher() (chan error, error) {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
watcher := make(chan error, 1) watcher := make(chan error, watcherChannelSize)
m.watchers = append(m.watchers, watcher) m.watchers = append(m.watchers, watcher)
if !m.running { if !m.running {
@ -83,7 +86,14 @@ func (m *monitor) notify(err error) {
}() }()
for _, c := range m.watchers { for _, c := range m.watchers {
c <- err // throw away message can not write to channel
// make it not stuck, the first error is useful.
select {
case c <- err:
default:
virtLog.WithField("channel-size", watcherChannelSize).Warnf("watcher channel is full, throw notify message")
}
} }
} }
@ -98,8 +108,8 @@ func (m *monitor) stop() {
return return
} }
m.stopCh <- true
defer func() { defer func() {
m.stopCh <- true
m.watchers = nil m.watchers = nil
m.running = false m.running = false
}() }()