spoolframe_protocol/policy.rs
1//! Request retry semantics and wait budgets shared by clients and servers.
2//! These do not change message encoding or protocol negotiation.
3
4use crate::Request;
5use std::time::Duration;
6
7/// Shared server waits and client allowances. Each is how long the server
8/// may take before it answers, not counting the round trip.
9pub mod wait {
10 use std::time::Duration;
11
12 /// `AwaitDeviceSignIn` (the server's `device_sign_in::AWAIT`).
13 pub const SIGN_IN: Duration = Duration::from_secs(25);
14 /// A read past what an encode has written, or of a subtitle still
15 /// being read as text (the server's `READ_WAIT`).
16 pub const READ: Duration = Duration::from_secs(10);
17 /// A request that may run ffprobe on the file (`Prober::DEFAULT_TIMEOUT`).
18 pub const PROBE: Duration = Duration::from_secs(30);
19 /// Starting a playback, or changing its subtitle: a probe, and for HLS
20 /// of a copied video every keyframe of the file, which the server
21 /// bounds only at 10 minutes. Two minutes is longer than a player
22 /// should spin; past it the user is better told.
23 pub const PLAYBACK: Duration = Duration::from_secs(120);
24 /// A request the server answers from a subtitle site: a
25 /// search, or a sign-in, a download and the file. Each call to the
26 /// site has 10 seconds.
27 pub const SUBTITLE_SITE: Duration = Duration::from_secs(30);
28}
29
30/// How long the server may hold `request` before it answers.
31pub fn server_wait(request: &Request) -> Duration {
32 match request {
33 Request::AwaitDeviceSignIn { .. } => wait::SIGN_IN,
34 Request::ReadSubtitle { .. } | Request::ReadSubtitleSegment { .. } => wait::READ,
35 // A probe of the file for its track, then a wait as a read's.
36 Request::ReadTrackSubtitle { .. } => wait::PROBE + wait::READ,
37 Request::Tracks { .. }
38 | Request::PlaybackPlan { .. }
39 | Request::SetTrackOverride { .. }
40 // Each probes the file to find the track.
41 | Request::SyncSubtitle { .. }
42 | Request::UseOriginalSubtitleTiming { .. }
43 | Request::SetSubtitleOffset { .. }
44 // Each probes the file to plan the download.
45 | Request::DownloadOptions { .. }
46 | Request::PrepareDownload { .. }
47 | Request::DownloadAddonOptions { .. }
48 | Request::PrepareAudioAddon { .. }
49 // An artist's albums are read one by one from the catalog.
50 | Request::MusicDownloadChanges { .. } => wait::PROBE,
51 Request::SearchSubtitles { .. } | Request::DownloadSubtitle { .. } => wait::SUBTITLE_SITE,
52 Request::StartPlayback { .. }
53 | Request::ChangeSubtitle { .. }
54 | Request::ChangeQuality { .. } => wait::PLAYBACK,
55 _ => Duration::ZERO,
56 }
57}
58
59/// Whether `request` may be sent a second time, on a new connection, when
60/// the first one died before it was answered. Reads and idempotent connection
61/// setup are safe:
62/// one that changes something may have been done before its answer was
63/// lost, and doing it twice is not the same as once (a sign-in approved
64/// twice, a PIN tried twice). Every request is named, so a new one is
65/// decided here when it is added.
66pub fn retry_safe(request: &Request) -> bool {
67 match request {
68 Request::Hello(_)
69 | Request::ActivateProfile
70 | Request::PlaybackProgressSequence { .. }
71 | Request::AuthMethods
72 | Request::Ping
73 | Request::Libraries
74 | Request::Items { .. }
75 | Request::ItemFilterValues { .. }
76 | Request::Item { .. }
77 | Request::NextEpisode { .. }
78 | Request::ShowNext { .. }
79 | Request::Search(_)
80 | Request::PersonItems { .. }
81 | Request::Tracks { .. }
82 | Request::WhoAmI
83 | Request::ListSessions
84 | Request::PlaybackPlan { .. }
85 | Request::PlaybackRevision { .. }
86 | Request::ListDevices
87 | Request::ListPasskeys
88 | Request::GetPlexLink
89 | Request::GetManifest { .. }
90 | Request::ReadSubtitle { .. }
91 | Request::ReadSubtitleSegment { .. }
92 | Request::GetPreviews { .. }
93 | Request::Home
94 | Request::TrustedProfiles
95 | Request::GetTrackPreferences
96 // A second challenge only takes the place of the first, which
97 // stays bounded per session and user.
98 | Request::TieBegin
99 // Public data read, and an offer that replaces itself.
100 | Request::IdentityRecord { .. }
101 | Request::IdentityRecordByHandle { .. }
102 | Request::OfferPairing(_)
103 | Request::ListInvites
104 // Asked again, the same playback's live token, or a new
105 // one bound to the same playback.
106 | Request::HttpPlaybackUrl { .. }
107 // Reads. A read may drop entries whose items left the
108 // library, which a second read would drop too.
109 | Request::ListPlaylists
110 | Request::GetPlaylist { .. }
111 // Liking what is liked, or unliking what is not, changes
112 // nothing.
113 | Request::Like { .. }
114 | Request::Unlike { .. }
115 // A read.
116 | Request::ListShareCandidates { .. }
117 // A read.
118 | Request::RecentlyPlayed { .. }
119 // Reads.
120 | Request::RecentlyPlayedRow { .. }
121 | Request::RecentlyPlayedPreferences
122 // Keeping the same preferences twice is keeping them once.
123 | Request::SetRecentlyPlayedPreferences { .. }
124 // Where a paired server is, read.
125 | Request::RemoteHints { .. }
126 // A read; settings saved twice are saved once, and shuffle turned
127 // on twice shuffles once.
128 | Request::UpcomingInPlaylist { .. }
129 | Request::SetPlaylistSettings { .. }
130 // Taken away twice is taken away once.
131 | Request::RemovePlaylistArt { .. }
132 // A second sync joins the first; original timing or an
133 // offset set twice is set once.
134 | Request::SyncSubtitle { .. }
135 | Request::UseOriginalSubtitleTiming { .. }
136 | Request::SetSubtitleOffset { .. }
137 // A search is a read; the same result is never downloaded
138 // twice (the second gets the first's track); a download removed
139 // twice is removed once.
140 | Request::SearchSubtitles { .. }
141 | Request::DownloadSubtitle { .. }
142 | Request::RemoveSubtitleDownload { .. }
143 // Reads; the same request prepared twice is one download,
144 // under one id; a download cancelled twice is cancelled once (the
145 // second says `NotFound`).
146 | Request::DownloadOptions { .. }
147 | Request::PrepareDownload { .. }
148 | Request::DownloadStatus { .. }
149 | Request::CancelDownload { .. }
150 // Reads; an audio add-on prepared twice is one download, as
151 // `PrepareDownload`'s.
152 | Request::DownloadAddonOptions { .. }
153 | Request::ReadTrackSubtitle { .. }
154 | Request::PrepareAudioAddon { .. }
155 // Newest wins: an entry written once is older the second time.
156 | Request::SyncOfflineWatch { .. }
157 // A read; a play is recorded once by its track and time,
158 // and a like is newest-wins as a watch is.
159 | Request::MusicDownloadChanges { .. }
160 | Request::SyncOfflinePlays { .. }
161 // A row shown twice is shown once.
162 | Request::SetServerRow { .. }
163 // A server chosen twice is chosen once.
164 | Request::SetPlayFrom { .. } => true,
165 // Signing in: a failed password counts, a challenge is replaced,
166 // an approved sign-in is collected once.
167 Request::LoginPassword(_)
168 | Request::BeginDeviceSignIn { .. }
169 | Request::AwaitDeviceSignIn { .. }
170 | Request::BeginPasskeySignIn { .. }
171 | Request::FinishPasskeySignIn { .. }
172 | Request::BeginPortableSignIn
173 | Request::FinishPortableSignIn(_)
174 // An invite makes an account or a grant and uses itself up; a
175 // passkey's registration is replaced.
176 | Request::RedeemInvite(_)
177 | Request::BeginInvitePasskey(_)
178 | Request::RedeemInviteWithPasskey(_)
179 | Request::RedeemPortableInvite(_)
180 // A fetch counts against the code's tries, an answer is
181 // taken once, and a collected answer is gone.
182 | Request::FetchPairing { .. }
183 | Request::AnswerPairing { .. }
184 | Request::CollectPairing { .. }
185 // A resume makes a session; trust changes end sessions.
186 | Request::ResumeProfile { .. }
187 | Request::SetDeviceTrust { .. }
188 | Request::ForgetDevice { .. }
189 | Request::Logout
190 // Playback: each starts, stops or restarts an encode.
191 | Request::StartPlayback { .. }
192 | Request::StopPlayback { .. }
193 | Request::SeekPlayback { .. }
194 | Request::ChangeSubtitle { .. }
195 | Request::ChangeQuality { .. }
196 // PINs: a wrong one counts towards the lock-out.
197 | Request::SetPin { .. }
198 | Request::ClearPin { .. }
199 | Request::UnlockProfile { .. }
200 | Request::SetPinRequired { .. }
201 | Request::BeginPasskeyRegistration
202 | Request::FinishPasskeyRegistration(_)
203 | Request::RenamePasskey { .. }
204 | Request::RemovePasskey { .. }
205 // A wrong current password counts; a new one ends sessions.
206 | Request::ChangePassword(_)
207 // Confirming: a wrong password or passkey counts, a challenge is
208 // replaced or used up.
209 | Request::ConfirmPassword(_)
210 | Request::BeginConfirmPasskey
211 | Request::FinishConfirmPasskey(_)
212 // Looking up a wrong code consumes a failure-limit attempt too.
213 | Request::LookupDeviceSignIn { .. }
214 | Request::LookupInvite(_)
215 | Request::ApproveDeviceSignIn { .. }
216 | Request::DenyDeviceSignIn { .. }
217 | Request::UpdatePlaybackProgress { .. }
218 // As a playback's progress.
219 | Request::UpdateWatchProgress { .. }
220 | Request::MarkWatched { .. }
221 | Request::MarkUnwatched { .. }
222 | Request::HideFromContinueWatching { .. }
223 | Request::UnhideFromContinueWatching { .. }
224 | Request::SetTrackPreferences { .. }
225 | Request::SetTrackOverride { .. }
226 | Request::ClearTrackOverride { .. }
227 // A tie uses its nonce up; severing one is a change.
228 | Request::TieFinish { .. }
229 | Request::Untie { .. }
230 // A second invite would be a second code; a revoke is a change.
231 | Request::CreateInvite(_)
232 | Request::RevokeInvite { .. }
233 // A pairing takes the other server's use.
234 | Request::PairServer(_)
235 // Sharing is a change an admin confirmed.
236 | Request::ShareLibraries(_)
237 // A play token works once.
238 | Request::RedeemPlayToken(_)
239 // Each mints a token and counts a play.
240 | Request::RemoteGrant { .. }
241 // Leaving ends every session; a second try finds none.
242 | Request::LeaveServer
243 // A second create is a second playlist; an edit's adds
244 // would be added twice; pins move.
245 | Request::CreatePlaylist { .. }
246 | Request::UpdatePlaylist { .. }
247 | Request::EditPlaylist { .. }
248 | Request::DeletePlaylist { .. }
249 | Request::PinPlaylist { .. }
250 | Request::UnpinPlaylist { .. }
251 | Request::DismissPinNotes
252 // Each moves the user's place on.
253 | Request::PlayPlaylist { .. }
254 | Request::NextInPlaylist { .. }
255 // Imported twice is made twice.
256 | Request::ImportPlexPlaylists { .. }
257 // A second is a second report, and one a minute is allowed.
258 | Request::SendDiagnostics { .. }
259 // A second offer is a second sign-in pending for the TV.
260 | Request::OfferDeviceSignIn { .. }
261 | Request::Unknown => false,
262 }
263}