Skip to content

Commit b1390dc

Browse files
committed
add some locking
1 parent 8bcb960 commit b1390dc

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Client struct {
1616
events chan *Event
1717
closed bool
1818
waiter sync.WaitGroup
19+
lock sync.Mutex
1920
lastFlush time.Time
2021
lastWrite time.Time
2122
}
@@ -99,8 +100,10 @@ func (c *Client) run() {
99100
}
100101

101102
// send the event
103+
c.lock.Lock()
102104
io.Copy(c.write, ev)
103105
c.lastWrite = time.Now()
106+
c.lock.Unlock()
104107

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

118121
for !c.closed {
119122
<-ticker.C
123+
c.lock.Lock()
120124
if c.lastFlush.Before(c.lastWrite) {
121125
c.lastFlush = c.lastWrite
122126
c.flush.Flush()
123127
}
128+
c.lock.Unlock()
124129
}
125130

126131
ticker.Stop()

0 commit comments

Comments
 (0)