dyweb / gommon

A collection of common util libraries for Go
MIT License
27 stars 6 forks source link

[util][httputil] Unix domain socket #93

Closed at15 closed 5 years ago

at15 commented 6 years ago

unix client

func NewPooledUnixTransport(sockFile string) *http.Transport {
    return &http.Transport{
        DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
            d := net.Dialer{
                Timeout:   30 * time.Second,
                KeepAlive: 30 * time.Second,
                DualStack: true,
            }
            return d.DialContext(ctx, "unix", sockFile)
        },
        MaxIdleConns:          100,
        IdleConnTimeout:       90 * time.Second,
        TLSHandshakeTimeout:   10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    }
}

unix server

func ListenAndServeUnix(srv *http.Server, addr string) error {
    ln, err := net.Listen("unix", addr)
    if err != nil {
        return err
    }
    // TODO: do we need tcpKeepAliveListener like the default tcp ListenAndServe?
    return srv.Serve(ln)
}
at15 commented 5 years ago

the test is causing race problem in go tip, not sure if the race detector has improved or something else

see https://travis-ci.org/dyweb/gommon/jobs/459629689

at15 commented 5 years ago

fixed in #90