Skip to content

Commit

Permalink
Move context creation and add timeout to propely handle timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Oded-B committed May 31, 2024
1 parent 7f9a0ac commit ab0adee
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cmd/telefonistka/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ func init() { //nolint:gochecknoinits
rootCmd.AddCommand(serveCmd)
}

func handleWebhook(ctx context.Context, githubWebhookSecret []byte, mainGhClientCache *lru.Cache[string, githubapi.GhClientPair], prApproverGhClientCache *lru.Cache[string, githubapi.GhClientPair]) func(http.ResponseWriter, *http.Request) {
func handleWebhook(githubWebhookSecret []byte, mainGhClientCache *lru.Cache[string, githubapi.GhClientPair], prApproverGhClientCache *lru.Cache[string, githubapi.GhClientPair]) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
githubapi.HandleEvent(r, ctx, mainGhClientCache, prApproverGhClientCache, githubWebhookSecret)
}
}

func serve() {
ctx := context.Background()
githubWebhookSecret := []byte(getCrucialEnv("GITHUB_WEBHOOK_SECRET"))
livenessChecker := health.NewChecker() // No checks for the moment, other then the http server availability
readinessChecker := health.NewChecker()
Expand All @@ -54,7 +55,7 @@ func serve() {
prApproverGhClientCache, _ := lru.New[string, githubapi.GhClientPair](128)

mux := http.NewServeMux()
mux.HandleFunc("/webhook", handleWebhook(ctx, githubWebhookSecret, mainGhClientCache, prApproverGhClientCache))
mux.HandleFunc("/webhook", handleWebhook(githubWebhookSecret, mainGhClientCache, prApproverGhClientCache))
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/live", health.NewHandler(livenessChecker))
mux.Handle("/ready", health.NewHandler(readinessChecker))
Expand Down

0 comments on commit ab0adee

Please sign in to comment.