Skip to content

Commit

Permalink
Fix panic in webseed request rejection logging
Browse files Browse the repository at this point in the history
Also use a new helper in Logger to simplify things.
  • Loading branch information
anacrolix committed Jun 4, 2020
1 parent 6459016 commit 62e7f29
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
12 changes: 3 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions peer-impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type peerImpl interface {
_postCancel(request)
onGotInfo(*metainfo.Info)
drop()
String() string
}
1 change: 1 addition & 0 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions web_seed.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 62e7f29

Please sign in to comment.