diff --git a/worker/host.go b/worker/host.go index fcaac0cf1..86e92ce27 100644 --- a/worker/host.go +++ b/worker/host.go @@ -52,6 +52,7 @@ type ( acc *account bus Bus contractSpendingRecorder ContractSpendingRecorder + interactionRecorder HostInteractionRecorder logger *zap.SugaredLogger transportPool *transportPoolV3 priceTables *priceTables @@ -69,6 +70,7 @@ func (w *worker) Host(hk types.PublicKey, fcid types.FileContractID, siamuxAddr acc: w.accounts.ForHost(hk), bus: w.bus, contractSpendingRecorder: w.contractSpendingRecorder, + interactionRecorder: w.hostInteractionRecorder, logger: w.logger.Named(hk.String()[:4]), fcid: fcid, siamuxAddr: siamuxAddr, @@ -196,7 +198,7 @@ func (h *host) FetchPriceTable(ctx context.Context, rev *types.FileContractRevis fetchPT := func(paymentFn PriceTablePaymentFunc) (hpt hostdb.HostPriceTable, err error) { err = h.transportPool.withTransportV3(ctx, h.hk, h.siamuxAddr, func(ctx context.Context, t *transportV3) (err error) { hpt, err = RPCPriceTable(ctx, t, paymentFn) - HostInteractionRecorderFromContext(ctx).RecordPriceTableUpdate(hostdb.PriceTableUpdate{ + h.interactionRecorder.RecordPriceTableUpdate(hostdb.PriceTableUpdate{ HostKey: h.hk, Success: isSuccessfulInteraction(err), Timestamp: time.Now(), diff --git a/worker/interactions.go b/worker/interactions.go index 70629c1f0..dfe8c4017 100644 --- a/worker/interactions.go +++ b/worker/interactions.go @@ -3,11 +3,9 @@ package worker import ( "context" "fmt" - "net/http" "sync" "time" - "go.sia.tech/jape" "go.sia.tech/renterd/hostdb" "go.uber.org/zap" ) @@ -42,26 +40,6 @@ var ( _ HostInteractionRecorder = (*hostInteractionRecorder)(nil) ) -func HostInteractionRecorderFromContext(ctx context.Context) HostInteractionRecorder { - ir, ok := ctx.Value(keyInteractionRecorder).(HostInteractionRecorder) - if !ok { - panic("no interaction recorder attached to the context") // developer error - } - return ir -} - -func interactionMiddleware(ir HostInteractionRecorder, routes map[string]jape.Handler) map[string]jape.Handler { - for route, handler := range routes { - routes[route] = jape.Adapt(func(h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), keyInteractionRecorder, ir) - h.ServeHTTP(w, r.WithContext(ctx)) - }) - })(handler) - } - return routes -} - func (w *worker) initHostInteractionRecorder(flushInterval time.Duration) { if w.hostInteractionRecorder != nil { panic("HostInteractionRecorder already initialized") // developer error diff --git a/worker/worker.go b/worker/worker.go index b917960d4..5ed7e8a05 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -342,7 +342,7 @@ func (w *worker) rhpPriceTableHandler(jc jape.Context) { var err error var hpt hostdb.HostPriceTable defer func() { - HostInteractionRecorderFromContext(ctx).RecordPriceTableUpdate(hostdb.PriceTableUpdate{ + w.hostInteractionRecorder.RecordPriceTableUpdate(hostdb.PriceTableUpdate{ HostKey: rptr.HostKey, Success: isSuccessfulInteraction(err), Timestamp: time.Now(), @@ -1323,7 +1323,7 @@ func New(masterKey [32]byte, id string, b Bus, contractLockingDuration, busFlush // Handler returns an HTTP handler that serves the worker API. func (w *worker) Handler() http.Handler { - return jape.Mux(interactionMiddleware(w.hostInteractionRecorder, map[string]jape.Handler{ + return jape.Mux(map[string]jape.Handler{ "GET /account/:hostkey": w.accountHandlerGET, "GET /id": w.idHandlerGET, @@ -1351,7 +1351,7 @@ func (w *worker) Handler() http.Handler { "PUT /multipart/*path": w.multipartUploadHandlerPUT, "GET /state": w.stateHandlerGET, - })) + }) } // Shutdown shuts down the worker. @@ -1442,7 +1442,7 @@ func (w *worker) scanHost(ctx context.Context, hostKey types.PublicKey, hostIP s } // record host scan - HostInteractionRecorderFromContext(ctx).RecordHostScan(hostdb.HostScan{ + w.hostInteractionRecorder.RecordHostScan(hostdb.HostScan{ HostKey: hostKey, Success: isSuccessfulInteraction(err), Timestamp: time.Now(),