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