Skip to content

Commit

Permalink
feat(torch): change context to context.WithTimeout
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <jose@celestia.org>
  • Loading branch information
tty47 committed Oct 26, 2023
1 parent d5d0d80 commit 163b985
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions pkg/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
"encoding/json"
"errors"
"net/http"
"time"

"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"

"github.com/celestiaorg/torch/config"
"github.com/celestiaorg/torch/pkg/db/redis"
"github.com/celestiaorg/torch/pkg/nodes"

"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)

// errorMsg common error message.
const errorMsg = "Error: "
const (
errorMsg = "Error: " // errorMsg common error message.
timeoutDuration = 30 * time.Second // timeoutDuration we specify the max time to run the func.
)

type RequestBody struct {
// Body response response body.
Expand Down Expand Up @@ -52,7 +55,11 @@ func GetConfig(w http.ResponseWriter, cfg config.MutualPeersConfig) {
// List handles the HTTP GET request for retrieving the list of matching pods as JSON.
func List(w http.ResponseWriter) {
red := redis.InitRedisConfig()
ctx := context.TODO()
// Create a new context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)

// Make sure to call the cancel function to release resources when you're done
defer cancel()

// get all values from redis
nodeIDs, err := red.GetAllKeys(ctx)
Expand Down Expand Up @@ -91,7 +98,11 @@ func GetNoId(w http.ResponseWriter, r *http.Request, cfg config.MutualPeersConfi
}

red := redis.InitRedisConfig()
ctx := context.TODO()
// Create a new context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)

// Make sure to call the cancel function to release resources when you're done
defer cancel()

// initialize the response struct
resp := Response{}
Expand Down

0 comments on commit 163b985

Please sign in to comment.