gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/resolver/unix/unix.go (about)

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * 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  // Package unix implements a resolver for unix targets.
    20  package unix
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"gitee.com/ks-custle/core-gm/grpc/internal/transport/networktype"
    26  	"gitee.com/ks-custle/core-gm/grpc/resolver"
    27  )
    28  
    29  const unixScheme = "unix"
    30  const unixAbstractScheme = "unix-abstract"
    31  
    32  type builder struct {
    33  	scheme string
    34  }
    35  
    36  func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
    37  	// target.Authority is deprecated, use target.GetAuthority() instead.
    38  	//if target.Authority != "" {
    39  	if target.GetAuthority() != "" {
    40  		//return nil, fmt.Errorf("invalid (non-empty) authority: %v", target.Authority)
    41  		return nil, fmt.Errorf("invalid (non-empty) authority: %v", target.GetAuthority())
    42  	}
    43  
    44  	// gRPC was parsing the dial target manually before PR #4817, and we
    45  	// switched to using url.Parse() in that PR. To avoid breaking existing
    46  	// resolver implementations we ended up stripping the leading "/" from the
    47  	// endpoint. This obviously does not work for the "unix" scheme. Hence we
    48  	// end up using the parsed URL instead.
    49  	endpoint := target.URL.Path
    50  	if endpoint == "" {
    51  		endpoint = target.URL.Opaque
    52  	}
    53  	addr := resolver.Address{Addr: endpoint}
    54  	if b.scheme == unixAbstractScheme {
    55  		// prepend "\x00" to address for unix-abstract
    56  		addr.Addr = "\x00" + addr.Addr
    57  	}
    58  	_ = cc.UpdateState(resolver.State{Addresses: []resolver.Address{networktype.Set(addr, "unix")}})
    59  	return &nopResolver{}, nil
    60  }
    61  
    62  func (b *builder) Scheme() string {
    63  	return b.scheme
    64  }
    65  
    66  type nopResolver struct {
    67  }
    68  
    69  func (*nopResolver) ResolveNow(resolver.ResolveNowOptions) {}
    70  
    71  func (*nopResolver) Close() {}
    72  
    73  func init() {
    74  	resolver.Register(&builder{scheme: unixScheme})
    75  	resolver.Register(&builder{scheme: unixAbstractScheme})
    76  }