github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/quotapool/config.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package quotapool
    12  
    13  import (
    14  	"context"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/log"
    18  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    19  )
    20  
    21  // Option is used to configure a QuotaPool.
    22  type Option interface {
    23  	apply(*config)
    24  }
    25  
    26  // AcquisitionFunc is used to configure a quotapool to call a function after
    27  // an acquisition has occurred.
    28  type AcquisitionFunc func(
    29  	ctx context.Context, poolName string, r Request, start time.Time,
    30  )
    31  
    32  // OnAcquisition creates an Option to configure a callback upon acquisition.
    33  // It is often useful for recording metrics.
    34  func OnAcquisition(f AcquisitionFunc) Option {
    35  	return optionFunc(func(cfg *config) {
    36  		cfg.onAcquisition = f
    37  	})
    38  }
    39  
    40  // OnSlowAcquisition creates an Option to configure a callback upon slow
    41  // acquisitions. Only one OnSlowAcquisition may be used. If multiple are
    42  // specified only the last will be used.
    43  func OnSlowAcquisition(threshold time.Duration, f SlowAcquisitionFunc) Option {
    44  	return optionFunc(func(cfg *config) {
    45  		cfg.slowAcquisitionThreshold = threshold
    46  		cfg.onSlowAcquisition = f
    47  	})
    48  }
    49  
    50  // LogSlowAcquisition is a SlowAcquisitionFunc.
    51  func LogSlowAcquisition(ctx context.Context, poolName string, r Request, start time.Time) func() {
    52  	log.Warningf(ctx, "have been waiting %s attempting to acquire %s quota",
    53  		timeutil.Since(start), poolName)
    54  	return func() {
    55  		log.Infof(ctx, "acquired %s quota after %s",
    56  			poolName, timeutil.Since(start))
    57  	}
    58  }
    59  
    60  // SlowAcquisitionFunc is used to configure a quotapool to call a function when
    61  // quota acquisition is slow. The returned callback is called when the
    62  // acquisition occurs.
    63  type SlowAcquisitionFunc func(
    64  	ctx context.Context, poolName string, r Request, start time.Time,
    65  ) (onAcquire func())
    66  
    67  type optionFunc func(cfg *config)
    68  
    69  func (f optionFunc) apply(cfg *config) { f(cfg) }
    70  
    71  type config struct {
    72  	onAcquisition            AcquisitionFunc
    73  	onSlowAcquisition        SlowAcquisitionFunc
    74  	slowAcquisitionThreshold time.Duration
    75  }