v0.3.0 99dc266

pacer.go RawHistoryFolder

1package dgram
2
3import (
4	"errors"
5	"math"
6	"math/bits"
7	"time"
8
9	"github.com/bnema/vev/internal/ports"
10)
11
12var errPacerClosed = errors.New("dgram: pacer closed")
13
14type paceLimits func() (bytesPerSecond int, burstBytes int)
15
16type bytePacer struct {
17	clk        ports.Clock
18	tokens     int64
19	last       time.Time
20	afterTimer func(time.Time)
21}
22
23func (p *bytePacer) wait(done <-chan struct{}, n int, limits paceLimits) error {
24	if n <= 0 {
25		return nil
26	}
27	for {
28		now := p.clk.Now()
29		rate, burst := limits()
30		if rate < 1 {
31			rate = 1
32		}
33		if burst < n {
34			burst = n
35		}
36		if p.last.IsZero() {
37			p.last = now
38			p.tokens = int64(burst)
39		} else {
40			elapsed := now.Sub(p.last)
41			if elapsed > 0 {
42				capacity := int64(burst) - p.tokens
43				p.tokens += mulDivFloorSaturating(int64(elapsed), int64(rate), int64(time.Second), capacity)
44				p.last = now
45			}
46			p.tokens = min(p.tokens, int64(burst))
47		}
48		if p.tokens >= int64(n) {
49			p.tokens -= int64(n)
50			return nil
51		}
52
53		deficit := int64(n) - p.tokens
54		wait := time.Duration(mulDivCeilSaturating(deficit, int64(time.Second), int64(rate)))
55		timer := p.clk.NewTimer(wait)
56		if p.afterTimer != nil {
57			p.afterTimer(now.Add(wait))
58		}
59		select {
60		case <-timer.C():
61			timer.Stop()
62		case <-done:
63			timer.Stop()
64			return errPacerClosed
65		}
66	}
67}
68
69func mulDivFloorSaturating(a, b, divisor, limit int64) int64 {
70	if a <= 0 || b <= 0 || limit <= 0 {
71		return 0
72	}
73	hi, lo := bits.Mul64(uint64(a), uint64(b))
74	if hi >= uint64(divisor) {
75		return limit
76	}
77	quotient, _ := bits.Div64(hi, lo, uint64(divisor))
78	return min(int64(min(quotient, uint64(math.MaxInt64))), limit)
79}
80
81func mulDivCeilSaturating(a, b, divisor int64) int64 {
82	hi, lo := bits.Mul64(uint64(a), uint64(b))
83	if hi >= uint64(divisor) {
84		return math.MaxInt64
85	}
86	quotient, remainder := bits.Div64(hi, lo, uint64(divisor))
87	if remainder != 0 {
88		quotient++
89	}
90	return int64(min(quotient, uint64(math.MaxInt64)))
91}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 12

Tags 11

↑↓ moveenter openesc close