vitess.io/vitess@v0.16.2/go/pools/rpc_pool.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package pools
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"vitess.io/vitess/go/vt/vterrors"
    24  
    25  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    26  )
    27  
    28  // RPCPool is a specialized version of the ResourcePool, for bounding concurrent
    29  // access to making RPC calls.
    30  //
    31  // Whether you use this, or a sync2.Semaphore to gate RPCs (or shared access to
    32  // any shared resource) depends on what timeout semantics you need. A sync2.Semaphore
    33  // takes a global timeout, which applies to calls to Acquire(); if you need to
    34  // respect a context's deadline, you can call AcquireContext(context.Context),
    35  // but that ignores the global timeout. Conversely, an RPCPool provides only
    36  // one method of acquisition, Acquire(context.Context), which always uses the
    37  // lower of the pool-global timeout or the context deadline.
    38  type RPCPool struct {
    39  	rp          IResourcePool
    40  	waitTimeout time.Duration
    41  }
    42  
    43  // NewRPCPool returns an RPCPool with the given size and wait timeout. A zero
    44  // timeout will be ignored on calls to Acquire, and only the context deadline
    45  // will be used. If a logWait function is provided, it will be called whenever
    46  // a call to Acquire has to wait for a resource, but only after a successful
    47  // wait (meaning if we hit a timeout before a resource becomes available, it
    48  // will not be called).
    49  func NewRPCPool(size int, waitTimeout time.Duration, logWait func(time.Time)) *RPCPool {
    50  	return &RPCPool{
    51  		rp:          NewResourcePool(rpcResourceFactory, size, size, 0, 0, logWait, nil, 0),
    52  		waitTimeout: waitTimeout,
    53  	}
    54  }
    55  
    56  // Acquire acquires one slot in the RPCPool. If a slot is not immediately
    57  // available, it will block until one becomes available or until a timeout is
    58  // reached. The lower of the context deadline and the pool's waitTimeout will
    59  // be used as the timeout, except when the pool's waitTimeout is zero, in which
    60  // case the context deadline will always serve as the overall timeout.
    61  //
    62  // It returns nil on successful acquisition, and an error if a timeout occurred
    63  // before a slot became available.
    64  //
    65  // Note: For every successful call to Acquire, the caller must make a
    66  // corresponding call to Release.
    67  func (pool *RPCPool) Acquire(ctx context.Context) error {
    68  	if pool.waitTimeout > 0 {
    69  		var cancel context.CancelFunc
    70  		ctx, cancel = context.WithTimeout(ctx, pool.waitTimeout)
    71  		defer cancel()
    72  	}
    73  
    74  	_, err := pool.rp.Get(ctx, nil)
    75  	return err
    76  }
    77  
    78  // Release frees a slot in the pool. It must only be called after a successful
    79  // call to Acquire.
    80  func (pool *RPCPool) Release() { pool.rp.Put(rpc) }
    81  
    82  // Close empties the pool, preventing further Acquire calls from succeeding.
    83  // It waits for all slots to be freed via Release.
    84  func (pool *RPCPool) Close() { pool.rp.Close() }
    85  
    86  func (pool *RPCPool) StatsJSON() string { return pool.rp.StatsJSON() }
    87  
    88  type _rpc struct{}
    89  
    90  var rpc = &_rpc{}
    91  
    92  // Close implements Resource for _rpc.
    93  func (*_rpc) Close() {}
    94  
    95  // ApplySetting implements Resource for _rpc.
    96  func (r *_rpc) ApplySetting(context.Context, *Setting) error {
    97  	// should be unreachable
    98  	return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG]: _rpc does not support ApplySetting")
    99  }
   100  
   101  func (r *_rpc) IsSettingApplied() bool {
   102  	return false
   103  }
   104  
   105  func (r *_rpc) IsSameSetting(string) bool {
   106  	return true
   107  }
   108  
   109  func (r *_rpc) ResetSetting(context.Context) error {
   110  	// should be unreachable
   111  	return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG]: _rpc does not support ResetSetting")
   112  }
   113  
   114  func (r *_rpc) Expired(time.Duration) bool {
   115  	return false
   116  }
   117  
   118  // we only ever return the same rpc pointer. it's used as a sentinel and is
   119  // only used internally so using the same one over and over doesn't matter.
   120  func rpcResourceFactory(ctx context.Context) (Resource, error) { return rpc, nil }