client.go | 73 ----------------------------------------------------- global.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ misc.go | 14 -------------- torrent.go | 7 ------- diff --git a/client.go b/client.go index 05f8ccf1a97652491ea01dc46e58221b6de1c15c..63413bab558bea3587ad43fb99150126ee2eae42 100644 --- a/client.go +++ b/client.go @@ -7,7 +7,6 @@ "crypto/rand" "crypto/sha1" "encoding/hex" "errors" - "expvar" "fmt" "io" "log" @@ -37,78 +36,6 @@ "github.com/anacrolix/torrent/mse" pp "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/storage" "github.com/anacrolix/torrent/tracker" -) - -// I could move a lot of these counters to their own file, but I suspect they -// may be attached to a Client someday. -var ( - unwantedChunksReceived = expvar.NewInt("chunksReceivedUnwanted") - unexpectedChunksReceived = expvar.NewInt("chunksReceivedUnexpected") - chunksReceived = expvar.NewInt("chunksReceived") - - peersAddedBySource = expvar.NewMap("peersAddedBySource") - - uploadChunksPosted = expvar.NewInt("uploadChunksPosted") - unexpectedCancels = expvar.NewInt("unexpectedCancels") - postedCancels = expvar.NewInt("postedCancels") - duplicateConnsAvoided = expvar.NewInt("duplicateConnsAvoided") - - pieceHashedCorrect = expvar.NewInt("pieceHashedCorrect") - pieceHashedNotCorrect = expvar.NewInt("pieceHashedNotCorrect") - - unsuccessfulDials = expvar.NewInt("dialSuccessful") - successfulDials = expvar.NewInt("dialUnsuccessful") - - acceptUTP = expvar.NewInt("acceptUTP") - acceptTCP = expvar.NewInt("acceptTCP") - acceptReject = expvar.NewInt("acceptReject") - - peerExtensions = expvar.NewMap("peerExtensions") - completedHandshakeConnectionFlags = expvar.NewMap("completedHandshakeConnectionFlags") - // Count of connections to peer with same client ID. - connsToSelf = expvar.NewInt("connsToSelf") - // Number of completed connections to a client we're already connected with. - duplicateClientConns = expvar.NewInt("duplicateClientConns") - receivedMessageTypes = expvar.NewMap("receivedMessageTypes") - receivedKeepalives = expvar.NewInt("receivedKeepalives") - supportedExtensionMessages = expvar.NewMap("supportedExtensionMessages") - postedMessageTypes = expvar.NewMap("postedMessageTypes") - postedKeepalives = expvar.NewInt("postedKeepalives") - // Requests received for pieces we don't have. - requestsReceivedForMissingPieces = expvar.NewInt("requestsReceivedForMissingPieces") -) - -const ( - // Justification for set bits follows. - // - // Extension protocol ([5]|=0x10): - // http://www.bittorrent.org/beps/bep_0010.html - // - // Fast Extension ([7]|=0x04): - // http://bittorrent.org/beps/bep_0006.html. - // Disabled until AllowedFast is implemented. - // - // DHT ([7]|=1): - // http://www.bittorrent.org/beps/bep_0005.html - defaultExtensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x01" - - socketsPerTorrent = 80 - torrentPeersHighWater = 200 - torrentPeersLowWater = 50 - - // Limit how long handshake can take. This is to reduce the lingering - // impact of a few bad apples. 4s loses 1% of successful handshakes that - // are obtained with 60s timeout, and 5% of unsuccessful handshakes. - handshakesTimeout = 20 * time.Second - - // These are our extended message IDs. - metadataExtendedId = iota + 1 // 0 is reserved for deleting keys - pexExtendedId - - // Updated occasionally to when there's been some changes to client - // behaviour in case other clients are assuming anything of us. See also - // `bep20`. - extendedHandshakeClientVersion = "go.torrent dev 20150624" ) // Currently doesn't really queue, but should in the future. diff --git a/global.go b/global.go new file mode 100644 index 0000000000000000000000000000000000000000..5cce77f8a3a1318f65157b225640ee05a7be8ec9 --- /dev/null +++ b/global.go @@ -0,0 +1,96 @@ +package torrent + +import ( + "crypto" + "expvar" + "time" +) + +const ( + pieceHash = crypto.SHA1 + maxRequests = 250 // Maximum pending requests we allow peers to send us. + defaultChunkSize = 0x4000 // 16KiB + + // Updated occasionally to when there's been some changes to client + // behaviour in case other clients are assuming anything of us. See also + // `bep20`. + extendedHandshakeClientVersion = "go.torrent dev 20150624" + // Peer ID client identifier prefix. We'll update this occasionally to + // reflect changes to client behaviour that other clients may depend on. + // Also see `extendedHandshakeClientVersion`. + bep20 = "-GT0001-" + + nominalDialTimeout = time.Second * 30 + minDialTimeout = 5 * time.Second + + // Justification for set bits follows. + // + // Extension protocol ([5]|=0x10): + // http://www.bittorrent.org/beps/bep_0010.html + // + // Fast Extension ([7]|=0x04): + // http://bittorrent.org/beps/bep_0006.html. + // Disabled until AllowedFast is implemented. + // + // DHT ([7]|=1): + // http://www.bittorrent.org/beps/bep_0005.html + defaultExtensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x01" + + socketsPerTorrent = 80 + torrentPeersHighWater = 200 + torrentPeersLowWater = 50 + + // Limit how long handshake can take. This is to reduce the lingering + // impact of a few bad apples. 4s loses 1% of successful handshakes that + // are obtained with 60s timeout, and 5% of unsuccessful handshakes. + handshakesTimeout = 20 * time.Second + + // These are our extended message IDs. Peers will use these values to + // select which extension a message is intended for. + metadataExtendedId = iota + 1 // 0 is reserved for deleting keys + pexExtendedId +) + +// I could move a lot of these counters to their own file, but I suspect they +// may be attached to a Client someday. +var ( + unwantedChunksReceived = expvar.NewInt("chunksReceivedUnwanted") + unexpectedChunksReceived = expvar.NewInt("chunksReceivedUnexpected") + chunksReceived = expvar.NewInt("chunksReceived") + + peersAddedBySource = expvar.NewMap("peersAddedBySource") + + uploadChunksPosted = expvar.NewInt("uploadChunksPosted") + unexpectedCancels = expvar.NewInt("unexpectedCancels") + postedCancels = expvar.NewInt("postedCancels") + duplicateConnsAvoided = expvar.NewInt("duplicateConnsAvoided") + + pieceHashedCorrect = expvar.NewInt("pieceHashedCorrect") + pieceHashedNotCorrect = expvar.NewInt("pieceHashedNotCorrect") + + unsuccessfulDials = expvar.NewInt("dialSuccessful") + successfulDials = expvar.NewInt("dialUnsuccessful") + + acceptUTP = expvar.NewInt("acceptUTP") + acceptTCP = expvar.NewInt("acceptTCP") + acceptReject = expvar.NewInt("acceptReject") + + peerExtensions = expvar.NewMap("peerExtensions") + completedHandshakeConnectionFlags = expvar.NewMap("completedHandshakeConnectionFlags") + // Count of connections to peer with same client ID. + connsToSelf = expvar.NewInt("connsToSelf") + // Number of completed connections to a client we're already connected with. + duplicateClientConns = expvar.NewInt("duplicateClientConns") + receivedMessageTypes = expvar.NewMap("receivedMessageTypes") + receivedKeepalives = expvar.NewInt("receivedKeepalives") + supportedExtensionMessages = expvar.NewMap("supportedExtensionMessages") + postedMessageTypes = expvar.NewMap("postedMessageTypes") + postedKeepalives = expvar.NewInt("postedKeepalives") + // Requests received for pieces we don't have. + requestsReceivedForMissingPieces = expvar.NewInt("requestsReceivedForMissingPieces") + + // Track the effectiveness of Torrent.connPieceInclinationPool. + pieceInclinationsReused = expvar.NewInt("pieceInclinationsReused") + pieceInclinationsNew = expvar.NewInt("pieceInclinationsNew") + pieceInclinationsPut = expvar.NewInt("pieceInclinationsPut") +) diff --git a/misc.go b/misc.go index 37283ee106a4d21b6cb7305c812ff9c6fb970682..42a65fe7d27314fec3244c225d3be85f23334951 100644 --- a/misc.go +++ b/misc.go @@ -1,24 +1,10 @@ package torrent import ( - "crypto" "errors" - "time" "github.com/anacrolix/torrent/metainfo" pp "github.com/anacrolix/torrent/peer_protocol" -) - -const ( - pieceHash = crypto.SHA1 - maxRequests = 250 // Maximum pending requests we allow peers to send us. - defaultChunkSize = 0x4000 // 16KiB - // Peer ID client identifier prefix. We'll update this occasionally to - // reflect changes to client behaviour that other clients may depend on. - // Also see `extendedHandshakeClientVersion`. - bep20 = "-GT0001-" - nominalDialTimeout = time.Second * 30 - minDialTimeout = 5 * time.Second ) type chunkSpec struct { diff --git a/torrent.go b/torrent.go index a54a9a12b5bf76e3f4303c1c869b72f26bbb1c5e..80ec38316ae18517397ba1add81e6c86cf693ea8 100644 --- a/torrent.go +++ b/torrent.go @@ -2,7 +2,6 @@ package torrent import ( "container/heap" - "expvar" "fmt" "io" "log" @@ -94,12 +93,6 @@ completedPieces bitmap.Bitmap connPieceInclinationPool sync.Pool } - -var ( - pieceInclinationsReused = expvar.NewInt("pieceInclinationsReused") - pieceInclinationsNew = expvar.NewInt("pieceInclinationsNew") - pieceInclinationsPut = expvar.NewInt("pieceInclinationsPut") -) func (t *Torrent) setDisplayName(dn string) { t.displayName = dn