detached 5278df2

mirrors.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package usecase
4
5import (
6	"context"
7	"crypto/ed25519"
8	"sync"
9	"time"
10
11	"bnema.dev/ermite/internal/domain"
12	"bnema.dev/ermite/internal/ports"
13)
14
15const mirrorQueueSize = 64
16
17// MirrorsConfig supplies the mirror use case's dependencies.
18type MirrorsConfig struct {
19	Store        ports.MirrorStore
20	Keys         ports.MirrorKeyStore
21	Sealer       ports.SecretSealer
22	Pusher       ports.MirrorPusher
23	Meta         ports.RepoMetaStore
24	Git          ports.GitRepos
25	Reader       ports.RepoReader
26	Bus          ports.EventBus
27	Clock        ports.Clock
28	PublicURL    string
29	ScheduleTick time.Duration
30	Workers      int
31	QueueSize    int // jobs waiting for a worker; default 64
32	// ValidateURL checks a resolved mirror URL; default domain.ValidateMirrorURL. Tests of
33	// local transports inject a permissive check.
34	ValidateURL func(string) error
35}
36
37// mirrorGen counts the changes of a repository (dirty) and the count the last successful
38// sync saw (clean, meaningful once cleanKnown).
39type mirrorGen struct {
40	dirty, clean uint64
41	cleanKnown   bool
42}
43
44// Mirrors manages outbound repository mirrors.
45type Mirrors struct {
46	cfg      MirrorsConfig
47	configMu sync.RWMutex // configuration changes; held for reads only around store reads and writes, never during a push
48	mu       sync.Mutex
49	gens     map[domain.RepoName]mirrorGen
50	flights  map[domain.RepoName]*mirrorFlight
51	flightWG sync.WaitGroup // one per flight, for tests that wait for the queue to drain
52	ctx      context.Context
53	cancel   context.CancelFunc
54	wg       sync.WaitGroup
55	start    sync.Once
56	closed   bool
57	jobs     chan domain.RepoName
58
59	scheduleMu sync.Mutex
60	lastMinute time.Time
61	deferred   map[domain.RepoName]struct{} // due, but the queue was full
62
63	keyMu      sync.Mutex
64	key        ed25519.PrivateKey
65	keyCreated time.Time
66}
67
68// NewMirrors builds the mirror use case.
69func NewMirrors(cfg MirrorsConfig) *Mirrors {
70	if cfg.ScheduleTick <= 0 {
71		cfg.ScheduleTick = time.Second
72	}
73	if cfg.Workers <= 0 {
74		cfg.Workers = 2
75	}
76	if cfg.QueueSize <= 0 {
77		cfg.QueueSize = mirrorQueueSize
78	}
79	if cfg.ValidateURL == nil {
80		cfg.ValidateURL = domain.ValidateMirrorURL
81	}
82	ctx, cancel := context.WithCancel(context.Background())
83	return &Mirrors{
84		cfg: cfg, ctx: ctx, cancel: cancel,
85		gens:       make(map[domain.RepoName]mirrorGen),
86		flights:    make(map[domain.RepoName]*mirrorFlight),
87		deferred:   make(map[domain.RepoName]struct{}),
88		jobs:       make(chan domain.RepoName, cfg.QueueSize),
89		lastMinute: cfg.Clock.Now().UTC().Truncate(time.Minute),
90	}
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 2

Tags 45

↑↓ moveenter openesc close