From f538b15d56c8bc3891b1395544e4ad8fb4c8028f Mon Sep 17 00:00:00 2001 From: gabe Date: Mon, 17 Jun 2024 20:29:56 -0700 Subject: [PATCH] fix failed count; improved logging --- impl/pkg/server/dht.go | 18 +++++++++--------- impl/pkg/storage/db/bolt/bolt.go | 4 +--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/impl/pkg/server/dht.go b/impl/pkg/server/dht.go index b0ffff87..9c83839b 100644 --- a/impl/pkg/server/dht.go +++ b/impl/pkg/server/dht.go @@ -52,11 +52,11 @@ func (r *DHTRouter) GetRecord(c *gin.Context) { // make sure the key is valid key, err := util.Z32Decode(*id) if err != nil { - LoggingRespondErrWithMsg(c, err, "invalid record id", http.StatusInternalServerError) + LoggingRespondErrWithMsg(c, err, fmt.Sprintf("invalid record id: %s", *id), http.StatusInternalServerError) return } if len(key) != ed25519.PublicKeySize { - LoggingRespondErrMsg(c, "invalid z32 encoded ed25519 public key", http.StatusBadRequest) + LoggingRespondErrMsg(c, fmt.Sprintf("invalid z32 encoded ed25519 public key: %s", *id), http.StatusBadRequest) return } @@ -67,11 +67,11 @@ func (r *DHTRouter) GetRecord(c *gin.Context) { LoggingRespondErrMsg(c, fmt.Sprintf("too many requests for bad key %s", *id), http.StatusTooManyRequests) return } - LoggingRespondErrWithMsg(c, err, "failed to get dht record", http.StatusInternalServerError) + LoggingRespondErrWithMsg(c, err, fmt.Sprintf("failed to get dht record: %s", *id), http.StatusInternalServerError) return } if resp == nil { - LoggingRespondErrMsg(c, "dht record not found", http.StatusNotFound) + LoggingRespondErrMsg(c, fmt.Sprintf("dht record not found: %s", *id), http.StatusNotFound) return } @@ -106,24 +106,24 @@ func (r *DHTRouter) PutRecord(c *gin.Context) { } key, err := util.Z32Decode(*id) if err != nil { - LoggingRespondErrWithMsg(c, err, "invalid record id", http.StatusInternalServerError) + LoggingRespondErrWithMsg(c, err, fmt.Sprintf("invalid record id: %s", *id), http.StatusInternalServerError) return } if len(key) != ed25519.PublicKeySize { - LoggingRespondErrMsg(c, "invalid z32 encoded ed25519 public key", http.StatusBadRequest) + LoggingRespondErrMsg(c, fmt.Sprintf("invalid z32 encoded ed25519 public key: %s", *id), http.StatusBadRequest) return } body, err := io.ReadAll(c.Request.Body) if err != nil { - LoggingRespondErrWithMsg(c, err, "failed to read body", http.StatusInternalServerError) + LoggingRespondErrWithMsg(c, err, fmt.Sprintf("failed to read body for id: %s", *id), http.StatusInternalServerError) return } defer c.Request.Body.Close() // 64 byte signature and 8 byte sequence number if len(body) <= 72 { - LoggingRespondErrMsg(c, "invalid request body", http.StatusBadRequest) + LoggingRespondErrMsg(c, fmt.Sprintf("invalid request body for id: %s", *id), http.StatusBadRequest) return } @@ -138,7 +138,7 @@ func (r *DHTRouter) PutRecord(c *gin.Context) { } if err = r.service.PublishDHT(ctx, *id, *request); err != nil { - LoggingRespondErrWithMsg(c, err, "failed to publish dht record", http.StatusInternalServerError) + LoggingRespondErrWithMsg(c, err, fmt.Sprintf("failed to publish dht record: %s", *id), http.StatusInternalServerError) return } diff --git a/impl/pkg/storage/db/bolt/bolt.go b/impl/pkg/storage/db/bolt/bolt.go index 6b9366d8..d8e9d159 100644 --- a/impl/pkg/storage/db/bolt/bolt.go +++ b/impl/pkg/storage/db/bolt/bolt.go @@ -235,9 +235,7 @@ func (b *Bolt) WriteFailedRecord(ctx context.Context, id string) error { var count int32 = 1 v := bucket.Get([]byte(id)) if v != nil { - if err = json.Unmarshal(v, &count); err != nil { - return err - } + count = int32(binary.LittleEndian.Uint32(v)) count++ }