Configure Go HTTP Client Timeouts for Monitoring Pings

Go

The Problem

Go's default HTTP client has no timeout. A monitoring ping can hang indefinitely, blocking your cron job from completing.

The Solution

Always create an http.Client with an explicit Timeout. Wrap the ping call in error handling so it never blocks your main logic.

The Default Client Trap

Go's http.DefaultClient has zero timeout — it will wait forever for a response. In a cron job, this means a DNS issue or slow network can hang your entire process.

Best Practice

Create a dedicated client with a 10-second timeout. Use a helper function for pings so monitoring never interferes with your core logic.

Code Examples

Robust monitoring helper

Go
package monitor

import (
	"net/http"
	"time"
)

var client = &http.Client{Timeout: 10 * time.Second}

func Ping(url string) {
	resp, err := client.Get(url)
	if err != nil {
		return // Don't let monitoring break the job
	}
	resp.Body.Close()
}

Ready to Monitor Your Cron Jobs?

Create your free account and add your first monitor in under 5 minutes. No credit card required.