(minor) refactor redis GC and fix TX calls,

add indicator that metrics are running
This commit is contained in:
Lawrence, Rendall
2022-04-17 00:57:30 +03:00
parent 7716aa828a
commit f9c72341c0
12 changed files with 169 additions and 137 deletions
+17 -20
View File
@@ -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 {
+1 -1
View File
@@ -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)
+3 -4
View File
@@ -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))
}
}()
}
+4 -2
View File
@@ -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{