github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/trace/retry_gtrace.go (about)

     1  // Code generated by gtrace. DO NOT EDIT.
     2  
     3  package trace
     4  
     5  import (
     6  	"context"
     7  )
     8  
     9  // retryComposeOptions is a holder of options
    10  type retryComposeOptions struct {
    11  	panicCallback func(e interface{})
    12  }
    13  
    14  // RetryOption specified Retry compose option
    15  // Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
    16  type RetryComposeOption func(o *retryComposeOptions)
    17  
    18  // WithRetryPanicCallback specified behavior on panic
    19  // Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
    20  func WithRetryPanicCallback(cb func(e interface{})) RetryComposeOption {
    21  	return func(o *retryComposeOptions) {
    22  		o.panicCallback = cb
    23  	}
    24  }
    25  
    26  // Compose returns a new Retry which has functional fields composed both from t and x.
    27  // Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
    28  func (t *Retry) Compose(x *Retry, opts ...RetryComposeOption) *Retry {
    29  	if t == nil {
    30  		return x
    31  	}
    32  	var ret Retry
    33  	options := retryComposeOptions{}
    34  	for _, opt := range opts {
    35  		if opt != nil {
    36  			opt(&options)
    37  		}
    38  	}
    39  	{
    40  		h1 := t.OnRetry
    41  		h2 := x.OnRetry
    42  		ret.OnRetry = func(r RetryLoopStartInfo) func(RetryLoopDoneInfo) {
    43  			if options.panicCallback != nil {
    44  				defer func() {
    45  					if e := recover(); e != nil {
    46  						options.panicCallback(e)
    47  					}
    48  				}()
    49  			}
    50  			var r1, r2 func(RetryLoopDoneInfo)
    51  			if h1 != nil {
    52  				r1 = h1(r)
    53  			}
    54  			if h2 != nil {
    55  				r2 = h2(r)
    56  			}
    57  			return func(r RetryLoopDoneInfo) {
    58  				if options.panicCallback != nil {
    59  					defer func() {
    60  						if e := recover(); e != nil {
    61  							options.panicCallback(e)
    62  						}
    63  					}()
    64  				}
    65  				if r1 != nil {
    66  					r1(r)
    67  				}
    68  				if r2 != nil {
    69  					r2(r)
    70  				}
    71  			}
    72  		}
    73  	}
    74  	return &ret
    75  }
    76  func (t *Retry) onRetry(r RetryLoopStartInfo) func(RetryLoopDoneInfo) {
    77  	fn := t.OnRetry
    78  	if fn == nil {
    79  		return func(RetryLoopDoneInfo) {
    80  			return
    81  		}
    82  	}
    83  	res := fn(r)
    84  	if res == nil {
    85  		return func(RetryLoopDoneInfo) {
    86  			return
    87  		}
    88  	}
    89  	return res
    90  }
    91  // Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
    92  func RetryOnRetry(t *Retry, c *context.Context, call call, label string, idempotent bool, nestedCall bool) func(attempts int, _ error) {
    93  	var p RetryLoopStartInfo
    94  	p.Context = c
    95  	p.Call = call
    96  	p.Label = label
    97  	p.Idempotent = idempotent
    98  	p.NestedCall = nestedCall
    99  	res := t.onRetry(p)
   100  	return func(attempts int, e error) {
   101  		var p RetryLoopDoneInfo
   102  		p.Attempts = attempts
   103  		p.Error = e
   104  		res(p)
   105  	}
   106  }