Skip to main content

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        // Where a paired server is, read.
120        | Request::RemoteHints { .. }
121        // A read; settings saved twice are saved once, and shuffle turned
122        // on twice shuffles once.
123        | Request::UpcomingInPlaylist { .. }
124        | Request::SetPlaylistSettings { .. }
125        // Taken away twice is taken away once.
126        | Request::RemovePlaylistArt { .. }
127        // A second sync joins the first; original timing or an
128        // offset set twice is set once.
129        | Request::SyncSubtitle { .. }
130        | Request::UseOriginalSubtitleTiming { .. }
131        | Request::SetSubtitleOffset { .. }
132        // A search is a read; the same result is never downloaded
133        // twice (the second gets the first's track); a download removed
134        // twice is removed once.
135        | Request::SearchSubtitles { .. }
136        | Request::DownloadSubtitle { .. }
137        | Request::RemoveSubtitleDownload { .. }
138        // Reads; the same request prepared twice is one download,
139        // under one id; a download cancelled twice is cancelled once (the
140        // second says `NotFound`).
141        | Request::DownloadOptions { .. }
142        | Request::PrepareDownload { .. }
143        | Request::DownloadStatus { .. }
144        | Request::CancelDownload { .. }
145        // Reads; an audio add-on prepared twice is one download, as
146        // `PrepareDownload`'s.
147        | Request::DownloadAddonOptions { .. }
148        | Request::ReadTrackSubtitle { .. }
149        | Request::PrepareAudioAddon { .. }
150        // Newest wins: an entry written once is older the second time.
151        | Request::SyncOfflineWatch { .. }
152        // A read; a play is recorded once by its track and time,
153        // and a like is newest-wins as a watch is.
154        | Request::MusicDownloadChanges { .. }
155        | Request::SyncOfflinePlays { .. }
156        // A row shown twice is shown once.
157        | Request::SetServerRow { .. }
158        // A server chosen twice is chosen once.
159        | Request::SetPlayFrom { .. } => true,
160        // Signing in: a failed password counts, a challenge is replaced,
161        // an approved sign-in is collected once.
162        Request::LoginPassword(_)
163        | Request::BeginDeviceSignIn { .. }
164        | Request::AwaitDeviceSignIn { .. }
165        | Request::BeginPasskeySignIn { .. }
166        | Request::FinishPasskeySignIn { .. }
167        | Request::BeginPortableSignIn
168        | Request::FinishPortableSignIn(_)
169        // An invite makes an account or a grant and uses itself up; a
170        // passkey's registration is replaced.
171        | Request::RedeemInvite(_)
172        | Request::BeginInvitePasskey(_)
173        | Request::RedeemInviteWithPasskey(_)
174        | Request::RedeemPortableInvite(_)
175        // A fetch counts against the code's tries, an answer is
176        // taken once, and a collected answer is gone.
177        | Request::FetchPairing { .. }
178        | Request::AnswerPairing { .. }
179        | Request::CollectPairing { .. }
180        // A resume makes a session; trust changes end sessions.
181        | Request::ResumeProfile { .. }
182        | Request::SetDeviceTrust { .. }
183        | Request::ForgetDevice { .. }
184        | Request::Logout
185        // Playback: each starts, stops or restarts an encode.
186        | Request::StartPlayback { .. }
187        | Request::StopPlayback { .. }
188        | Request::SeekPlayback { .. }
189        | Request::ChangeSubtitle { .. }
190        | Request::ChangeQuality { .. }
191        // PINs: a wrong one counts towards the lock-out.
192        | Request::SetPin { .. }
193        | Request::ClearPin { .. }
194        | Request::UnlockProfile { .. }
195        | Request::SetPinRequired { .. }
196        | Request::BeginPasskeyRegistration
197        | Request::FinishPasskeyRegistration(_)
198        | Request::RenamePasskey { .. }
199        | Request::RemovePasskey { .. }
200        // A wrong current password counts; a new one ends sessions.
201        | Request::ChangePassword(_)
202        // Confirming: a wrong password or passkey counts, a challenge is
203        // replaced or used up.
204        | Request::ConfirmPassword(_)
205        | Request::BeginConfirmPasskey
206        | Request::FinishConfirmPasskey(_)
207        // Looking up a wrong code consumes a failure-limit attempt too.
208        | Request::LookupDeviceSignIn { .. }
209        | Request::LookupInvite(_)
210        | Request::ApproveDeviceSignIn { .. }
211        | Request::DenyDeviceSignIn { .. }
212        | Request::UpdatePlaybackProgress { .. }
213        // As a playback's progress.
214        | Request::UpdateWatchProgress { .. }
215        | Request::MarkWatched { .. }
216        | Request::MarkUnwatched { .. }
217        | Request::HideFromContinueWatching { .. }
218        | Request::UnhideFromContinueWatching { .. }
219        | Request::SetTrackPreferences { .. }
220        | Request::SetTrackOverride { .. }
221        | Request::ClearTrackOverride { .. }
222        // A tie uses its nonce up; severing one is a change.
223        | Request::TieFinish { .. }
224        | Request::Untie { .. }
225        // A second invite would be a second code; a revoke is a change.
226        | Request::CreateInvite(_)
227        | Request::RevokeInvite { .. }
228        // A pairing takes the other server's use.
229        | Request::PairServer(_)
230        // Sharing is a change an admin confirmed.
231        | Request::ShareLibraries(_)
232        // A play token works once.
233        | Request::RedeemPlayToken(_)
234        // Each mints a token and counts a play.
235        | Request::RemoteGrant { .. }
236        // Leaving ends every session; a second try finds none.
237        | Request::LeaveServer
238        // A second create is a second playlist; an edit's adds
239        // would be added twice; pins move.
240        | Request::CreatePlaylist { .. }
241        | Request::UpdatePlaylist { .. }
242        | Request::EditPlaylist { .. }
243        | Request::DeletePlaylist { .. }
244        | Request::PinPlaylist { .. }
245        | Request::UnpinPlaylist { .. }
246        | Request::DismissPinNotes
247        // Each moves the user's place on.
248        | Request::PlayPlaylist { .. }
249        | Request::NextInPlaylist { .. }
250        // Imported twice is made twice.
251        | Request::ImportPlexPlaylists { .. }
252        // A second is a second report, and one a minute is allowed.
253        | Request::SendDiagnostics { .. }
254        | Request::Unknown => false,
255    }
256}