]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add lots of new expvars and perf timers
authorMatt Joiner <anacrolix@gmail.com>
Fri, 15 Jun 2018 12:42:05 +0000 (22:42 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 15 Jun 2018 12:42:05 +0000 (22:42 +1000)
client.go
mse/mse.go
socket.go
torrent.go

index b7d57b0994297d363bf6055e2ecc47dd8650ee7b..491424314c910742c795ac449cb080f5aca1f3eb 100644 (file)
--- a/client.go
+++ b/client.go
@@ -14,6 +14,8 @@ import (
        "strings"
        "time"
 
+       "github.com/anacrolix/missinggo/perf"
+
        "github.com/anacrolix/dht"
        "github.com/anacrolix/dht/krpc"
        "github.com/anacrolix/log"
@@ -541,9 +543,12 @@ func (cl *Client) dialFirst(ctx context.Context, addr string) net.Conn {
        }()
        var res dialResult
        // Wait for a successful connection.
-       for ; left > 0 && res.Conn == nil; left-- {
-               res = <-resCh
-       }
+       func() {
+               defer perf.ScopeTimer()()
+               for ; left > 0 && res.Conn == nil; left-- {
+                       res = <-resCh
+               }
+       }()
        // There are still incompleted dials.
        go func() {
                for ; left > 0; left-- {
@@ -709,6 +714,7 @@ func (cl *Client) forSkeys(f func([]byte) bool) {
 
 // Do encryption and bittorrent handshakes as receiver.
 func (cl *Client) receiveHandshakes(c *connection) (t *Torrent, err error) {
+       defer perf.ScopeTimerErr(&err)()
        var rw io.ReadWriter
        rw, c.headerEncrypted, c.cryptoMethod, err = handleEncryption(c.rw(), cl.forSkeys, cl.config.EncryptionPolicy)
        c.setRW(rw)
@@ -756,18 +762,27 @@ func (cl *Client) runReceivedConn(c *connection) {
        }
        t, err := cl.receiveHandshakes(c)
        if err != nil {
-               log.Fmsg("error receiving handshakes: %s", err).AddValue(debugLogValue).Add("network", c.remoteAddr().Network()).Log(cl.logger)
+               log.Fmsg(
+                       "error receiving handshakes: %s", err,
+               ).AddValue(
+                       debugLogValue,
+               ).Add(
+                       "network", c.remoteAddr().Network(),
+               ).Log(cl.logger)
+               torrent.Add("error receiving handshake", 1)
                cl.mu.Lock()
                cl.onBadAccept(c.remoteAddr())
                cl.mu.Unlock()
                return
        }
        if t == nil {
+               torrent.Add("received handshake for unloaded torrent", 1)
                cl.mu.Lock()
                cl.onBadAccept(c.remoteAddr())
                cl.mu.Unlock()
                return
        }
+       torrent.Add("received handshake for loaded torrent", 1)
        cl.mu.Lock()
        defer cl.mu.Unlock()
        cl.runHandshookConn(c, t)
index 83454b82e510cb22a03245330393eb94f2c9ed09..b10090c2bb5be2692e7a436b18af2dc54d5830ce 100644 (file)
@@ -18,6 +18,8 @@ import (
        "strconv"
        "sync"
 
+       "github.com/anacrolix/missinggo/perf"
+
        "github.com/bradfitz/iter"
 )
 
@@ -537,6 +539,7 @@ func InitiateHandshake(rw io.ReadWriter, skey []byte, initialPayload []byte, cry
                ia:             initialPayload,
                cryptoProvides: cryptoProvides,
        }
+       defer perf.ScopeTimerErr(&err)()
        return h.Do()
 }
 
index 33a19bc242e0afe3bb170f62b6bce849adf4b667..716b0c622b9f2dd62992c34d7b11f98d81c9e955 100644 (file)
--- a/socket.go
+++ b/socket.go
@@ -11,6 +11,7 @@ import (
        "golang.org/x/net/proxy"
 
        "github.com/anacrolix/missinggo"
+       "github.com/anacrolix/missinggo/perf"
 )
 
 type dialer interface {
@@ -169,7 +170,8 @@ type utpSocketSocket struct {
        d       proxy.Dialer
 }
 
-func (me utpSocketSocket) dial(ctx context.Context, addr string) (net.Conn, error) {
+func (me utpSocketSocket) dial(ctx context.Context, addr string) (conn net.Conn, err error) {
+       defer perf.ScopeTimerErr(&err)()
        if me.d != nil {
                return me.d.Dial(me.network, addr)
        }
index 2bb5f20bb9995744b9ab247edc76fe9f78496cdc..3c6c3d5291f83fdfd15ea28a0e1570db384cf099 100644 (file)
@@ -1222,6 +1222,7 @@ func (t *Torrent) deleteConnection(c *connection) (ret bool) {
        }
        _, ret = t.conns[c]
        delete(t.conns, c)
+       torrent.Add("deleted connections", 1)
        c.deleteAllRequests()
        if len(t.conns) == 0 {
                t.assertNoPendingRequests()
@@ -1493,7 +1494,12 @@ func (t *Torrent) reconcileHandshakeStats(c *connection) {
 }
 
 // Returns true if the connection is added.
-func (t *Torrent) addConnection(c *connection) error {
+func (t *Torrent) addConnection(c *connection) (err error) {
+       defer func() {
+               if err == nil {
+                       torrent.Add("added connections", 1)
+               }
+       }()
        if t.closed.IsSet() {
                return errors.New("torrent closed")
        }