Skip to content

Commit

Permalink
set SO_LINGER=0 to tcp connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sniper91 committed Jul 18, 2024
1 parent 909dd33 commit 3513b0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 15 additions & 2 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,27 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
}
}
}
client, err := pconfig.NewClientFromConfig(httpClientConfig, "http_probe", pconfig.WithKeepAlivesDisabled())
dialWithLinger := func(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{}
conn, err := d.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
tcpConn, ok := conn.(*net.TCPConn)
if ok {
tcpConn.SetLinger(0)
return tcpConn, nil
}
return conn, nil
}
client, err := pconfig.NewClientFromConfig(httpClientConfig, "http_probe", pconfig.WithKeepAlivesDisabled(), pconfig.WithDialContextFunc(dialWithLinger))
if err != nil {
level.Error(logger).Log("msg", "Error generating HTTP client", "err", err)
return false
}

httpClientConfig.TLSConfig.ServerName = ""
noServerName, err := pconfig.NewRoundTripperFromConfig(httpClientConfig, "http_probe", pconfig.WithKeepAlivesDisabled())
noServerName, err := pconfig.NewRoundTripperFromConfig(httpClientConfig, "http_probe", pconfig.WithKeepAlivesDisabled(), pconfig.WithDialContextFunc(dialWithLinger))
if err != nil {
level.Error(logger).Log("msg", "Error generating HTTP client without ServerName", "err", err)
return false
Expand Down
3 changes: 3 additions & 0 deletions prober/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry
level.Error(logger).Log("msg", "Error dialing TCP", "err", err)
return false
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetLinger(0)
}
defer conn.Close()
level.Info(logger).Log("msg", "Successfully dialed")

Expand Down

0 comments on commit 3513b0f

Please sign in to comment.