File: null.go

package info (click to toggle)
golang-github-lestrrat-go-backoff 2.0.8-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package backoff

import (
	"context"
	"sync"
)

// NullPolicy does not do any backoff. It allows the caller
// to execute the desired code once, and no more
type NullPolicy struct{}

func NewNull() *NullPolicy {
	return &NullPolicy{}
}

func (p *NullPolicy) Start(ctx context.Context) Controller {
	return newNullController(ctx)
}

type nullController struct {
	mu   *sync.RWMutex
	ctx  context.Context
	next chan struct{}
}

func newNullController(ctx context.Context) *nullController {
	cctx, cancel := context.WithCancel(ctx)
	c := &nullController{
		mu:   &sync.RWMutex{},
		ctx:  cctx,
		next: make(chan struct{}), // NO BUFFER
	}
	go func(ch chan struct{}, cancel func()) {
		ch <- struct{}{}
		close(ch)
		cancel()
	}(c.next, cancel)
	return c
}

func (c *nullController) Done() <-chan struct{} {
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.ctx.Done()
}

func (c *nullController) Next() <-chan struct{} {
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.next
}