Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: net/http/http_cache.h

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: HttpCache::IsCancelResponseBody added. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/http/http_cache.cc » ('j') | net/http/http_cache.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file declares a HttpTransactionFactory implementation that can be 5 // This file declares a HttpTransactionFactory implementation that can be
6 // layered on top of another HttpTransactionFactory to add HTTP caching. The 6 // layered on top of another HttpTransactionFactory to add HTTP caching. The
7 // caching logic follows RFC 7234 (any exceptions are called out in the code). 7 // caching logic follows RFC 7234 (any exceptions are called out in the code).
8 // 8 //
9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for 9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for
10 // the cache storage. 10 // the cache storage.
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; 249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList;
250 250
251 struct ActiveEntry { 251 struct ActiveEntry {
252 explicit ActiveEntry(disk_cache::Entry* entry); 252 explicit ActiveEntry(disk_cache::Entry* entry);
253 ~ActiveEntry(); 253 ~ActiveEntry();
254 size_t EstimateMemoryUsage() const; 254 size_t EstimateMemoryUsage() const;
255 255
256 // Returns true if no transactions are associated with this entry. 256 // Returns true if no transactions are associated with this entry.
257 bool HasNoTransactions(); 257 bool HasNoTransactions();
258 258
259 disk_cache::Entry* disk_entry; 259 // Returns true if no active readers/writer transactions are associated
260 Transaction* writer; 260 // with this entry.
261 bool HasNoActiveTransactions();
262
263 disk_cache::Entry* disk_entry = nullptr;
264
265 // We implement a basic reader/writer lock for the disk cache entry. If
266 // there is a writer, then all read-only transactions must wait. Non
267 // read-only transactions can proceed to their validation phase. Validation
268 // is allowed for one transaction at a time so that we do not end up with
269 // wasted network requests.
270
271 // The following variables represent the various transactions associated
272 // with this entry in different stages. A transaction goes through these
273 // transitions.
274 //
275 // Write mode transactions:
276 // add_to_entry_queue-> headers_transaction -> done_headers_queue ->
277 // writer
278 // add_to_entry_queue-> headers_transaction -> done_headers_queue ->
279 // readers (once the data is written to the cache by another writer)
280 //
281 // Read only transactions:
282 // add_to_entry_queue-> readers (once the data is written to the cache by
283 // the writer)
284
285 // Transactions waiting to be added to entry.
286 TransactionList add_to_entry_queue;
287
288 // Transaction currently in the headers phase, either validating the
289 // response or getting new headers. This can exist simultaneously with
290 // writer or readers while validating existing headers.
291 Transaction* headers_transaction = nullptr;
292
293 // Transactions that have completed their headers phase and are waiting
294 // to read the response body or write the response body.
295 TransactionList done_headers_queue;
296
297 // Transaction currently reading from the network and writing to the cache.
298 Transaction* writer = nullptr;
299
300 // Transactions that can only read from the cache. Only one of writer or
301 // readers can exist at a time.
261 TransactionSet readers; 302 TransactionSet readers;
262 TransactionList pending_queue; 303
263 bool will_process_pending_queue; 304 // The following variables are true if OnProcessQueuedTransactions is posted
264 bool doomed; 305 bool will_process_queued_transactions = false;
306
307 // True if entry is doomed.
308 bool doomed = false;
265 }; 309 };
266 310
267 using ActiveEntriesMap = 311 using ActiveEntriesMap =
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; 312 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>;
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; 313 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>;
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; 314 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>;
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; 315 using PlaybackCacheMap = std::unordered_map<std::string, int>;
272 316
273 // Methods ------------------------------------------------------------------ 317 // Methods ------------------------------------------------------------------
274 318
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 379
336 // Creates the disk cache entry associated with |key|, returning an 380 // Creates the disk cache entry associated with |key|, returning an
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if 381 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if
338 // this method returns ERR_IO_PENDING. 382 // this method returns ERR_IO_PENDING.
339 int CreateEntry(const std::string& key, ActiveEntry** entry, 383 int CreateEntry(const std::string& key, ActiveEntry** entry,
340 Transaction* trans); 384 Transaction* trans);
341 385
342 // Destroys an ActiveEntry (active or doomed). 386 // Destroys an ActiveEntry (active or doomed).
343 void DestroyEntry(ActiveEntry* entry); 387 void DestroyEntry(ActiveEntry* entry);
344 388
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING 389 // Adds a transaction to an ActiveEntry. This method returns ERR_IO_PENDING
346 // the transaction will be notified about completion via its IO callback. This 390 // and the transaction will be notified about completion via its IO callback.
347 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be 391 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction);
348 // added to the provided entry, and it should retry the process with another 392
349 // one (in this case, the entry is no longer valid). 393 // Transaction invokes this when its response headers phase is complete
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); 394 // either on validating existing headers/skipping validation or getting new
395 // headers. is_match should be passed as true if the validation was a match or
396 // headers were retrieved the first time.
397 // If its not a match, this entry is doomed/destroyed and pending
398 // transactions are restarted. Returns OK.
399 // If it is a match, the transaction is added to a queue and a task is posted
400 // to process queued transactions of the entry. Returns ERR_IO_PENDING.
401 int DoneResponseHeaders(ActiveEntry* entry,
402 Transaction* transaction,
403 bool is_match);
351 404
352 // Called when the transaction has finished working with this entry. |cancel| 405 // Called when the transaction has finished working with this entry. |cancel|
353 // is true if the operation was cancelled by the caller instead of running 406 // is true if the operation was cancelled by the caller instead of running
354 // to completion. 407 // to completion.
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); 408 void DoneWithEntry(ActiveEntry* entry, Transaction* transaction, bool cancel);
409
410 // Returns true if destroying this transaction will lead to terminating the
411 // response body writing before it is complete.
412 bool IsCancelResponseBody(ActiveEntry* entry,
413 Transaction* transaction,
414 const std::string& method);
Randy Smith (Not in Mondays) 2017/03/30 19:18:05 const? (More interested for documentation purpose
shivanisha 2017/03/31 17:47:05 Done.
356 415
357 // Called when the transaction has finished writing to this entry. |success| 416 // Called when the transaction has finished writing to this entry. |success|
358 // is false if the cache entry should be deleted. 417 // is false if the cache entry should be deleted.
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); 418 void DoneWritingToEntry(ActiveEntry* entry,
419 bool success,
420 Transaction* transaction);
360 421
361 // Called when the transaction has finished reading from this entry. 422 // Called when the transaction has finished reading from this entry.
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); 423 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction);
363
364 // Converts the active writer transaction to a reader so that other
365 // transactions can start reading from this entry.
366 void ConvertWriterToReader(ActiveEntry* entry);
367 424
368 // Returns the LoadState of the provided pending transaction. 425 // Returns the LoadState of the provided pending transaction.
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); 426 LoadState GetLoadStateForPendingTransaction(const Transaction* trans);
370 427
371 // Removes the transaction |trans|, from the pending list of an entry 428 // Removes the transaction |trans|, from the pending list of an entry
372 // (PendingOp, active or doomed entry). 429 // (PendingOp, active or doomed entry).
373 void RemovePendingTransaction(Transaction* trans); 430 void RemovePendingTransaction(Transaction* trans);
374 431
375 // Removes the transaction |trans|, from the pending list of |entry|. 432 // Removes the transaction |trans|, from the pending list of |entry|.
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, 433 bool RemovePendingTransactionFromEntry(ActiveEntry* entry,
377 Transaction* trans); 434 Transaction* trans);
378 435
379 // Removes the transaction |trans|, from the pending list of |pending_op|. 436 // Removes the transaction |trans|, from the pending list of |pending_op|.
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, 437 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op,
381 Transaction* trans); 438 Transaction* trans);
382 // Resumes processing the pending list of |entry|. 439
383 void ProcessPendingQueue(ActiveEntry* entry); 440 // Removes and returns all queued transactions in |entry| in FIFO order. This
441 // includes transactions that have completed the headers phase and those that
442 // have not been added to the entry yet in that order. |list| is the output
443 // argument.
444 void RemoveAllQueuedTransactions(ActiveEntry* entry, TransactionList* list);
445
446 // Resumes processing the queued transactions of |entry|.
447 void ProcessQueuedTransactions(ActiveEntry* entry);
448
449 // Checks if a transaction can be added to the entry. If yes, it will
450 // invoke the IO callback of the transaction. This is a helper function for
451 // OnProcessQueuedTransactions.
452 void ProcessAddToEntryTransaction(ActiveEntry* entry);
453
454 // Checks if a transaction that has already completed the response headers
455 // phase can resume reading/writing the response body. If yes, it will
456 // invoke the IO callback of the transaction. This is a helper function for
457 // OnProcessQueuedTransactions.
458 void ProcessDoneHeadersTransaction(ActiveEntry* entry);
459
460 // Processes either writer's failure to write response body or
461 // headers_transactions's failure to write headers.
462 void ProcessEntryFailure(ActiveEntry* entry);
384 463
385 // Events (called via PostTask) --------------------------------------------- 464 // Events (called via PostTask) ---------------------------------------------
386 465
387 void OnProcessPendingQueue(ActiveEntry* entry); 466 void OnProcessQueuedTransactions(ActiveEntry* entry);
388 467
389 // Callbacks ---------------------------------------------------------------- 468 // Callbacks ----------------------------------------------------------------
390 469
391 // Processes BackendCallback notifications. 470 // Processes BackendCallback notifications.
392 void OnIOComplete(int result, PendingOp* entry); 471 void OnIOComplete(int result, PendingOp* entry);
393 472
394 // Helper to conditionally delete |pending_op| if the HttpCache object it 473 // Helper to conditionally delete |pending_op| if the HttpCache object it
395 // is meant for has been deleted. 474 // is meant for has been deleted.
396 // 475 //
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might 476 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 std::unique_ptr<base::Clock> clock_; 514 std::unique_ptr<base::Clock> clock_;
436 515
437 base::WeakPtrFactory<HttpCache> weak_factory_; 516 base::WeakPtrFactory<HttpCache> weak_factory_;
438 517
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); 518 DISALLOW_COPY_AND_ASSIGN(HttpCache);
440 }; 519 };
441 520
442 } // namespace net 521 } // namespace net
443 522
444 #endif // NET_HTTP_HTTP_CACHE_H_ 523 #endif // NET_HTTP_HTTP_CACHE_H_
OLDNEW
« no previous file with comments | « no previous file | net/http/http_cache.cc » ('j') | net/http/http_cache.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698