From: Matt Joiner Date: Thu, 4 Jun 2020 01:50:20 +0000 (+1000) Subject: Fix panic in webseed request rejection logging X-Git-Tag: v1.16.0~13 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=62e7f29a0c02b1d6738d440a69a2b49c07b9b182;p=btrtrc.git Fix panic in webseed request rejection logging Also use a new helper in Logger to simplify things. --- diff --git a/client.go b/client.go index a3f0a78a..3832fdee 100644 --- a/client.go +++ b/client.go @@ -354,9 +354,7 @@ func (cl *Client) newAnacrolixDhtServer(conn net.PacketConn) (s *dht.Server, err StartingNodes: cl.config.DhtStartingNodes(conn.LocalAddr().Network()), ConnectionTracking: cl.config.ConnTracker, OnQuery: cl.config.DHTOnQuery, - Logger: cl.logger.WithText(func(m log.Msg) string { - return fmt.Sprintf("dht server on %v: %s", conn.LocalAddr().String(), m.Text()) - }), + Logger: cl.logger.WithContextText(fmt.Sprintf("dht server on %v", conn.LocalAddr().String())), } s, err = dht.NewServer(&cfg) if err == nil { @@ -1096,9 +1094,7 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) ( } t._pendingPieces.NewSet = priorityBitmapStableNewSet t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks(), &cl._mu) - t.logger = cl.logger.WithValues(t).WithText(func(m log.Msg) string { - return fmt.Sprintf("%v: %s", t, m.Text()) - }) + t.logger = cl.logger.WithContextValue(t) t.setChunkSize(defaultChunkSize) return } @@ -1348,9 +1344,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr, writeBuffer: new(bytes.Buffer), } c.peerImpl = c - c.logger = cl.logger.WithValues(c).WithDefaultLevel(log.Debug).WithText(func(m log.Msg) string { - return fmt.Sprintf("%v: %s", c, m.Text()) - }) + c.logger = cl.logger.WithDefaultLevel(log.Debug).WithContextValue(c) c.writerCond.L = cl.locker() c.setRW(connStatsReadWriter{nc, c}) c.r = &rateLimitedReader{ diff --git a/go.mod b/go.mod index d8298624..a56d4113 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/anacrolix/dht/v2 v2.6.1-0.20200416071723-3850fa1b802a github.com/anacrolix/envpprof v1.1.0 github.com/anacrolix/go-libutp v1.0.2 - github.com/anacrolix/log v0.7.0 + github.com/anacrolix/log v0.7.1-0.20200604014615-c244de44fd2d github.com/anacrolix/missinggo v1.2.1 github.com/anacrolix/missinggo/perf v1.0.0 github.com/anacrolix/missinggo/v2 v2.4.1-0.20200419051441-747d9d7544c6 diff --git a/go.sum b/go.sum index c6a1882b..b74356cd 100644 --- a/go.sum +++ b/go.sum @@ -76,6 +76,8 @@ github.com/anacrolix/log v0.6.1-0.20200416071330-f58a030e6149 h1:3cEyLU9ObAfTnBH github.com/anacrolix/log v0.6.1-0.20200416071330-f58a030e6149/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI= github.com/anacrolix/log v0.7.0 h1:koGkC/K0LjIbrhLhwfpsfMuvu8nhvY7J4TmLVc1mAwE= github.com/anacrolix/log v0.7.0/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI= +github.com/anacrolix/log v0.7.1-0.20200604014615-c244de44fd2d h1:AkY1QtaxDBPLr1hrxLD8tUL04EPI0asYc2VB8v7Bg2U= +github.com/anacrolix/log v0.7.1-0.20200604014615-c244de44fd2d/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI= github.com/anacrolix/missinggo v0.0.0-20180522035225-b4a5853e62ff/go.mod h1:b0p+7cn+rWMIphK1gDH2hrDuwGOcbB6V4VXeSsEfHVk= github.com/anacrolix/missinggo v0.0.0-20180725070939-60ef2fbf63df/go.mod h1:kwGiTUTZ0+p4vAz3VbAI5a30t2YbvemcmspjKwrAz5s= github.com/anacrolix/missinggo v0.2.1-0.20190310234110-9fbdc9f242a8 h1:E2Xb2SBsVzHJ1tNMW9QcckYEQcyBKz1ee8qVjeVRWys= diff --git a/peer-impl.go b/peer-impl.go index 41540c47..2b65d1ad 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -18,4 +18,5 @@ type peerImpl interface { _postCancel(request) onGotInfo(*metainfo.Info) drop() + String() string } diff --git a/torrent.go b/torrent.go index bd4a025e..749a5dae 100644 --- a/torrent.go +++ b/torrent.go @@ -2049,6 +2049,7 @@ func (t *Torrent) addWebSeed(url string) { }, requests: make(map[request]webseed.Request, maxRequests), } + ws.peer.logger = t.logger.WithContextValue(&ws) ws.peer.peerImpl = &ws if t.haveInfo() { ws.onGotInfo(t.info) diff --git a/web_seed.go b/web_seed.go index 72bda5fd..c2bb78e1 100644 --- a/web_seed.go +++ b/web_seed.go @@ -1,9 +1,9 @@ package torrent import ( + "fmt" "net/http" - "github.com/anacrolix/log" "github.com/anacrolix/torrent/common" "github.com/anacrolix/torrent/metainfo" pp "github.com/anacrolix/torrent/peer_protocol" @@ -34,6 +34,10 @@ type webSeed struct { var _ peerImpl = (*webSeed)(nil) +func (ws *webSeed) String() string { + return fmt.Sprintf("webseed peer for %q", ws.client.Url) +} + func (ws *webSeed) onGotInfo(info *metainfo.Info) { ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles())) ws.client.Info = info @@ -81,7 +85,7 @@ func (ws *webSeed) requestResultHandler(r request, webseedRequest webseed.Reques ws.peer.t.cl.lock() defer ws.peer.t.cl.unlock() if result.Err != nil { - log.Printf("webseed request rejected: %v", result.Err) + ws.peer.logger.Printf("request %v rejected: %v", r, result.Err) ws.peer.remoteRejectedRequest(r) } else { err := ws.peer.receiveChunk(&pp.Message{