github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/balancer/priority/ignore_resolve_now.go (about)

     1  /*
     2   *
     3   * Copyright 2021 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 priority
    20  
    21  import (
    22  	"sync/atomic"
    23  
    24  	"github.com/hxx258456/ccgo/grpc/balancer"
    25  	"github.com/hxx258456/ccgo/grpc/resolver"
    26  )
    27  
    28  type ignoreResolveNowBalancerBuilder struct {
    29  	balancer.Builder
    30  	ignoreResolveNow *uint32
    31  }
    32  
    33  // If `ignore` is true, all `ResolveNow()` from the balancer built from this
    34  // builder will be ignored.
    35  //
    36  // `ignore` can be updated later by `updateIgnoreResolveNow`, and the update
    37  // will be propagated to all the old and new balancers built with this.
    38  func newIgnoreResolveNowBalancerBuilder(bb balancer.Builder, ignore bool) *ignoreResolveNowBalancerBuilder {
    39  	ret := &ignoreResolveNowBalancerBuilder{
    40  		Builder:          bb,
    41  		ignoreResolveNow: new(uint32),
    42  	}
    43  	ret.updateIgnoreResolveNow(ignore)
    44  	return ret
    45  }
    46  
    47  func (irnbb *ignoreResolveNowBalancerBuilder) updateIgnoreResolveNow(b bool) {
    48  	if b {
    49  		atomic.StoreUint32(irnbb.ignoreResolveNow, 1)
    50  		return
    51  	}
    52  	atomic.StoreUint32(irnbb.ignoreResolveNow, 0)
    53  
    54  }
    55  
    56  func (irnbb *ignoreResolveNowBalancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
    57  	return irnbb.Builder.Build(&ignoreResolveNowClientConn{
    58  		ClientConn:       cc,
    59  		ignoreResolveNow: irnbb.ignoreResolveNow,
    60  	}, opts)
    61  }
    62  
    63  type ignoreResolveNowClientConn struct {
    64  	balancer.ClientConn
    65  	ignoreResolveNow *uint32
    66  }
    67  
    68  func (i ignoreResolveNowClientConn) ResolveNow(o resolver.ResolveNowOptions) {
    69  	if atomic.LoadUint32(i.ignoreResolveNow) != 0 {
    70  		return
    71  	}
    72  	i.ClientConn.ResolveNow(o)
    73  }