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

     1  /*
     2   *
     3   * Copyright 2017 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 manual defines a resolver that can be used to manually send resolved
    20  // addresses to ClientConn.
    21  package manual
    22  
    23  import (
    24  	"gitee.com/ks-custle/core-gm/grpc/resolver"
    25  )
    26  
    27  // NewBuilderWithScheme creates a new test resolver builder with the given scheme.
    28  func NewBuilderWithScheme(scheme string) *Resolver {
    29  	return &Resolver{
    30  		BuildCallback:      func(resolver.Target, resolver.ClientConn, resolver.BuildOptions) {},
    31  		ResolveNowCallback: func(resolver.ResolveNowOptions) {},
    32  		CloseCallback:      func() {},
    33  		scheme:             scheme,
    34  	}
    35  }
    36  
    37  // Resolver is also a resolver builder.
    38  // It's build() function always returns itself.
    39  type Resolver struct {
    40  	// BuildCallback is called when the Build method is called.  Must not be
    41  	// nil.  Must not be changed after the resolver may be built.
    42  	BuildCallback func(resolver.Target, resolver.ClientConn, resolver.BuildOptions)
    43  	// ResolveNowCallback is called when the ResolveNow method is called on the
    44  	// resolver.  Must not be nil.  Must not be changed after the resolver may
    45  	// be built.
    46  	ResolveNowCallback func(resolver.ResolveNowOptions)
    47  	// CloseCallback is called when the Close method is called.  Must not be
    48  	// nil.  Must not be changed after the resolver may be built.
    49  	CloseCallback func()
    50  	scheme        string
    51  
    52  	// Fields actually belong to the resolver.
    53  	CC             resolver.ClientConn
    54  	bootstrapState *resolver.State
    55  }
    56  
    57  // InitialState adds initial state to the resolver so that UpdateState doesn't
    58  // need to be explicitly called after Dial.
    59  func (r *Resolver) InitialState(s resolver.State) {
    60  	r.bootstrapState = &s
    61  }
    62  
    63  // Build returns itself for Resolver, because it's both a builder and a resolver.
    64  func (r *Resolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
    65  	r.BuildCallback(target, cc, opts)
    66  	r.CC = cc
    67  	if r.bootstrapState != nil {
    68  		r.UpdateState(*r.bootstrapState)
    69  	}
    70  	return r, nil
    71  }
    72  
    73  // Scheme returns the test scheme.
    74  func (r *Resolver) Scheme() string {
    75  	return r.scheme
    76  }
    77  
    78  // ResolveNow is a noop for Resolver.
    79  func (r *Resolver) ResolveNow(o resolver.ResolveNowOptions) {
    80  	r.ResolveNowCallback(o)
    81  }
    82  
    83  // Close is a noop for Resolver.
    84  func (r *Resolver) Close() {
    85  	r.CloseCallback()
    86  }
    87  
    88  // UpdateState calls CC.UpdateState.
    89  func (r *Resolver) UpdateState(s resolver.State) {
    90  	r.CC.UpdateState(s)
    91  }
    92  
    93  // ReportError calls CC.ReportError.
    94  func (r *Resolver) ReportError(err error) {
    95  	r.CC.ReportError(err)
    96  }