Skip to content

Commit

Permalink
Setting timeout in server
Browse files Browse the repository at this point in the history
  • Loading branch information
yashnevatia committed Jan 4, 2024
1 parent f59cc4f commit 71c9f8d
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cmd

import (
"context"
"math/big"
"fmt"
"os"
"math/big"
"net/http"
"os"

Check failure on line 8 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Unit test

"os" imported and not used

Check failure on line 8 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Build

"os" imported and not used

"github.com/smartcontractkit/timelock-worker/pkg/cli"
"github.com/smartcontractkit/timelock-worker/pkg/timelock"
Expand Down Expand Up @@ -93,19 +93,21 @@ func startHandler(cmd *cobra.Command) {
logs.Info().Msg("shutting down timelock-worker")
}

// starts a http server, serving the healthz endpoint
// starts a http server, serving the healthz endpoint.
func startServer() {
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})

port := 8080
addr := fmt.Sprintf(":%d", port)

fmt.Printf("Starting HTTP server on port %d...\n", port)
if err := http.ListenAndServe(addr, nil); err != nil {
fmt.Println("Error starting HTTP server:", err)
os.Exit(1)
mux := http.NewServeMux()
mux.HandleFunc("/health", healthHandler)

Check failure on line 99 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Unit test

undefined: healthHandler

Check failure on line 99 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Build

undefined: healthHandler

server := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second, // Set your desired read timeout

Check failure on line 104 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Unit test

undefined: time

Check failure on line 104 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Build

undefined: time
WriteTimeout: 10 * time.Second, // Set your desired write timeout

Check failure on line 105 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Unit test

undefined: time

Check failure on line 105 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Build

undefined: time
IdleTimeout: 15 * time.Second, // Set your desired idle timeout

Check failure on line 106 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Unit test

undefined: time

Check failure on line 106 in cmd/start.go

View workflow job for this annotation

GitHub Actions / Build

undefined: time
}
}

fmt.Println("Server listening on :8080")
if err := server.ListenAndServe(); err != nil {
fmt.Println(err)
}
}

0 comments on commit 71c9f8d

Please sign in to comment.