add unix socket support

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-09-05 12:33:01 +02:00
parent 23370fd04d
commit b230278441
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF
3 changed files with 16 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea .idea
.bin .bin
/tmp

View File

@ -43,11 +43,16 @@ func New(opts ...Option) (Client, error) {
if len(c.opts.streamInterceptors) > 0 { if len(c.opts.streamInterceptors) > 0 {
c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(c.opts.streamInterceptors...))) c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(c.opts.streamInterceptors...)))
} }
if c.opts.addr == "" { switch {
case c.opts.addr == "":
c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name) c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name)
} else if strings.HasPrefix(c.opts.addr, "tcp://") { case strings.HasPrefix(c.opts.addr, "tcp://"):
c.addr = strings.Replace(c.opts.addr, "tcp://", "", 1) c.addr = strings.Replace(c.opts.addr, "tcp://", "", 1)
} else { case strings.HasPrefix(c.opts.addr, "unix:///"):
c.addr = c.opts.addr
case strings.HasPrefix(c.opts.addr, "unix://"):
c.addr = strings.Replace(c.opts.addr, "unix://", "unix:", 1)
default:
c.addr = c.opts.addr c.addr = c.opts.addr
} }
if c.opts.version != "" && c.opts.addr == "" { if c.opts.version != "" && c.opts.addr == "" {

View File

@ -145,7 +145,13 @@ func (s *service) run() error {
return err return err
} }
lis, err := net.Listen("tcp", s.opts.address) network := "tcp"
if strings.HasPrefix(s.opts.address, "unix://") {
network = "unix"
s.opts.address = strings.TrimPrefix(s.opts.address, "unix://")
}
lis, err := net.Listen(network, s.opts.address)
if err != nil { if err != nil {
return err return err
} }