cloudwego / hertz

Go HTTP framework with high-performance and strong-extensibility for building micro-services.
https://www.cloudwego.io
Apache License 2.0
5.22k stars 508 forks source link

how to create a client connection to unix domain socket? #1195

Open sprappcom opened 1 week ago

sprappcom commented 1 week ago

Describe the Question want to have a hertz golang client to unix domain socket calling.

Reproducible Code

something like... ? client.WithNetwork("unix"), // <- here doesnt work like this. my backendAddr is "unix:///home/ubuntu/web.sock"

    newPool = &sync.Pool{
            New: func() interface{} {
                    cli, err := client.NewClient(
                            client.WithDialTimeout(1*time.Second),
                            client.WithMaxConnsPerHost(10240),
                            client.WithMaxIdleConnDuration(30*time.Second),
                            client.WithMaxConnDuration(0),
                            client.WithMaxConnWaitTimeout(10*time.Second),
                            client.WithKeepAlive(true),
                            client.WithNetwork("unix"), // <- here doesnt work like this. my backendAddr is "unix:///home/ubuntu/web.sock"
                            client.WithClientReadTimeout(60*time.Second),
                            client.WithWriteTimeout(60*time.Second),
                            client.WithDialer(standard.NewDialer()),
                            client.WithTLSConfig(&tls.Config{InsecureSkipVerify: true}),
                            client.WithHostClientConfigHook(func(hc interface{}) error {
                                    if hct, ok := hc.(*http1.HostClient); ok {
                                            hct.Addr = backendAddr
                                    }
                                    return nil
                            }),
                    )
                    if err != nil {
                            log.Fatalf("failed to create client for %s: %v", backendAddr, err)
                    }
                    return cli
            },
    }

Expected behavior

client unix domain socket backend calling to unix domain socket connection

Screenshots

Hertz version:

0.9.3

Environment:

go env GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='/root/.cache/go-build' GOENV='/root/.config/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='amd64' GOHOSTOS='linux' GOINSECURE='*' GOMODCACHE='/root/go/pkg/mod' GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64' GOVCS='' GOVERSION='go1.23.1' GODEBUG=''

Additional context there's no information regarding this in doc. previously mentioned here: https://github.com/cloudwego/hertz/issues/1194

sprappcom commented 1 week ago

@li-jin-gou @Duslia any help?

FGYFFFF commented 4 days ago

refer: https://github.com/cloudwego/hertz/blob/develop/pkg/app/client/client_test.go#L133