From 159c4a0e285d8e7523fe0dcc4cad3f26146379dd Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 23 Jun 2018 18:33:56 +1000 Subject: [PATCH] Ensure 64-bit alignment of ConnStats fields Fixes #262. --- client.go | 5 +++-- connection.go | 5 +++-- torrent.go | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 705354ba..bc90f0cb 100644 --- a/client.go +++ b/client.go @@ -39,6 +39,9 @@ import ( // Clients contain zero or more Torrents. A Client manages a blocklist, the // TCP/UDP protocol ports, and DHT as desired. type Client struct { + // An aggregate of stats over all connections. First in struct to ensure + // 64-bit alignment of fields. See #262. + stats ConnStats mu sync.RWMutex event sync.Cond closed missinggo.Event @@ -62,8 +65,6 @@ type Client struct { dopplegangerAddrs map[string]struct{} badPeerIPs map[string]struct{} torrents map[metainfo.Hash]*Torrent - // An aggregate of stats over all connections. - stats ConnStats acceptLimiter map[ipStr]int } diff --git a/connection.go b/connection.go index f58ab288..21ebbf1b 100644 --- a/connection.go +++ b/connection.go @@ -38,6 +38,9 @@ const ( // Maintains the state of a connection with a peer. type connection struct { + // First to ensure 64-bit alignment for atomics. See #262. + stats ConnStats + t *Torrent // The actual Conn, used for closing, and setting socket options. conn net.Conn @@ -55,8 +58,6 @@ type connection struct { // other ConnStat instances as determined when the *Torrent became known. reconciledHandshakeStats bool - stats ConnStats - lastMessageReceived time.Time completedHandshake time.Time lastUsefulChunkReceived time.Time diff --git a/torrent.go b/torrent.go index 481d4c34..ccddd573 100644 --- a/torrent.go +++ b/torrent.go @@ -45,6 +45,9 @@ type peersKey struct { // Maintains state of torrent within a Client. type Torrent struct { + // Torrent-level aggregate statistics. First in struct to ensure 64-bit + // alignment. See #262. + stats ConnStats cl *Client logger *log.Logger @@ -134,8 +137,6 @@ type Torrent struct { // These "inclinations" are used to give connections preference for // different pieces. connPieceInclinationPool sync.Pool - // Torrent-level statistics. - stats ConnStats // Count of each request across active connections. pendingRequests map[request]int