...

Source file src/github.com/lestrrat-go/backoff/v2/null.go

Documentation: github.com/lestrrat-go/backoff/v2

     1  package backoff
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  )
     7  
     8  // NullPolicy does not do any backoff. It allows the caller
     9  // to execute the desired code once, and no more
    10  type NullPolicy struct{}
    11  
    12  func NewNull() *NullPolicy {
    13  	return &NullPolicy{}
    14  }
    15  
    16  func (p *NullPolicy) Start(ctx context.Context) Controller {
    17  	return newNullController(ctx)
    18  }
    19  
    20  type nullController struct {
    21  	mu   *sync.RWMutex
    22  	ctx  context.Context
    23  	next chan struct{}
    24  }
    25  
    26  func newNullController(ctx context.Context) *nullController {
    27  	cctx, cancel := context.WithCancel(ctx)
    28  	c := &nullController{
    29  		mu:   &sync.RWMutex{},
    30  		ctx:  cctx,
    31  		next: make(chan struct{}), // NO BUFFER
    32  	}
    33  	go func(ch chan struct{}, cancel func()) {
    34  		ch <- struct{}{}
    35  		close(ch)
    36  		cancel()
    37  	}(c.next, cancel)
    38  	return c
    39  }
    40  
    41  func (c *nullController) Done() <-chan struct{} {
    42  	c.mu.RLock()
    43  	defer c.mu.RUnlock()
    44  	return c.ctx.Done()
    45  }
    46  
    47  func (c *nullController) Next() <-chan struct{} {
    48  	c.mu.RLock()
    49  	defer c.mu.RUnlock()
    50  	return c.next
    51  }
    52  

View as plain text