v0.10.0 ce48ee3

events.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package usecase
4
5import (
6	"context"
7	"errors"
8	"fmt"
9	"strconv"
10	"strings"
11	"sync"
12	"time"
13
14	"github.com/bnema/zerowrap"
15
16	"bnema.dev/ermite/internal/domain"
17	"bnema.dev/ermite/internal/ports"
18)
19
20// DefaultEventBuffer is the number of events the Events use case remembers.
21const DefaultEventBuffer = 1024
22
23// Limits of the watchers.
24const (
25	// MaxWatchTopics is the most topics one watch may follow.
26	MaxWatchTopics = 64
27	// MaxWatchers is the most watches in progress at once.
28	MaxWatchers = 256
29	// MaxWatchersPerClient is the most watches in progress for one client
30	// address (see ports.WatchParams.Client).
31	MaxWatchersPerClient = 16
32	// DefaultRevalidateEvery is how long a watcher with a stored token goes
33	// without asking the token store whether the token still exists.
34	DefaultRevalidateEvery = 5 * time.Second
35)
36
37// Topics a watcher may follow.
38const (
39	// TopicHome follows every repository the caller may see.
40	TopicHome = "home"
41	// topicRepoPrefix starts the topic of one repository: "repo:<name>".
42	topicRepoPrefix = "repo:"
43)
44
45// bufferedEvent is an event with its sequence number in the buffer, and what
46// was true of its repository when it was buffered. Access to an event is
47// decided from that, and from the repository as it is when the event is
48// delivered: a repository made public later does not reveal what happened
49// while it was private, and a repository deleted and created again under the
50// same name does not inherit the events of the old one.
51type bufferedEvent struct {
52	seq uint64
53	ev  domain.Event
54
55	// known is false when the repository did not exist any more when the
56	// event was buffered: nobody is told about it.
57	known bool
58	// vis is the visibility of the repository at that time.
59	vis domain.Visibility
60	// created is the creation time of that repository.
61	created time.Time
62}
63
64// Events keeps the last events of the bus in a ring buffer, numbers them, and
65// lets callers watch them: live, or replayed from the id of the last event
66// they saw. Events of a private repository only reach callers that may read
67// it, checked again for every event.
68//
69// Event ids are opaque to clients: "<boot>-<seq>", where boot identifies this
70// run of the server, so that an id from before a restart is unknown rather
71// than taken for a newer event.
72//
73// The goroutine that feeds the buffer uses the logger of the context given to
74// NewEvents and lives until Close.
75type Events struct {
76	repos *Repos
77	auth  ports.Revalidator
78	clock ports.Clock
79	boot  string
80	size  int
81	every time.Duration // see EventsConfig.RevalidateEvery
82
83	stop context.CancelFunc
84	done chan struct{}
85
86	mu      sync.Mutex
87	ring    []bufferedEvent // capacity size, oldest first
88	nextSeq uint64          // sequence number of the next event, from 1
89	changed chan struct{}   // closed and replaced on every append and on close
90	closed  bool
91	watches int
92	clients map[string]int // watches in progress by client
93}
94
95var _ ports.EventService = (*Events)(nil)
96
97// EventsConfig configures Events.
98type EventsConfig struct {
99	// Size is the number of events remembered; DefaultEventBuffer when not
100	// positive.
101	Size int
102	// Auth confirms that the token of a watcher still exists. Required.
103	Auth ports.Revalidator
104	// RevalidateEvery is how long the answer of Auth is kept for a watcher;
105	// DefaultRevalidateEvery when zero, and negative to ask for every event.
106	RevalidateEvery time.Duration
107}
108
109// NewEvents subscribes to the bus and starts remembering its events. ctx
110// carries the logger; its cancellation is not followed, call Close.
111func NewEvents(ctx context.Context, repos *Repos, bus ports.EventBus, clock ports.Clock, cfg EventsConfig) *Events {
112	size := cfg.Size
113	if size <= 0 {
114		size = DefaultEventBuffer
115	}
116	every := cfg.RevalidateEvery
117	if every == 0 {
118		every = DefaultRevalidateEvery
119	}
120	run, stop := context.WithCancel(context.WithoutCancel(ctx))
121	e := &Events{
122		repos:   repos,
123		auth:    cfg.Auth,
124		clock:   clock,
125		every:   every,
126		clients: make(map[string]int),
127		boot:    strconv.FormatInt(clock.Now().UnixMilli(), 36),
128		size:    size,
129		stop:    stop,
130		done:    make(chan struct{}),
131		nextSeq: 1,
132		changed: make(chan struct{}),
133	}
134	sub := bus.Subscribe(run) // before returning: nothing published after is missed
135	go e.feed(run, sub)
136	return e
137}
138
139// feed appends the events of the bus until it stops.
140func (e *Events) feed(ctx context.Context, sub <-chan domain.Event) {
141	defer close(e.done)
142	defer e.shutdown()
143	for {
144		select {
145		case ev, ok := <-sub:
146			if !ok {
147				return
148			}
149			e.append(ctx, ev)
150		case <-ctx.Done():
151			return
152		}
153	}
154}
155
156// snapshot records the visibility and the generation of the repository an
157// event is about. Every event of the repository carries them.
158func (e *Events) snapshot(ctx context.Context, ev domain.Event) bufferedEvent {
159	switch ev := ev.(type) {
160	case domain.RepoCreated:
161		return bufferedEvent{known: true, vis: ev.Visibility, created: ev.At}
162	case domain.RepoUpdated:
163		return bufferedEvent{known: true, vis: ev.Visibility, created: ev.CreatedAt}
164	case domain.RepoDeleted:
165		return bufferedEvent{known: true, vis: ev.Visibility, created: ev.CreatedAt}
166	case domain.PushEvent:
167		if ev.CreatedAt.IsZero() {
168			return bufferedEvent{} // repository unknown at publish time: nobody is told
169		}
170		return bufferedEvent{known: true, vis: ev.Visibility, created: ev.CreatedAt}
171	case domain.MergeRequestOpened:
172		return repoSnapshot(ev.Visibility, ev.CreatedAt)
173	case domain.MergeRequestMerged:
174		return repoSnapshot(ev.Visibility, ev.CreatedAt)
175	case domain.MergeRequestClosed:
176		return repoSnapshot(ev.Visibility, ev.CreatedAt)
177	case domain.CommentAdded:
178		return repoSnapshot(ev.Visibility, ev.CreatedAt)
179	case domain.RunUpdated:
180		return repoSnapshot(ev.Visibility, ev.CreatedAt)
181	case domain.JobUpdated:
182		return repoSnapshot(ev.Visibility, ev.CreatedAt)
183	}
184	return bufferedEvent{known: true}
185}
186
187// repoSnapshot is the snapshot of an event that carries the visibility and the
188// creation time of its repository; a zero creation time means the repository
189// was unknown when it was published, and nobody is told.
190func repoSnapshot(vis domain.Visibility, created time.Time) bufferedEvent {
191	if created.IsZero() {
192		return bufferedEvent{}
193	}
194	return bufferedEvent{known: true, vis: vis, created: created}
195}
196
197func (e *Events) append(ctx context.Context, ev domain.Event) {
198	be := e.snapshot(ctx, ev) // outside the lock: it reads the store
199	e.mu.Lock()
200	defer e.mu.Unlock()
201	if len(e.ring) == e.size {
202		copy(e.ring, e.ring[1:])
203		e.ring = e.ring[:e.size-1]
204	}
205	be.seq, be.ev = e.nextSeq, ev
206	e.ring = append(e.ring, be)
207	e.nextSeq++
208	e.wake()
209}
210
211// wake releases the watchers waiting for news. The caller holds mu.
212func (e *Events) wake() {
213	close(e.changed)
214	e.changed = make(chan struct{})
215}
216
217func (e *Events) shutdown() {
218	e.mu.Lock()
219	defer e.mu.Unlock()
220	if !e.closed {
221		e.closed = true
222		e.wake()
223	}
224}
225
226// Close ends every watch cleanly, stops feeding and waits for the feeder,
227// until ctx ends. It is safe to call more than once.
228func (e *Events) Close(ctx context.Context) {
229	e.stop()
230	select {
231	case <-e.done:
232	case <-ctx.Done():
233	}
234}
235
236// Watchers returns the number of watches in progress.
237func (e *Events) Watchers() int {
238	e.mu.Lock()
239	defer e.mu.Unlock()
240	return e.watches
241}
242
243func (e *Events) id(seq uint64) string { return e.boot + "-" + strconv.FormatUint(seq, 10) }
244
245// parseID returns the sequence number of an id of this run.
246func (e *Events) parseID(id string) (uint64, bool) {
247	boot, seq, ok := strings.Cut(id, "-")
248	if !ok || boot != e.boot {
249		return 0, false
250	}
251	n, err := strconv.ParseUint(seq, 10, 64)
252	return n, err == nil && n > 0
253}
254
255// watch is the state of one Watch call.
256type watch struct {
257	p     domain.Principal
258	home  bool
259	repos map[domain.RepoName]bool
260
261	checked time.Time // when the token store last confirmed the principal
262}
263
264func (w *watch) wants(name domain.RepoName) bool { return w.home || w.repos[name] }
265
266// parseTopics validates the topics of a watch. A repository topic needs a
267// repository the principal may read (else domain.ErrNotFound); an unknown
268// topic, or more than MaxWatchTopics, is domain.ErrInvalid.
269func (e *Events) parseTopics(ctx context.Context, p domain.Principal, topics []string) (*watch, error) {
270	if len(topics) > MaxWatchTopics {
271		return nil, fmt.Errorf("%w: %d topics, at most %d", domain.ErrInvalid, len(topics), MaxWatchTopics)
272	}
273	w := &watch{p: p, repos: make(map[domain.RepoName]bool), checked: time.Now()}
274	if len(topics) == 0 {
275		w.home = true
276	}
277	for _, t := range topics {
278		if t == TopicHome {
279			w.home = true
280			continue
281		}
282		raw, ok := strings.CutPrefix(t, topicRepoPrefix)
283		if !ok {
284			return nil, fmt.Errorf("%w: unknown topic %q (want %q or \"repo:<name>\")", domain.ErrInvalid, t, TopicHome)
285		}
286		name, err := domain.ParseRepoName(raw)
287		if err != nil {
288			return nil, err
289		}
290		if _, err := e.repos.Get(ctx, p, name); err != nil {
291			return nil, err
292		}
293		w.repos[name] = true
294	}
295	return w, nil
296}
297
298// Watch calls send for the events the principal may see until ctx ends, the
299// buffer closes (both return nil) or send fails (its error is returned).
300//
301// A principal with a stored token is checked again for every event, and every
302// few seconds when idle: when the token expired or was revoked the watch ends
303// with domain.ErrUnauthenticated and nothing more is sent. More watches than
304// MaxWatchers, or than MaxWatchersPerClient for one Client, give
305// domain.ErrResourceExhausted.
306//
307// Without ResumeAfter only new events are sent. With it, the buffered events
308// after that id are sent first, then new ones. When the id is no longer
309// buffered, or was not issued by this run, a gap comes first and the oldest
310// buffered event follows. A watcher that falls more than the buffer behind
311// gets a gap too, and carries on with the oldest buffered event.
312func (e *Events) Watch(ctx context.Context, p domain.Principal, params ports.WatchParams, send func(ports.WatchEvent) error) error {
313	w, err := e.parseTopics(ctx, p, params.Topics)
314	if err != nil {
315		return err
316	}
317
318	e.mu.Lock()
319	next := e.nextSeq // only new events
320	gap := false
321	if params.ResumeAfter != "" {
322		seq, ok := e.parseID(params.ResumeAfter)
323		oldest := e.nextSeq - uint64(len(e.ring)) // first buffered sequence, nextSeq when empty
324		switch {
325		case ok && seq >= oldest-1 && seq < e.nextSeq:
326			next = seq + 1
327		default:
328			next, gap = oldest, true
329		}
330	}
331	if e.watches >= MaxWatchers || (params.Client != "" && e.clients[params.Client] >= MaxWatchersPerClient) {
332		e.mu.Unlock()
333		return fmt.Errorf("%w: too many watchers", domain.ErrResourceExhausted)
334	}
335	e.watches++
336	if params.Client != "" {
337		e.clients[params.Client]++
338	}
339	e.mu.Unlock()
340	defer func() {
341		e.mu.Lock()
342		e.watches--
343		if params.Client != "" {
344			if e.clients[params.Client]--; e.clients[params.Client] == 0 {
345				delete(e.clients, params.Client)
346			}
347		}
348		e.mu.Unlock()
349	}()
350
351	if params.Ready != nil {
352		params.Ready()
353	}
354	log := zerowrap.FromCtx(ctx)
355	log.Debug().Strs("topics", params.Topics).Bool("resume", params.ResumeAfter != "").Bool("gap", gap).Msg("watch started")
356	if gap {
357		if err := send(ports.WatchEvent{Gap: true}); err != nil {
358			return err
359		}
360	}
361	for {
362		if err := e.stillAllowed(ctx, w); err != nil {
363			return err
364		}
365		batch, changed, oldest, lost, closed := e.since(next)
366		if lost {
367			if err := send(ports.WatchEvent{Gap: true}); err != nil {
368				return err
369			}
370			next = oldest
371			continue
372		}
373		for _, be := range batch {
374			if err := e.deliver(ctx, w, be, send); err != nil {
375				return err
376			}
377			next = be.seq + 1
378		}
379		if len(batch) > 0 {
380			continue
381		}
382		if closed {
383			return nil
384		}
385		e.wait(ctx, w, changed)
386		if ctx.Err() != nil {
387			return nil
388		}
389	}
390}
391
392// wait blocks until more events arrive or ctx ends. A watcher with a stored
393// token also wakes up every few seconds, so that a revocation ends the watch
394// of a quiet forge too (the caller checks the token on the next turn).
395func (e *Events) wait(ctx context.Context, w *watch, changed <-chan struct{}) {
396	var idle <-chan time.Time
397	if w.p.TokenID() != "" {
398		t := time.NewTimer(e.idleCheck())
399		defer t.Stop()
400		idle = t.C
401	}
402	select {
403	case <-changed:
404	case <-idle:
405	case <-ctx.Done():
406	}
407}
408
409// idleCheck is how long a watcher with a stored token waits for news before
410// checking its token anyway.
411func (e *Events) idleCheck() time.Duration {
412	if e.every <= 0 {
413		return DefaultRevalidateEvery
414	}
415	return min(e.every, DefaultRevalidateEvery)
416}
417
418// stillAllowed checks that the holder of a stored token may still watch: the
419// token has not expired and, at most every few seconds, still exists. Root and
420// anonymous callers have nothing to lose.
421func (e *Events) stillAllowed(ctx context.Context, w *watch) error {
422	if w.p.TokenID() == "" {
423		return nil
424	}
425	if exp := w.p.ExpiresAt(); !exp.IsZero() && !e.clock.Now().Before(exp) {
426		return domain.ErrUnauthenticated
427	}
428	if e.every > 0 && time.Since(w.checked) < e.every {
429		return nil
430	}
431	if err := e.auth.Revalidate(ctx, w.p); err != nil {
432		if ctx.Err() != nil {
433			return nil
434		}
435		return err
436	}
437	w.checked = time.Now()
438	return nil
439}
440
441// since returns the buffered events from sequence next on, and the channel
442// that closes when more arrive. lost is true when next fell out of the buffer;
443// the caller then restarts from oldest, the first buffered sequence. The
444// returned slice is a copy.
445func (e *Events) since(next uint64) (batch []bufferedEvent, changed <-chan struct{}, oldest uint64, lost, closed bool) {
446	e.mu.Lock()
447	defer e.mu.Unlock()
448	oldest = e.nextSeq - uint64(len(e.ring))
449	if next < oldest {
450		return nil, e.changed, oldest, true, e.closed
451	}
452	batch = append(batch, e.ring[next-oldest:]...)
453	return batch, e.changed, oldest, false, e.closed
454}
455
456// deliver sends the event if the watcher follows its topic and may see it.
457func (e *Events) deliver(ctx context.Context, w *watch, be bufferedEvent, send func(ports.WatchEvent) error) error {
458	if err := e.stillAllowed(ctx, w); err != nil {
459		return err
460	}
461	out := ports.WatchEvent{ID: e.id(be.seq), Event: be.ev}
462	switch ev := be.ev.(type) {
463	case domain.EventGap:
464		// The feeder lost events: tell every watcher.
465		return send(ports.WatchEvent{Gap: true})
466	case domain.RepoDeleted:
467		// The repository is gone: use the visibility it had.
468		if !w.wants(ev.Repo) || !canReadAs(w.p, ev.Repo, ev.Visibility) {
469			return nil
470		}
471		return send(out)
472	case domain.RepoCreated:
473		return e.deliverRepo(ctx, w, ev.Repo, true, be, out, send)
474	case domain.RepoUpdated:
475		return e.deliverRepo(ctx, w, ev.Repo, true, be, out, send)
476	case domain.PushEvent:
477		return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
478	case domain.MergeRequestOpened:
479		return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
480	case domain.MergeRequestMerged:
481		return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
482	case domain.MergeRequestClosed:
483		return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
484	case domain.CommentAdded:
485		return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
486	case domain.RunUpdated:
487		// CI events go on the topic of their repository only, never on home.
488		if !w.repos[ev.Run.Repo] {
489			return nil
490		}
491		return e.deliverRepo(ctx, w, ev.Run.Repo, false, be, out, send)
492	case domain.JobUpdated:
493		if !w.repos[ev.Job.Repo] {
494			return nil
495		}
496		return e.deliverRepo(ctx, w, ev.Job.Repo, false, be, out, send)
497	}
498	return nil // an event this version does not stream
499}
500
501// deliverRepo sends an event about a repository if the watcher could read the
502// repository when the event happened and can read it now, and the repository
503// is still the one the event is about. A repository that no longer exists is
504// skipped: its RepoDeleted follows or came before.
505//
506// The exception is a repository that became private: the watchers who could
507// read it before are told once, with its name and its new visibility only,
508// so that what they show can be removed.
509func (e *Events) deliverRepo(ctx context.Context, w *watch, name domain.RepoName, withRepo bool, be bufferedEvent, out ports.WatchEvent, send func(ports.WatchEvent) error) error {
510	if !w.wants(name) || !be.known {
511		return nil
512	}
513	repo, err := lookupRepo(ctx, e.repos.meta, name)
514	switch {
515	case errors.Is(err, domain.ErrNotFound):
516		return nil
517	case ctx.Err() != nil:
518		return nil
519	case err != nil:
520		return fmt.Errorf("check access to %q: %w", name, err)
521	}
522	if repo.CreatedAt.After(be.created) {
523		return nil // the event is about a repository of this name that was deleted
524	}
525	if !canReadAs(w.p, name, be.vis) {
526		if up, ok := be.ev.(domain.RepoUpdated); ok && canReadAs(w.p, name, up.Previous) {
527			out.Repo = &ports.RepoView{Repo: domain.Repo{Name: name, Visibility: up.Visibility}}
528			return send(out)
529		}
530		return nil
531	}
532	if !canRead(w.p, repo) {
533		return nil
534	}
535	if withRepo {
536		view, err := e.repos.View(ctx, repo)
537		if err != nil {
538			return err
539		}
540		out.Repo = &view
541	}
542	return send(out)
543}

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