Rename per-torrent ws tracker and output stats

This commit is contained in:
Matt Joiner 2020-04-22 11:42:31 +10:00
parent a5db7a7fa6
commit 3226dc1ccd
3 changed files with 26 additions and 6 deletions

View File

@ -1330,7 +1330,7 @@ func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
<-t.closed.LockedChan(t.cl.locker()) <-t.closed.LockedChan(t.cl.locker())
release() release()
}() }()
wst := websocketTracker{u, wtc} wst := websocketTrackerStatus{u, wtc}
go func() { go func() {
err := wtc.Announce(tracker.Started, t.infoHash) err := wtc.Announce(tracker.Started, t.infoHash)
if err != nil { if err != nil {

View File

@ -15,6 +15,12 @@ import (
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
type TrackerClientStats struct {
Dials int64
ConvertedInboundConns int64
ConvertedOutboundConns int64
}
// Client represents the webtorrent client // Client represents the webtorrent client
type TrackerClient struct { type TrackerClient struct {
Url string Url string
@ -28,6 +34,13 @@ type TrackerClient struct {
outboundOffers map[string]outboundOffer // OfferID to outboundOffer outboundOffers map[string]outboundOffer // OfferID to outboundOffer
wsConn *websocket.Conn wsConn *websocket.Conn
closed bool closed bool
stats TrackerClientStats
}
func (me *TrackerClient) Stats() TrackerClientStats {
me.mu.Lock()
defer me.mu.Unlock()
return me.stats
} }
func (me *TrackerClient) peerIdBinary() string { func (me *TrackerClient) peerIdBinary() string {
@ -53,6 +66,7 @@ type onDataChannelOpen func(_ datachannel.ReadWriteCloser, dcc DataChannelContex
func (tc *TrackerClient) doWebsocket() error { func (tc *TrackerClient) doWebsocket() error {
metrics.Add("websocket dials", 1) metrics.Add("websocket dials", 1)
tc.stats.Dials++
c, _, err := websocket.DefaultDialer.Dial(tc.Url, nil) c, _, err := websocket.DefaultDialer.Dial(tc.Url, nil)
if err != nil { if err != nil {
return fmt.Errorf("dialing tracker: %w", err) return fmt.Errorf("dialing tracker: %w", err)
@ -232,6 +246,9 @@ func (tc *TrackerClient) handleOffer(
setDataChannelOnOpen(d, peerConnection, func(dc datachannel.ReadWriteCloser) { setDataChannelOnOpen(d, peerConnection, func(dc datachannel.ReadWriteCloser) {
timer.Stop() timer.Stop()
metrics.Add("answering peer connection conversions", 1) metrics.Add("answering peer connection conversions", 1)
tc.mu.Lock()
tc.stats.ConvertedInboundConns++
tc.mu.Unlock()
tc.OnConn(dc, DataChannelContext{ tc.OnConn(dc, DataChannelContext{
Local: answer, Local: answer,
Remote: offer, Remote: offer,
@ -256,6 +273,9 @@ func (tc *TrackerClient) handleAnswer(offerId string, answer webrtc.SessionDescr
metrics.Add("outbound offers answered", 1) metrics.Add("outbound offers answered", 1)
err := offer.setAnswer(answer, func(dc datachannel.ReadWriteCloser) { err := offer.setAnswer(answer, func(dc datachannel.ReadWriteCloser) {
metrics.Add("outbound offers answered with datachannel", 1) metrics.Add("outbound offers answered with datachannel", 1)
tc.mu.Lock()
tc.stats.ConvertedOutboundConns++
tc.mu.Unlock()
tc.OnConn(dc, DataChannelContext{ tc.OnConn(dc, DataChannelContext{
Local: offer.originalOffer, Local: offer.originalOffer,
Remote: answer, Remote: answer,

View File

@ -12,16 +12,16 @@ import (
"github.com/pion/datachannel" "github.com/pion/datachannel"
) )
type websocketTracker struct { type websocketTrackerStatus struct {
url url.URL url url.URL
*webtorrent.TrackerClient tc *webtorrent.TrackerClient
} }
func (me websocketTracker) statusLine() string { func (me websocketTrackerStatus) statusLine() string {
return fmt.Sprintf("%q", me.url.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 return me.url
} }