github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/ratelimiter/options/acquire.go (about) 1 package options 2 3 import "time" 4 5 const ( 6 DefaultDecrease = 100 * time.Millisecond 7 ) 8 9 type AcquireType uint8 10 11 const ( 12 AcquireTypeAcquire = AcquireType(iota) 13 AcquireTypeReport 14 15 AcquireTypeDefault = AcquireTypeAcquire 16 ) 17 18 type Acquire interface { 19 // Type defines type of acquire request 20 Type() AcquireType 21 22 // OperationTimeout defines operation Timeout for acquire request 23 OperationTimeout() time.Duration 24 25 // OperationCancelAfter defines operation CancelAfter for acquire request 26 OperationCancelAfter() time.Duration 27 } 28 29 type acquireOptionsHolder struct { 30 acquireType AcquireType 31 operationTimeout time.Duration 32 operationCancelAfter time.Duration 33 } 34 35 func (h *acquireOptionsHolder) OperationTimeout() time.Duration { 36 return h.operationTimeout 37 } 38 39 func (h *acquireOptionsHolder) OperationCancelAfter() time.Duration { 40 return h.operationTimeout 41 } 42 43 func (h *acquireOptionsHolder) Type() AcquireType { 44 return h.acquireType 45 } 46 47 type AcquireOption func(h *acquireOptionsHolder) 48 49 func WithAcquire() AcquireOption { 50 return func(h *acquireOptionsHolder) { 51 h.acquireType = AcquireTypeAcquire 52 } 53 } 54 55 func WithReport() AcquireOption { 56 return func(h *acquireOptionsHolder) { 57 h.acquireType = AcquireTypeReport 58 } 59 } 60 61 func WithOperationTimeout(operationTimeout time.Duration) AcquireOption { 62 return func(h *acquireOptionsHolder) { 63 h.operationTimeout = operationTimeout 64 } 65 } 66 67 func WithOperationCancelAfter(operationCancelAfter time.Duration) AcquireOption { 68 return func(h *acquireOptionsHolder) { 69 h.operationCancelAfter = operationCancelAfter 70 } 71 } 72 73 func NewAcquire(opts ...AcquireOption) Acquire { 74 h := &acquireOptionsHolder{ 75 acquireType: AcquireTypeDefault, 76 } 77 for _, o := range opts { 78 if o != nil { 79 o(h) 80 } 81 } 82 83 return h 84 }