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        Request::Tracks { .. }
36        | Request::PlaybackPlan { .. }
37        | Request::SetTrackOverride { .. }
38        // Each probes the file to find the track.
39        | Request::SyncSubtitle { .. }
40        | Request::UseOriginalSubtitleTiming { .. }
41        | Request::SetSubtitleOffset { .. }
42        // Each probes the file to plan the download.
43        | Request::DownloadOptions { .. }
44        | Request::PrepareDownload { .. }
45        // An artist's albums are read one by one from the catalog.
46        | Request::MusicDownloadChanges { .. } => wait::PROBE,
47        Request::SearchSubtitles { .. } | Request::DownloadSubtitle { .. } => wait::SUBTITLE_SITE,
48        Request::StartPlayback { .. }
49        | Request::ChangeSubtitle { .. }
50        | Request::ChangeQuality { .. } => wait::PLAYBACK,
51        _ => Duration::ZERO,
52    }
53}
54
55/// Whether `request` may be sent a second time, on a new connection, when
56/// the first one died before it was answered. Reads and idempotent connection
57/// setup are safe:
58/// one that changes something may have been done before its answer was
59/// lost, and doing it twice is not the same as once (a sign-in approved
60/// twice, a PIN tried twice). Every request is named, so a new one is
61/// decided here when it is added.
62pub fn retry_safe(request: &Request) -> bool {
63    match request {
64        Request::Hello(_)
65        | Request::ActivateProfile
66        | Request::PlaybackProgressSequence { .. }
67        | Request::AuthMethods
68        | Request::Ping
69        | Request::Libraries
70        | Request::Items { .. }
71        | Request::ItemFilterValues { .. }
72        | Request::Item { .. }
73        | Request::NextEpisode { .. }
74        | Request::ShowNext { .. }
75        | Request::Search(_)
76        | Request::PersonItems { .. }
77        | Request::Tracks { .. }
78        | Request::WhoAmI
79        | Request::ListSessions
80        | Request::PlaybackPlan { .. }
81        | Request::PlaybackRevision { .. }
82        | Request::ListDevices
83        | Request::ListPasskeys
84        | Request::GetPlexLink
85        | Request::GetManifest { .. }
86        | Request::ReadSubtitle { .. }
87        | Request::ReadSubtitleSegment { .. }
88        | Request::GetPreviews { .. }
89        | Request::Home
90        | Request::TrustedProfiles
91        | Request::GetTrackPreferences
92        // A second challenge only takes the place of the first, which
93        // stays bounded per session and user.
94        | Request::TieBegin
95        // Public data read, and an offer that replaces itself.
96        | Request::IdentityRecord { .. }
97        | Request::IdentityRecordByHandle { .. }
98        | Request::OfferPairing(_)
99        | Request::ListInvites
100        // Asked again, the same playback's live token, or a new
101        // one bound to the same playback.
102        | Request::HttpPlaybackUrl { .. }
103        // Reads. A read may drop entries whose items left the
104        // library, which a second read would drop too.
105        | Request::ListPlaylists
106        | Request::GetPlaylist { .. }
107        // Liking what is liked, or unliking what is not, changes
108        // nothing.
109        | Request::Like { .. }
110        | Request::Unlike { .. }
111        // A read.
112        | Request::ListShareCandidates { .. }
113        // A read.
114        | Request::RecentlyPlayed { .. }
115        // Where a paired server is, read.
116        | Request::RemoteHints { .. }
117        // A read; settings saved twice are saved once, and shuffle turned
118        // on twice shuffles once.
119        | Request::UpcomingInPlaylist { .. }
120        | Request::SetPlaylistSettings { .. }
121        // Taken away twice is taken away once.
122        | Request::RemovePlaylistArt { .. }
123        // A second sync joins the first; original timing or an
124        // offset set twice is set once.
125        | Request::SyncSubtitle { .. }
126        | Request::UseOriginalSubtitleTiming { .. }
127        | Request::SetSubtitleOffset { .. }
128        // A search is a read; the same result is never downloaded
129        // twice (the second gets the first's track); a download removed
130        // twice is removed once.
131        | Request::SearchSubtitles { .. }
132        | Request::DownloadSubtitle { .. }
133        | Request::RemoveSubtitleDownload { .. }
134        // Reads; the same request prepared twice is one download,
135        // under one id; a download cancelled twice is cancelled once (the
136        // second says `NotFound`).
137        | Request::DownloadOptions { .. }
138        | Request::PrepareDownload { .. }
139        | Request::DownloadStatus { .. }
140        | Request::CancelDownload { .. }
141        // Newest wins: an entry written once is older the second time.
142        | Request::SyncOfflineWatch { .. }
143        // A read; a play is recorded once by its track and time,
144        // and a like is newest-wins as a watch is.
145        | Request::MusicDownloadChanges { .. }
146        | Request::SyncOfflinePlays { .. }
147        // A row shown twice is shown once.
148        | Request::SetServerRow { .. }
149        // A server chosen twice is chosen once.
150        | Request::SetPlayFrom { .. } => true,
151        // Signing in: a failed password counts, a challenge is replaced,
152        // an approved sign-in is collected once.
153        Request::LoginPassword(_)
154        | Request::BeginDeviceSignIn { .. }
155        | Request::AwaitDeviceSignIn { .. }
156        | Request::BeginPasskeySignIn { .. }
157        | Request::FinishPasskeySignIn { .. }
158        | Request::BeginPortableSignIn
159        | Request::FinishPortableSignIn(_)
160        // An invite makes an account or a grant and uses itself up; a
161        // passkey's registration is replaced.
162        | Request::RedeemInvite(_)
163        | Request::BeginInvitePasskey(_)
164        | Request::RedeemInviteWithPasskey(_)
165        | Request::RedeemPortableInvite(_)
166        // A fetch counts against the code's tries, an answer is
167        // taken once, and a collected answer is gone.
168        | Request::FetchPairing { .. }
169        | Request::AnswerPairing { .. }
170        | Request::CollectPairing { .. }
171        // A resume makes a session; trust changes end sessions.
172        | Request::ResumeProfile { .. }
173        | Request::SetDeviceTrust { .. }
174        | Request::ForgetDevice { .. }
175        | Request::Logout
176        // Playback: each starts, stops or restarts an encode.
177        | Request::StartPlayback { .. }
178        | Request::StopPlayback { .. }
179        | Request::SeekPlayback { .. }
180        | Request::ChangeSubtitle { .. }
181        | Request::ChangeQuality { .. }
182        // PINs: a wrong one counts towards the lock-out.
183        | Request::SetPin { .. }
184        | Request::ClearPin { .. }
185        | Request::UnlockProfile { .. }
186        | Request::SetPinRequired { .. }
187        | Request::BeginPasskeyRegistration
188        | Request::FinishPasskeyRegistration(_)
189        | Request::RenamePasskey { .. }
190        | Request::RemovePasskey { .. }
191        // A wrong current password counts; a new one ends sessions.
192        | Request::ChangePassword(_)
193        // Confirming: a wrong password or passkey counts, a challenge is
194        // replaced or used up.
195        | Request::ConfirmPassword(_)
196        | Request::BeginConfirmPasskey
197        | Request::FinishConfirmPasskey(_)
198        // Looking up a wrong code consumes a failure-limit attempt too.
199        | Request::LookupDeviceSignIn { .. }
200        | Request::LookupInvite(_)
201        | Request::ApproveDeviceSignIn { .. }
202        | Request::DenyDeviceSignIn { .. }
203        | Request::UpdatePlaybackProgress { .. }
204        // As a playback's progress.
205        | Request::UpdateWatchProgress { .. }
206        | Request::MarkWatched { .. }
207        | Request::MarkUnwatched { .. }
208        | Request::HideFromContinueWatching { .. }
209        | Request::UnhideFromContinueWatching { .. }
210        | Request::SetTrackPreferences { .. }
211        | Request::SetTrackOverride { .. }
212        | Request::ClearTrackOverride { .. }
213        // A tie uses its nonce up; severing one is a change.
214        | Request::TieFinish { .. }
215        | Request::Untie { .. }
216        // A second invite would be a second code; a revoke is a change.
217        | Request::CreateInvite(_)
218        | Request::RevokeInvite { .. }
219        // A pairing takes the other server's use.
220        | Request::PairServer(_)
221        // Sharing is a change an admin confirmed.
222        | Request::ShareLibraries(_)
223        // A play token works once.
224        | Request::RedeemPlayToken(_)
225        // Each mints a token and counts a play.
226        | Request::RemoteGrant { .. }
227        // Leaving ends every session; a second try finds none.
228        | Request::LeaveServer
229        // A second create is a second playlist; an edit's adds
230        // would be added twice; pins move.
231        | Request::CreatePlaylist { .. }
232        | Request::UpdatePlaylist { .. }
233        | Request::EditPlaylist { .. }
234        | Request::DeletePlaylist { .. }
235        | Request::PinPlaylist { .. }
236        | Request::UnpinPlaylist { .. }
237        | Request::DismissPinNotes
238        // Each moves the user's place on.
239        | Request::PlayPlaylist { .. }
240        | Request::NextInPlaylist { .. }
241        // Imported twice is made twice.
242        | Request::ImportPlexPlaylists { .. }
243        | Request::Unknown => false,
244    }
245}