in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! The thumbnail worker. It takes the files whose thumbnail is `pending` (pictures of the types the decoder |
| 2 | //! reads), makes a WebP thumbnail of each, stores it under `thumbs/<id>` and marks the file `ready`; a picture |
| 3 | //! that cannot be decoded, or that is too large, is marked `failed`. It runs at most [`CONCURRENCY`] decodes at |
| 4 | //! once, each on a blocking thread, and never runs an external program. |
| 5 | //! |
| 6 | //! Ordering: the thumbnail bytes are stored before the row says `ready`. If the file was purged while its |
| 7 | //! thumbnail was made, the row update finds nothing and the stored bytes are deleted, so no row is left |
| 8 | //! without its file. |
| 9 | |
| 10 | use ; |
| 11 | use Duration; |
| 12 | |
| 13 | use ; |
| 14 | use ; |
| 15 | use ; |
| 16 | use ; |
| 17 | use ; |
| 18 | use ; |
| 19 | |
| 20 | use crateTopic; |
| 21 | use crateAppState; |
| 22 | |
| 23 | /// Most files taken from the queue in one round. |
| 24 | pub const BATCH: usize = 16; |
| 25 | |
| 26 | /// Most thumbnails made at the same time. One, because a decode may hold a whole source picture |
| 27 | /// ([`MAX_SOURCE_BYTES`], 64 MiB) and its decoder buffers ([`in_core::thumbs::MAX_ALLOC_BYTES`], 256 MiB); two |
| 28 | /// together could reach about 640 MiB. One keeps the peak near 320 MiB. |
| 29 | pub const CONCURRENCY: usize = 1; |
| 30 | |
| 31 | /// Longest a decode may run before its file is marked `failed`: ten seconds. |
| 32 | pub const DECODE_TIMEOUT: Duration = from_secs; |
| 33 | |
| 34 | /// The decode slots. A slot is moved into the blocking decode, so it is released when the decoder returns, |
| 35 | /// not when the caller stops waiting. A timed-out decode that still runs keeps its slot, and no other decode |
| 36 | /// starts beside it, so stragglers cannot pile up. |
| 37 | static DECODE_SLOTS: = |
| 38 | new; |
| 39 | |
| 40 | /// How long the worker waits when the queue is empty before it looks again. |
| 41 | pub const IDLE: Duration = from_secs; |
| 42 | |
| 43 | /// Largest thumbnail the worker stores. A 320 pixel lossless WebP is far below this. |
| 44 | const MAX_THUMB_BYTES: u64 = 8 * 1024 * 1024; |
| 45 | |
| 46 | /// Runs rounds until the stop flag is set. A round that found work is followed at once by the next one. |
| 47 | pub async |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | /// One round: up to [`BATCH`] pending files, [`CONCURRENCY`] at a time. Returns how many files it took. |
| 65 | async |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | /// Makes the thumbnail of one file and records the outcome. Every taken file leaves `pending`, so the queue |
| 75 | /// always shrinks. |
| 76 | async |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | /// Decodes the stored picture and stores its thumbnail. Any failure is returned, and the caller marks the |
| 99 | /// file `failed`. |
| 100 | async |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | /// The whole stored picture, read in frames. `size` is at most [`MAX_SOURCE_BYTES`]. |
| 135 | async |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 |