File: poller.go

package info (click to toggle)
golang-github-cli-oauth 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 168 kB
  • sloc: makefile: 2
file content (43 lines) | stat: -rw-r--r-- 775 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
package device

import (
	"context"
	"time"
)

type poller interface {
	Wait() error
	Cancel()
}

type pollerFactory func(context.Context, time.Duration, time.Duration) (context.Context, poller)

func newPoller(ctx context.Context, checkInteval, expiresIn time.Duration) (context.Context, poller) {
	c, cancel := context.WithTimeout(ctx, expiresIn)
	return c, &intervalPoller{
		ctx:        c,
		interval:   checkInteval,
		cancelFunc: cancel,
	}
}

type intervalPoller struct {
	ctx        context.Context
	interval   time.Duration
	cancelFunc func()
}

func (p intervalPoller) Wait() error {
	t := time.NewTimer(p.interval)
	select {
	case <-p.ctx.Done():
		t.Stop()
		return p.ctx.Err()
	case <-t.C:
		return nil
	}
}

func (p intervalPoller) Cancel() {
	p.cancelFunc()
}