]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Rename per-torrent ws tracker and output stats
authorMatt Joiner <anacrolix@gmail.com>
Wed, 22 Apr 2020 01:42:31 +0000 (11:42 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 22 Apr 2020 01:42:31 +0000 (11:42 +1000)
torrent.go
webtorrent/tracker_client.go
wstracker.go

index 11fb8eba458734de8248246e056cd08cddc1efeb..0117094a06b0c49867d7e8b23ff33224f69ea1a5 100644 (file)
@@ -1330,7 +1330,7 @@ func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
                <-t.closed.LockedChan(t.cl.locker())
                release()
        }()
-       wst := websocketTracker{u, wtc}
+       wst := websocketTrackerStatus{u, wtc}
        go func() {
                err := wtc.Announce(tracker.Started, t.infoHash)
                if err != nil {
index 14b62ed28a5d6faca5542b627cab64b8df090d3d..0948cdcdeacad63228fcc63296ecaf080d604995 100644 (file)
@@ -15,6 +15,12 @@ import (
        "github.com/pion/webrtc/v2"
 )
 
+type TrackerClientStats struct {
+       Dials                  int64
+       ConvertedInboundConns  int64
+       ConvertedOutboundConns int64
+}
+
 // Client represents the webtorrent client
 type TrackerClient struct {
        Url                string
@@ -28,6 +34,13 @@ type TrackerClient struct {
        outboundOffers map[string]outboundOffer // OfferID to outboundOffer
        wsConn         *websocket.Conn
        closed         bool
+       stats          TrackerClientStats
+}
+
+func (me *TrackerClient) Stats() TrackerClientStats {
+       me.mu.Lock()
+       defer me.mu.Unlock()
+       return me.stats
 }
 
 func (me *TrackerClient) peerIdBinary() string {
@@ -53,6 +66,7 @@ type onDataChannelOpen func(_ datachannel.ReadWriteCloser, dcc DataChannelContex
 
 func (tc *TrackerClient) doWebsocket() error {
        metrics.Add("websocket dials", 1)
+       tc.stats.Dials++
        c, _, err := websocket.DefaultDialer.Dial(tc.Url, nil)
        if err != nil {
                return fmt.Errorf("dialing tracker: %w", err)
@@ -232,6 +246,9 @@ func (tc *TrackerClient) handleOffer(
                setDataChannelOnOpen(d, peerConnection, func(dc datachannel.ReadWriteCloser) {
                        timer.Stop()
                        metrics.Add("answering peer connection conversions", 1)
+                       tc.mu.Lock()
+                       tc.stats.ConvertedInboundConns++
+                       tc.mu.Unlock()
                        tc.OnConn(dc, DataChannelContext{
                                Local:        answer,
                                Remote:       offer,
@@ -256,6 +273,9 @@ func (tc *TrackerClient) handleAnswer(offerId string, answer webrtc.SessionDescr
        metrics.Add("outbound offers answered", 1)
        err := offer.setAnswer(answer, func(dc datachannel.ReadWriteCloser) {
                metrics.Add("outbound offers answered with datachannel", 1)
+               tc.mu.Lock()
+               tc.stats.ConvertedOutboundConns++
+               tc.mu.Unlock()
                tc.OnConn(dc, DataChannelContext{
                        Local:        offer.originalOffer,
                        Remote:       answer,
index 95780d774eb5fca1b69d550abf9c74bae4efc3d7..6f376c55e5aa3a805354f9fc9e73cfd492cecfe0 100644 (file)
@@ -12,16 +12,16 @@ import (
        "github.com/pion/datachannel"
 )
 
-type websocketTracker struct {
+type websocketTrackerStatus struct {
        url url.URL
-       *webtorrent.TrackerClient
+       tc  *webtorrent.TrackerClient
 }
 
-func (me websocketTracker) statusLine() string {
-       return fmt.Sprintf("%q", me.url.String())
+func (me websocketTrackerStatus) statusLine() string {
+       return fmt.Sprintf("%q: %+v", me.tc.Url, me.tc.Stats())
 }
 
-func (me websocketTracker) URL() url.URL {
+func (me websocketTrackerStatus) URL() url.URL {
        return me.url
 }