I'm trying to write a small http downloader package which can allow to control tcp socket options while creating, and i really stuck with possibility of definition socket options. As far as I understand, in the version 1.11 in package net struct Dialer was added interface Control which should allow to define such options, but i cannot find any valid example regarding usage in the context i'm trying to implement.
dialer := &net.Dialer{
Timeout: 5 * time.Second,
Deadline: time.Time{},
LocalAddr: nil,
DualStack: false,
FallbackDelay: 0,
KeepAlive: 0,
Resolver: nil,
Control: controlOnConnectionSetup,
}
// set client
client := http.Client{
Transport: &http.Transport{
Proxy: nil,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, "tcp", addr)
},
Dial: nil,
DialTLS: nil,
TLSClientConfig: nil,
TLSHandshakeTimeout: 0,
DisableKeepAlives: false,
DisableCompression: false,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
MaxConnsPerHost: 0,
IdleConnTimeout: 0,
ResponseHeaderTimeout: 0,
ExpectContinueTimeout: 0,
TLSNextProto: nil,
ProxyConnectHeader: nil,
MaxResponseHeaderBytes: 0,
},
CheckRedirect: nil,
Jar: nil,
Timeout: 3,
}
And the code of cotrol function (which is not working, it's just copy from example where i cannot find any mentions of fd variable):
func controlOnConnectionSetup(network string, address string, c syscall.RawConn) error {
var operr error
fn := func(s uintptr) {
handle := syscall.Handle(fd)
operr = syscall.SetsockoptInt(handle, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
if operr != nil {
return
}
}
if err := c.Control(fn); err != nil {
return err
}
if operr != nil {
return operr
}
return nil
}
I would be really appreciated if anyone can tell me what am i doing wrong or at least provide example on how to control socket parameters in tcp connection.