mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-21 23:48:11 -07:00
(minor) refactor redis GC and fix TX calls,
add indicator that metrics are running
This commit is contained in:
+17
-20
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/sot-tech/mochi/frontend"
|
||||
"github.com/sot-tech/mochi/pkg/conf"
|
||||
"github.com/sot-tech/mochi/pkg/log"
|
||||
"github.com/sot-tech/mochi/pkg/metrics"
|
||||
"github.com/sot-tech/mochi/pkg/stop"
|
||||
)
|
||||
|
||||
@@ -285,17 +286,15 @@ func injectRouteParamsToContext(ctx context.Context, ps httprouter.Params) conte
|
||||
func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
var err error
|
||||
var start time.Time
|
||||
if f.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
var addr netip.Addr
|
||||
defer func() {
|
||||
if f.EnableRequestTiming {
|
||||
recordResponseDuration("announce", addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration("announce", addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
start = time.Now()
|
||||
defer func() {
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
recordResponseDuration("announce", addr, err, time.Since(start))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
req, err := ParseAnnounce(r, f.ParseOptions)
|
||||
if err != nil {
|
||||
@@ -325,17 +324,15 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps http
|
||||
func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
var err error
|
||||
var start time.Time
|
||||
if f.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
var addr netip.Addr
|
||||
defer func() {
|
||||
if f.EnableRequestTiming {
|
||||
recordResponseDuration("scrape", addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration("scrape", addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
start = time.Now()
|
||||
defer func() {
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
recordResponseDuration("scrape", addr, err, time.Since(start))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
req, err := ParseScrape(r, f.ParseOptions)
|
||||
if err != nil {
|
||||
|
||||
@@ -79,7 +79,7 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
}
|
||||
request.Peer.ID, err = bittorrent.NewPeerID([]byte(peerID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errInvalidPeerID
|
||||
}
|
||||
// Determine the number of remaining bytes for the client.
|
||||
request.Left, err = qp.Uint("left", 64)
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/sot-tech/mochi/frontend/udp/bytepool"
|
||||
"github.com/sot-tech/mochi/pkg/conf"
|
||||
"github.com/sot-tech/mochi/pkg/log"
|
||||
"github.com/sot-tech/mochi/pkg/metrics"
|
||||
"github.com/sot-tech/mochi/pkg/stop"
|
||||
"github.com/sot-tech/mochi/pkg/timecache"
|
||||
)
|
||||
@@ -214,17 +215,15 @@ func (t *Frontend) serve() error {
|
||||
// Handle the request.
|
||||
addr := addrPort.Addr().Unmap()
|
||||
var start time.Time
|
||||
if t.EnableRequestTiming {
|
||||
if t.EnableRequestTiming && metrics.Enabled() {
|
||||
start = time.Now()
|
||||
}
|
||||
action, err := t.handleRequest(
|
||||
Request{(*buffer)[:n], addr},
|
||||
ResponseWriter{t.socket, addrPort},
|
||||
)
|
||||
if t.EnableRequestTiming {
|
||||
if t.EnableRequestTiming && metrics.Enabled() {
|
||||
recordResponseDuration(action, addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration(action, addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ var (
|
||||
errUnknownAction = bittorrent.ClientError("unknown action ID")
|
||||
errBadConnectionID = bittorrent.ClientError("bad connection ID")
|
||||
errUnknownOptionType = bittorrent.ClientError("unknown option type")
|
||||
errInvalidInfoHash = bittorrent.ClientError("invalid info hash")
|
||||
errInvalidPeerID = bittorrent.ClientError("invalid info hash")
|
||||
)
|
||||
|
||||
// ParseOptions is the configuration used to parse an Announce Request.
|
||||
@@ -117,12 +119,12 @@ func ParseAnnounce(r Request, v6Action bool, opts ParseOptions) (*bittorrent.Ann
|
||||
|
||||
ih, err := bittorrent.NewInfoHash(infohash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errInvalidInfoHash
|
||||
}
|
||||
|
||||
peerID, err := bittorrent.NewPeerID(peerIDBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errInvalidPeerID
|
||||
}
|
||||
|
||||
request := &bittorrent.AnnounceRequest{
|
||||
|
||||
Reference in New Issue
Block a user