dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/resolver/unix/unix.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  /*
    19   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  // Package unix implements a resolver for unix targets.
    25  package unix
    26  
    27  import (
    28  	"fmt"
    29  )
    30  
    31  import (
    32  	"google.golang.org/grpc/resolver"
    33  )
    34  
    35  import (
    36  	"dubbo.apache.org/dubbo-go/v3/xds/utils/transport/networktype"
    37  )
    38  
    39  const unixScheme = "unix"
    40  const unixAbstractScheme = "unix-abstract"
    41  
    42  type builder struct {
    43  	scheme string
    44  }
    45  
    46  func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
    47  	if target.Authority != "" {
    48  		return nil, fmt.Errorf("invalid (non-empty) authority: %v", target.Authority)
    49  	}
    50  
    51  	// gRPC was parsing the dial target manually before PR #4817, and we
    52  	// switched to using url.Parse() in that PR. To avoid breaking existing
    53  	// resolver implementations we ended up stripping the leading "/" from the
    54  	// endpoint. This obviously does not work for the "unix" scheme. Hence we
    55  	// end up using the parsed URL instead.
    56  	endpoint := target.URL.Path
    57  	if endpoint == "" {
    58  		endpoint = target.URL.Opaque
    59  	}
    60  	addr := resolver.Address{Addr: endpoint}
    61  	if b.scheme == unixAbstractScheme {
    62  		// prepend "\x00" to address for unix-abstract
    63  		addr.Addr = "\x00" + addr.Addr
    64  	}
    65  	cc.UpdateState(resolver.State{Addresses: []resolver.Address{networktype.Set(addr, "unix")}})
    66  	return &nopResolver{}, nil
    67  }
    68  
    69  func (b *builder) Scheme() string {
    70  	return b.scheme
    71  }
    72  
    73  type nopResolver struct {
    74  }
    75  
    76  func (*nopResolver) ResolveNow(resolver.ResolveNowOptions) {}
    77  
    78  func (*nopResolver) Close() {}
    79  
    80  func init() {
    81  	resolver.Register(&builder{scheme: unixScheme})
    82  	resolver.Register(&builder{scheme: unixAbstractScheme})
    83  }