same_peer_switch.go RawHistoryFolder

1package daemon
2
3import (
4	"sync"
5
6	"github.com/bnema/vev/internal/domain"
7	"github.com/bnema/vev/internal/protocol"
8)
9
10func (ac *attachedClient) offerSamePeerTarget(target protocol.ExactSessionTarget) {
11	ac.samePeerOfferMu.Lock()
12	ac.samePeerOffer = &target
13	ac.samePeerOfferMu.Unlock()
14}
15
16// offerSamePeerAttachTarget arms the exact target before publishing its
17// endpoint-empty handoff, then rolls the offer back if publication fails.
18func offerSamePeerAttachTarget(effect *attachmentEffect, target protocol.ExactSessionTarget) error {
19	if effect == nil || effect.ac == nil || !effect.current() {
20		return errAttachmentTransition
21	}
22	handoff := protocol.AttachTarget{
23		Session:           target.SessionName,
24		Intent:            protocol.IntentAttach,
25		ExactTarget:       &target,
26		EnvironmentPolicy: protocol.EnvironmentPolicyDaemonOwned,
27		SamePeer:          true,
28	}
29	if protocol.ValidateAttachTarget(handoff) != nil {
30		return errAttachmentTransition
31	}
32	effect.ac.offerSamePeerTarget(target)
33	if err := effect.sendControl(handoff); err != nil {
34		effect.ac.clearSamePeerOffer()
35		return err
36	}
37	return nil
38}
39
40func (ac *attachedClient) clearSamePeerOffer() {
41	ac.samePeerOfferMu.Lock()
42	ac.samePeerOffer = nil
43	ac.samePeerOfferMu.Unlock()
44}
45
46// consumeSamePeerOffer linearizes a switch request with the daemon's pending
47// endpoint-empty offer: while one is pending, only its exact target is
48// accepted. Without a pending offer the request is the client's own picker
49// choice. It grants no more than a fresh exact attach would on this
50// authenticated connection, and samePeerTarget still requires a live session
51// with the exact lifecycle.
52func (ac *attachedClient) consumeSamePeerOffer(target protocol.ExactSessionTarget) bool {
53	ac.samePeerOfferMu.Lock()
54	defer ac.samePeerOfferMu.Unlock()
55	if ac.samePeerOffer == nil {
56		return true
57	}
58	if *ac.samePeerOffer != target {
59		return false
60	}
61	ac.samePeerOffer = nil
62	return true
63}
64
65// switchSamePeerForAttachment commits one client-confirmed endpoint-empty
66// target. The request never names an endpoint: its authority is the current
67// authenticated attachment plus the target's exact lifecycle identity.
68func (d *Daemon) switchSamePeerForAttachment(effect *attachmentEffect, request protocol.SamePeerSwitchRequest) {
69	if err := request.Validate(); err != nil || !effect.current() || effect.sess == nil || effect.ac == nil {
70		return
71	}
72	if !effect.ac.consumeSamePeerOffer(request.Target) {
73		d.sendSamePeerSwitchFailure(effect, request.RequestID, protocol.SamePeerSwitchStaleTarget)
74		return
75	}
76
77	// The switch moves an attachment between sessions, so it must not run
78	// inside the exact set a KillAll purge has captured.
79	d.mu.Lock()
80	if d.purgeAdmissionClosedLocked() {
81		d.mu.Unlock()
82		d.sendSamePeerSwitchFailure(effect, request.RequestID, protocol.SamePeerSwitchStaleTarget)
83		return
84	}
85	d.purgeAdmissionActive++
86	d.mu.Unlock()
87	// The reservation covers only the membership change; first paint can
88	// block on a slow client and must not stall a purge's drain.
89	var releaseOnce sync.Once
90	release := func() { releaseOnce.Do(d.releasePurgeAdmission) }
91	defer release()
92
93	target, targetTabIndex, ok := d.samePeerTarget(request)
94	if !ok || target == effect.sess {
95		d.sendSamePeerSwitchFailure(effect, request.RequestID, protocol.SamePeerSwitchStaleTarget)
96		return
97	}
98
99	capability := effect.capability()
100	transition, err := d.transitionAttachment(attachmentTransitionRequest{
101		source:            effect.sess,
102		target:            target,
103		next:              effect.ac,
104		expectedTransport: effect.transport,
105		sourceCapability:  &capability,
106		sourceEffect:      effect,
107		action:            "same-peer-switch",
108		expectedTargetLifecycle: &attachmentLifecycleFence{
109			name:             request.Target.SessionName,
110			incarnation:      request.Target.LifecycleID,
111			checkIncarnation: true,
112		},
113		activateTargetTab: true,
114		targetTabIndex:    targetTabIndex,
115		ready:             true,
116	})
117	if err != nil {
118		d.sendSamePeerSwitchFailure(effect, request.RequestID, protocol.SamePeerSwitchStaleTarget)
119		return
120	}
121	release()
122
123	if fresh, admitted := effect.ac.beginAttachmentEffect(transition.published); admitted {
124		fresh.End()
125	}
126	d.touchMRU(target)
127	d.deferAttachmentTransitionCleanups(transition)
128	d.firstPaintForTransition(transition.published)
129}
130
131// samePeerTarget resolves the exact target and the client-owned tab cursor
132// under the daemon/session locks. A missing cursor deliberately falls back to
133// the target session's current default rather than leaking the source view.
134func (d *Daemon) samePeerTarget(request protocol.SamePeerSwitchRequest) (*session, int, bool) {
135	d.mu.Lock()
136	defer d.mu.Unlock()
137
138	target := d.findByNameLocked(request.Target.SessionName)
139	if target == nil {
140		return nil, 0, false
141	}
142	target.mu.Lock()
143	defer target.mu.Unlock()
144	if target.incarnation != request.Target.LifecycleID || len(target.tabs) == 0 {
145		return nil, 0, false
146	}
147	tabIndex := 0
148	if request.PreferredTabID == "" {
149		return target, tabIndex, true
150	}
151	for index, tab := range target.tabs {
152		if tab != nil && domain.TabStableID(tab.stableID) == request.PreferredTabID {
153			return target, index, true
154		}
155	}
156	return target, tabIndex, true
157}
158
159func (d *Daemon) sendSamePeerSwitchFailure(effect *attachmentEffect, requestID uint64, code protocol.SamePeerSwitchFailureCode) {
160	if effect == nil || effect.ac == nil {
161		return
162	}
163	sender := effect
164	if !sender.current() {
165		var admitted bool
166		sender, admitted = effect.ac.beginAttachmentEffect(effect.capability())
167		if !admitted {
168			return
169		}
170		defer sender.End()
171	}
172	failure := protocol.SamePeerSwitchFailure{RequestID: requestID, Code: code}
173	if failure.Validate() != nil {
174		return
175	}
176	if err := sender.sendControl(failure); err != nil {
177		d.detachOnAttachmentSendError(sender.capability(), sender.transport.transport)
178	}
179}

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