github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
     7  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     8  )
     9  
    10  var defaultRetryBudget = budget.Limited(-1)
    11  
    12  type Common struct {
    13  	operationTimeout     time.Duration
    14  	operationCancelAfter time.Duration
    15  	disableAutoRetry     bool
    16  	traceRetry           trace.Retry
    17  	retryBudget          budget.Budget
    18  
    19  	panicCallback func(e interface{})
    20  }
    21  
    22  // AutoRetry defines auto-retry flag
    23  func (c *Common) AutoRetry() bool {
    24  	return !c.disableAutoRetry
    25  }
    26  
    27  // PanicCallback returns user-defined panic callback
    28  // If nil - panic callback not defined
    29  func (c *Common) PanicCallback() func(e interface{}) {
    30  	return c.panicCallback
    31  }
    32  
    33  // OperationTimeout is the maximum amount of time a YDB server will process
    34  // an operation. After timeout exceeds YDB will try to cancel operation and
    35  // regardless of the cancellation appropriate error will be returned to
    36  // the client.
    37  // If OperationTimeout is zero then no timeout is used.
    38  func (c *Common) OperationTimeout() time.Duration {
    39  	return c.operationTimeout
    40  }
    41  
    42  // OperationCancelAfter is the maximum amount of time a YDB server will process an
    43  // operation. After timeout exceeds YDB will try to cancel operation and if
    44  // it succeeds appropriate error will be returned to the client; otherwise
    45  // processing will be continued.
    46  // If OperationCancelAfter is zero then no timeout is used.
    47  func (c *Common) OperationCancelAfter() time.Duration {
    48  	return c.operationCancelAfter
    49  }
    50  
    51  func (c *Common) TraceRetry() *trace.Retry {
    52  	return &c.traceRetry
    53  }
    54  
    55  func (c *Common) RetryBudget() budget.Budget {
    56  	if c.retryBudget == nil {
    57  		return defaultRetryBudget
    58  	}
    59  
    60  	return c.retryBudget
    61  }
    62  
    63  // SetOperationTimeout define the maximum amount of time a YDB server will process
    64  // an operation. After timeout exceeds YDB will try to cancel operation and
    65  // regardless of the cancellation appropriate error will be returned to
    66  // the client.
    67  //
    68  // If OperationTimeout is zero then no timeout is used.
    69  func SetOperationTimeout(c *Common, operationTimeout time.Duration) {
    70  	c.operationTimeout = operationTimeout
    71  }
    72  
    73  // SetOperationCancelAfter set the maximum amount of time a YDB server will process an
    74  // operation. After timeout exceeds YDB will try to cancel operation and if
    75  // it succeeds appropriate error will be returned to the client; otherwise
    76  // processing will be continued.
    77  //
    78  // If OperationCancelAfter is zero then no timeout is used.
    79  func SetOperationCancelAfter(c *Common, operationCancelAfter time.Duration) {
    80  	c.operationCancelAfter = operationCancelAfter
    81  }
    82  
    83  // SetPanicCallback applies panic callback to config
    84  func SetPanicCallback(c *Common, panicCallback func(e interface{})) {
    85  	c.panicCallback = panicCallback
    86  }
    87  
    88  // SetAutoRetry affects on AutoRetry() flag
    89  func SetAutoRetry(c *Common, autoRetry bool) {
    90  	c.disableAutoRetry = !autoRetry
    91  }
    92  
    93  func SetTraceRetry(c *Common, t *trace.Retry, opts ...trace.RetryComposeOption) {
    94  	c.traceRetry = *c.traceRetry.Compose(t, opts...)
    95  }
    96  
    97  func SetRetryBudget(c *Common, b budget.Budget) {
    98  	c.retryBudget = b
    99  }