Skip to content

Commit

Permalink
add some locking
Browse files Browse the repository at this point in the history
  • Loading branch information
coder543 committed Jan 8, 2019
1 parent 8bcb960 commit b1390dc
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Client struct {
events chan *Event
closed bool
waiter sync.WaitGroup
lock sync.Mutex
lastFlush time.Time
lastWrite time.Time
}
Expand Down Expand Up @@ -99,8 +100,10 @@ func (c *Client) run() {
}

// send the event
c.lock.Lock()
io.Copy(c.write, ev)
c.lastWrite = time.Now()
c.lock.Unlock()

case <-c.close.CloseNotify():
c.closed = true
Expand All @@ -117,10 +120,12 @@ func (c *Client) flusher() {

for !c.closed {
<-ticker.C
c.lock.Lock()
if c.lastFlush.Before(c.lastWrite) {
c.lastFlush = c.lastWrite
c.flush.Flush()
}
c.lock.Unlock()
}

ticker.Stop()
Expand Down

0 comments on commit b1390dc

Please sign in to comment.