Skip to content

Commit 470b7b8

Browse files
chalvernlonng
authored andcommitted
Last at data race bugfix (#45)
1 parent 5944d85 commit 470b7b8

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
1515

16-
.idea/
16+
.idea/
17+
.vscode/

agent.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (a *agent) RemoteAddr() net.Addr {
198198

199199
// String, implementation for Stringer interface
200200
func (a *agent) String() string {
201-
return fmt.Sprintf("Remote=%s, LastTime=%d", a.conn.RemoteAddr().String(), a.lastAt)
201+
return fmt.Sprintf("Remote=%s, LastTime=%d", a.conn.RemoteAddr().String(), atomic.LoadInt64(&a.lastAt))
202202
}
203203

204204
func (a *agent) status() int32 {
@@ -227,8 +227,8 @@ func (a *agent) write() {
227227
select {
228228
case <-ticker.C:
229229
deadline := time.Now().Add(-2 * env.heartbeat).Unix()
230-
if a.lastAt < deadline {
231-
logger.Println(fmt.Sprintf("Session heartbeat timeout, LastTime=%d, Deadline=%d", a.lastAt, deadline))
230+
if atomic.LoadInt64(&a.lastAt) < deadline {
231+
logger.Println(fmt.Sprintf("Session heartbeat timeout, LastTime=%d, Deadline=%d", atomic.LoadInt64(&a.lastAt), deadline))
232232
return
233233
}
234234
chWrite <- hbd

handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626
"net"
2727
"reflect"
28+
"sync/atomic"
2829
"time"
2930

3031
"github.com/lonng/nano/component"
@@ -276,7 +277,7 @@ func (h *handlerService) processPacket(agent *agent, p *packet.Packet) error {
276277
// expected
277278
}
278279

279-
agent.lastAt = time.Now().Unix()
280+
atomic.StoreInt64(&agent.lastAt, time.Now().Unix())
280281
return nil
281282
}
282283

0 commit comments

Comments
 (0)