prometheus-community / pro-bing

A library for creating continuous probers
MIT License
309 stars 52 forks source link

Add resolve timeout #56

Closed iFurySt closed 5 months ago

iFurySt commented 1 year ago

This is an example code where the program will block for approximately 30 seconds. This behavior can be counterintuitive in some cases because the Timeout is set but fails to stop the ongoing resolving process. However, when using the Ping command, the ongoing resolution process is terminated based on the specified timeout (-t).

package main

import (
    "fmt"
    ping "github.com/prometheus-community/pro-bing"
    "time"
)

func main() {
    start := time.Now()
    defer func() {
        fmt.Println(time.Since(start))
        // 30.003322236s
        // panic: lookup 8.8.8.8 : no such host
    }()
    pinger, err := ping.NewPinger("8.8.8.8 ")
    if err != nil {
        panic(err)
    }
    pinger.Count = 3
    pinger.Timeout = 1 * time.Second
    pinger.RecordRtts = false
    err = pinger.Run() // Blocks until finished.
    if err != nil {
        panic(err)
    }
}

To address this issue, I have added the ResolveTimeout, which handles situations where the resolve time exceeds a certain threshold. While expanding the Timeout to influence the resolve time might be a potential solution, it could have backward compatibility implications? Based on this, I have opted to submit the ResolveTimeout version.

SuperQ commented 1 year ago

This needs a DCO sign-off. You can use git commit -s --amend to add it.

iFurySt commented 1 year ago

I think that adding context to the parameters of Run may be a better way to support overall timeout control.