Skip to content

Commit

Permalink
Merge pull request #965 from SiaFoundation/chris/interaction-recorder…
Browse files Browse the repository at this point in the history
…-panic

Fix interaction recorder panic
  • Loading branch information
ChrisSchinnerl authored Feb 14, 2024
2 parents 96a697a + d21fd3c commit c5fa4c8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
4 changes: 3 additions & 1 deletion worker/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type (
acc *account
bus Bus
contractSpendingRecorder ContractSpendingRecorder
interactionRecorder HostInteractionRecorder
logger *zap.SugaredLogger
transportPool *transportPoolV3
priceTables *priceTables
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
22 changes: 0 additions & 22 deletions worker/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -1351,7 +1351,7 @@ func (w *worker) Handler() http.Handler {
"PUT /multipart/*path": w.multipartUploadHandlerPUT,

"GET /state": w.stateHandlerGET,
}))
})
}

// Shutdown shuts down the worker.
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit c5fa4c8

Please sign in to comment.