OLD | NEW |
---|---|
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 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 true, | 90 true, |
91 thread_, | 91 thread_, |
92 net_log, | 92 net_log, |
93 backend, | 93 backend, |
94 callback); | 94 callback); |
95 } | 95 } |
96 | 96 |
97 //----------------------------------------------------------------------------- | 97 //----------------------------------------------------------------------------- |
98 | 98 |
99 HttpCache::ActiveEntry::ActiveEntry(disk_cache::Entry* entry) | 99 HttpCache::ActiveEntry::ActiveEntry(disk_cache::Entry* entry) |
100 : disk_entry(entry), | 100 : disk_entry(entry) {} |
101 writer(NULL), | |
102 will_process_pending_queue(false), | |
103 doomed(false) { | |
104 } | |
105 | 101 |
106 HttpCache::ActiveEntry::~ActiveEntry() { | 102 HttpCache::ActiveEntry::~ActiveEntry() { |
107 if (disk_entry) { | 103 if (disk_entry) { |
108 disk_entry->Close(); | 104 disk_entry->Close(); |
109 disk_entry = NULL; | 105 disk_entry = nullptr; |
110 } | 106 } |
111 } | 107 } |
112 | 108 |
113 size_t HttpCache::ActiveEntry::EstimateMemoryUsage() const { | 109 size_t HttpCache::ActiveEntry::EstimateMemoryUsage() const { |
114 // Skip |disk_entry| which is tracked in simple_backend_impl; Skip |readers| | 110 // Skip |disk_entry| which is tracked in simple_backend_impl; Skip |readers| |
115 // and |pending_queue| because the Transactions are owned by their respective | 111 // and |add_to_entry_queue| because the Transactions are owned by their |
116 // URLRequestHttpJobs. | 112 // respective URLRequestHttpJobs. |
117 return 0; | 113 return 0; |
118 } | 114 } |
119 | 115 |
120 bool HttpCache::ActiveEntry::HasNoTransactions() { | 116 bool HttpCache::ActiveEntry::HasNoTransactions() { |
121 return !writer && readers.empty() && pending_queue.empty(); | 117 return !writer && readers.empty() && add_to_entry_queue.empty() && |
118 done_headers_queue.empty() && !headers_transaction; | |
119 } | |
120 | |
121 bool HttpCache::ActiveEntry::HasNoActiveTransactions() { | |
122 return !writer && readers.empty() && !headers_transaction; | |
122 } | 123 } |
123 | 124 |
124 //----------------------------------------------------------------------------- | 125 //----------------------------------------------------------------------------- |
125 | 126 |
126 // This structure keeps track of work items that are attempting to create or | 127 // This structure keeps track of work items that are attempting to create or |
127 // open cache entries or the backend itself. | 128 // open cache entries or the backend itself. |
128 struct HttpCache::PendingOp { | 129 struct HttpCache::PendingOp { |
129 PendingOp() : disk_entry(NULL) {} | 130 PendingOp() : disk_entry(NULL) {} |
130 ~PendingOp() {} | 131 ~PendingOp() {} |
131 | 132 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 new QuicServerInfoFactoryAdaptor(this)); | 361 new QuicServerInfoFactoryAdaptor(this)); |
361 } | 362 } |
362 } | 363 } |
363 | 364 |
364 HttpCache::~HttpCache() { | 365 HttpCache::~HttpCache() { |
365 // Transactions should see an invalid cache after this point; otherwise they | 366 // Transactions should see an invalid cache after this point; otherwise they |
366 // could see an inconsistent object (half destroyed). | 367 // could see an inconsistent object (half destroyed). |
367 weak_factory_.InvalidateWeakPtrs(); | 368 weak_factory_.InvalidateWeakPtrs(); |
368 | 369 |
369 // If we have any active entries remaining, then we need to deactivate them. | 370 // If we have any active entries remaining, then we need to deactivate them. |
370 // We may have some pending calls to OnProcessPendingQueue, but since those | 371 // We may have some pending tasks to process queued transactions ,but since |
371 // won't run (due to our destruction), we can simply ignore the corresponding | 372 // those won't run (due to our destruction), we can simply ignore the |
372 // will_process_pending_queue flag. | 373 // corresponding flags. |
373 while (!active_entries_.empty()) { | 374 while (!active_entries_.empty()) { |
374 ActiveEntry* entry = active_entries_.begin()->second.get(); | 375 ActiveEntry* entry = active_entries_.begin()->second.get(); |
375 entry->will_process_pending_queue = false; | 376 entry->will_process_queued_transactions = false; |
376 entry->pending_queue.clear(); | 377 entry->add_to_entry_queue.clear(); |
377 entry->readers.clear(); | 378 entry->readers.clear(); |
378 entry->writer = NULL; | 379 entry->done_headers_queue.clear(); |
380 entry->headers_transaction = nullptr; | |
381 entry->writer = nullptr; | |
379 DeactivateEntry(entry); | 382 DeactivateEntry(entry); |
380 } | 383 } |
381 | 384 |
382 doomed_entries_.clear(); | 385 doomed_entries_.clear(); |
383 | 386 |
384 // Before deleting pending_ops_, we have to make sure that the disk cache is | 387 // Before deleting pending_ops_, we have to make sure that the disk cache is |
385 // done with said operations, or it will attempt to use deleted data. | 388 // done with said operations, or it will attempt to use deleted data. |
386 disk_cache_.reset(); | 389 disk_cache_.reset(); |
387 | 390 |
388 for (auto pending_it = pending_ops_.begin(); pending_it != pending_ops_.end(); | 391 for (auto pending_it = pending_ops_.begin(); pending_it != pending_ops_.end(); |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 // We keep track of doomed entries so that we can ensure that they are | 632 // We keep track of doomed entries so that we can ensure that they are |
630 // cleaned up properly when the cache is destroyed. | 633 // cleaned up properly when the cache is destroyed. |
631 ActiveEntry* entry_ptr = entry.get(); | 634 ActiveEntry* entry_ptr = entry.get(); |
632 DCHECK_EQ(0u, doomed_entries_.count(entry_ptr)); | 635 DCHECK_EQ(0u, doomed_entries_.count(entry_ptr)); |
633 doomed_entries_[entry_ptr] = std::move(entry); | 636 doomed_entries_[entry_ptr] = std::move(entry); |
634 | 637 |
635 entry_ptr->disk_entry->Doom(); | 638 entry_ptr->disk_entry->Doom(); |
636 entry_ptr->doomed = true; | 639 entry_ptr->doomed = true; |
637 | 640 |
638 DCHECK(entry_ptr->writer || !entry_ptr->readers.empty() || | 641 DCHECK(entry_ptr->writer || !entry_ptr->readers.empty() || |
639 entry_ptr->will_process_pending_queue); | 642 entry_ptr->headers_transaction || |
643 entry_ptr->will_process_queued_transactions); | |
640 return OK; | 644 return OK; |
641 } | 645 } |
642 | 646 |
643 int HttpCache::AsyncDoomEntry(const std::string& key, Transaction* trans) { | 647 int HttpCache::AsyncDoomEntry(const std::string& key, Transaction* trans) { |
644 std::unique_ptr<WorkItem> item = | 648 std::unique_ptr<WorkItem> item = |
645 base::MakeUnique<WorkItem>(WI_DOOM_ENTRY, trans, nullptr); | 649 base::MakeUnique<WorkItem>(WI_DOOM_ENTRY, trans, nullptr); |
646 PendingOp* pending_op = GetPendingOp(key); | 650 PendingOp* pending_op = GetPendingOp(key); |
647 if (pending_op->writer) { | 651 if (pending_op->writer) { |
648 pending_op->pending_queue.push_back(std::move(item)); | 652 pending_op->pending_queue.push_back(std::move(item)); |
649 return ERR_IO_PENDING; | 653 return ERR_IO_PENDING; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
685 DCHECK(entry->doomed); | 689 DCHECK(entry->doomed); |
686 DCHECK(entry->HasNoTransactions()); | 690 DCHECK(entry->HasNoTransactions()); |
687 | 691 |
688 auto it = doomed_entries_.find(entry); | 692 auto it = doomed_entries_.find(entry); |
689 DCHECK(it != doomed_entries_.end()); | 693 DCHECK(it != doomed_entries_.end()); |
690 doomed_entries_.erase(it); | 694 doomed_entries_.erase(it); |
691 } | 695 } |
692 | 696 |
693 HttpCache::ActiveEntry* HttpCache::FindActiveEntry(const std::string& key) { | 697 HttpCache::ActiveEntry* HttpCache::FindActiveEntry(const std::string& key) { |
694 auto it = active_entries_.find(key); | 698 auto it = active_entries_.find(key); |
695 return it != active_entries_.end() ? it->second.get() : NULL; | 699 return it != active_entries_.end() ? it->second.get() : nullptr; |
696 } | 700 } |
697 | 701 |
698 HttpCache::ActiveEntry* HttpCache::ActivateEntry( | 702 HttpCache::ActiveEntry* HttpCache::ActivateEntry( |
699 disk_cache::Entry* disk_entry) { | 703 disk_cache::Entry* disk_entry) { |
700 DCHECK(!FindActiveEntry(disk_entry->GetKey())); | 704 DCHECK(!FindActiveEntry(disk_entry->GetKey())); |
701 ActiveEntry* entry = new ActiveEntry(disk_entry); | 705 ActiveEntry* entry = new ActiveEntry(disk_entry); |
702 active_entries_[disk_entry->GetKey()] = base::WrapUnique(entry); | 706 active_entries_[disk_entry->GetKey()] = base::WrapUnique(entry); |
703 return entry; | 707 return entry; |
704 } | 708 } |
705 | 709 |
706 void HttpCache::DeactivateEntry(ActiveEntry* entry) { | 710 void HttpCache::DeactivateEntry(ActiveEntry* entry) { |
707 DCHECK(!entry->will_process_pending_queue); | 711 DCHECK(!entry->will_process_queued_transactions); |
708 DCHECK(!entry->doomed); | 712 DCHECK(!entry->doomed); |
709 DCHECK(entry->disk_entry); | 713 DCHECK(entry->disk_entry); |
710 DCHECK(entry->HasNoTransactions()); | 714 DCHECK(entry->HasNoTransactions()); |
711 | 715 |
712 std::string key = entry->disk_entry->GetKey(); | 716 std::string key = entry->disk_entry->GetKey(); |
713 if (key.empty()) | 717 if (key.empty()) |
714 return SlowDeactivateEntry(entry); | 718 return SlowDeactivateEntry(entry); |
715 | 719 |
716 auto it = active_entries_.find(key); | 720 auto it = active_entries_.find(key); |
717 DCHECK(it != active_entries_.end()); | 721 DCHECK(it != active_entries_.end()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
827 } | 831 } |
828 | 832 |
829 void HttpCache::DestroyEntry(ActiveEntry* entry) { | 833 void HttpCache::DestroyEntry(ActiveEntry* entry) { |
830 if (entry->doomed) { | 834 if (entry->doomed) { |
831 FinalizeDoomedEntry(entry); | 835 FinalizeDoomedEntry(entry); |
832 } else { | 836 } else { |
833 DeactivateEntry(entry); | 837 DeactivateEntry(entry); |
834 } | 838 } |
835 } | 839 } |
836 | 840 |
837 int HttpCache::AddTransactionToEntry(ActiveEntry* entry, Transaction* trans) { | 841 int HttpCache::AddTransactionToEntry(ActiveEntry* entry, |
842 Transaction* transaction) { | |
838 DCHECK(entry); | 843 DCHECK(entry); |
839 DCHECK(entry->disk_entry); | 844 DCHECK(entry->disk_entry); |
840 | 845 // Always add a new transaction to the queue to maintain FIFO order. |
841 // We implement a basic reader/writer lock for the disk cache entry. If | 846 entry->add_to_entry_queue.push_back(transaction); |
842 // there is already a writer, then everyone has to wait for the writer to | 847 ProcessQueuedTransactions(entry); |
843 // finish before they can access the cache entry. There can be multiple | 848 return ERR_IO_PENDING; |
844 // readers. | 849 } |
845 // | 850 |
846 // NOTE: If the transaction can only write, then the entry should not be in | 851 int HttpCache::DoneWithResponseHeaders(ActiveEntry* entry, |
847 // use (since any existing entry should have already been doomed). | 852 Transaction* transaction) { |
848 | 853 // If |transaction| is the current writer, do nothing. This can happen for |
849 if (entry->writer || entry->will_process_pending_queue) { | 854 // range requests since they can go back to headers phase after starting to |
850 entry->pending_queue.push_back(trans); | 855 // write. |
851 return ERR_IO_PENDING; | 856 if (entry->writer == transaction) |
852 } | 857 return OK; |
853 | 858 |
854 if (trans->mode() & Transaction::WRITE) { | 859 DCHECK_EQ(entry->headers_transaction, transaction); |
855 // transaction needs exclusive access to the entry | 860 |
856 if (entry->readers.empty()) { | 861 entry->headers_transaction = nullptr; |
857 entry->writer = trans; | 862 |
858 } else { | 863 // If transaction is responsible for writing the response body, then do not go |
859 entry->pending_queue.push_back(trans); | 864 // through done_headers_queue for performance benefit. (Also, in case of |
860 return ERR_IO_PENDING; | 865 // writer transaction, the consumer sometimes depend on synchronous behaviour |
861 } | 866 // e.g. while computing raw headers size.) |
862 } else { | 867 if (transaction->mode() & Transaction::WRITE) { |
863 // transaction needs read access to the entry | 868 DCHECK(entry->done_headers_queue.empty()); |
864 entry->readers.insert(trans); | 869 DCHECK(!entry->writer); |
865 } | 870 entry->writer = transaction; |
866 | 871 ProcessQueuedTransactions(entry); |
867 // We do this before calling EntryAvailable to force any further calls to | 872 return OK; |
868 // AddTransactionToEntry to add their transaction to the pending queue, which | 873 } |
869 // ensures FIFO ordering. | 874 |
870 if (!entry->writer && !entry->pending_queue.empty()) | 875 // If this is not the first transaction in done_headers_queue, it should be a |
871 ProcessPendingQueue(entry); | 876 // read-mode transaction. |
872 | 877 DCHECK(entry->done_headers_queue.empty() || |
873 return OK; | 878 !(transaction->mode() & Transaction::WRITE)); |
874 } | 879 |
875 | 880 entry->done_headers_queue.push_back(transaction); |
876 void HttpCache::DoneWithEntry(ActiveEntry* entry, Transaction* trans, | 881 ProcessQueuedTransactions(entry); |
882 return ERR_IO_PENDING; | |
883 } | |
884 | |
885 void HttpCache::DoneWithEntry(ActiveEntry* entry, | |
886 Transaction* transaction, | |
877 bool cancel) { | 887 bool cancel) { |
878 // If we already posted a task to move on to the next transaction and this was | 888 // Transaction is waiting in the done_headers_queue. |
879 // the writer, there is nothing to cancel. | 889 auto it = std::find(entry->done_headers_queue.begin(), |
880 if (entry->will_process_pending_queue && entry->readers.empty()) | 890 entry->done_headers_queue.end(), transaction); |
881 return; | 891 if (it != entry->done_headers_queue.end()) { |
882 | 892 entry->done_headers_queue.erase(it); |
883 if (entry->writer) { | 893 if (cancel) |
884 DCHECK(trans == entry->writer); | 894 ProcessEntryFailure(entry); |
885 | 895 return; |
896 } | |
897 | |
898 // Transaction is removed in the headers phase. | |
899 if (transaction == entry->headers_transaction) { | |
900 // If the response is not written (cancel is true), consider it a failure. | |
901 DoneWritingToEntry(entry, !cancel, transaction); | |
902 return; | |
903 } | |
904 | |
905 // Transaction is removed in the writing phase. | |
906 if (transaction == entry->writer) { | |
886 // Assume there was a failure. | 907 // Assume there was a failure. |
887 bool success = false; | 908 bool success = false; |
888 if (cancel) { | 909 if (cancel) { |
889 DCHECK(entry->disk_entry); | 910 DCHECK(entry->disk_entry); |
890 // This is a successful operation in the sense that we want to keep the | 911 // This is a successful operation in the sense that we want to keep the |
891 // entry. | 912 // entry. |
892 success = trans->AddTruncatedFlag(); | 913 success = transaction->AddTruncatedFlag(); |
893 // The previous operation may have deleted the entry. | 914 // The previous operation may have deleted the entry. |
894 if (!trans->entry()) | 915 if (!transaction->entry()) |
895 return; | 916 return; |
896 } | 917 } |
897 DoneWritingToEntry(entry, success); | 918 DoneWritingToEntry(entry, success, transaction); |
919 return; | |
920 } | |
921 | |
922 // Transaction is reading from the entry. | |
923 DoneReadingFromEntry(entry, transaction); | |
924 } | |
925 | |
926 void HttpCache::DoneWritingToEntry(ActiveEntry* entry, | |
927 bool success, | |
928 Transaction* transaction) { | |
929 DCHECK(transaction == entry->writer || | |
930 transaction == entry->headers_transaction); | |
931 | |
932 if (transaction == entry->writer) | |
933 entry->writer = nullptr; | |
934 else | |
935 entry->headers_transaction = nullptr; | |
936 | |
937 // If writer fails, all transactions restart the headers_transaction. | |
jkarlin
2017/04/20 15:05:29
Not sure what this comment means.
shivanisha
2017/04/21 23:06:01
Whoops, fixed the comment.
| |
938 if (!success && entry->headers_transaction) { | |
939 entry->headers_transaction->SetValidatingCannotProceed(); | |
940 entry->headers_transaction = nullptr; | |
jkarlin
2017/04/20 15:05:29
It looks like the code is going to restart every t
shivanisha
2017/04/21 23:06:01
done
| |
941 DCHECK(entry->HasNoActiveTransactions()); | |
942 } | |
943 if (!success) | |
944 ProcessEntryFailure(entry); | |
945 else | |
946 ProcessQueuedTransactions(entry); | |
947 } | |
948 | |
949 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, | |
950 Transaction* transaction) { | |
951 DCHECK(!entry->writer); | |
952 auto it = entry->readers.find(transaction); | |
953 DCHECK(it != entry->readers.end()); | |
954 entry->readers.erase(it); | |
955 | |
956 ProcessQueuedTransactions(entry); | |
957 } | |
958 | |
959 void HttpCache::RemoveAllQueuedTransactions(ActiveEntry* entry, | |
960 TransactionList* list) { | |
961 // Process done_headers_queue before add_to_entry_queue to maintain FIFO | |
962 // order. | |
963 for (auto* transaction : entry->done_headers_queue) | |
964 list->push_back(transaction); | |
965 entry->done_headers_queue.clear(); | |
966 | |
967 for (auto* transaction : entry->add_to_entry_queue) | |
968 list->push_back(transaction); | |
969 entry->add_to_entry_queue.clear(); | |
970 } | |
971 | |
972 void HttpCache::ProcessEntryFailure(ActiveEntry* entry) { | |
973 // Failure case is either writer failing to completely write the response to | |
974 // the cache or validating transaction received a non-304 response. | |
975 TransactionList list; | |
976 if (entry->HasNoActiveTransactions() && | |
977 !entry->will_process_queued_transactions) { | |
978 entry->disk_entry->Doom(); | |
979 RemoveAllQueuedTransactions(entry, &list); | |
980 DestroyEntry(entry); | |
898 } else { | 981 } else { |
899 DoneReadingFromEntry(entry, trans); | 982 DoomActiveEntry(entry->disk_entry->GetKey()); |
900 } | 983 RemoveAllQueuedTransactions(entry, &list); |
901 } | 984 } |
902 | 985 // ERR_CACHE_RACE causes the transaction to restart the whole process. |
903 void HttpCache::DoneWritingToEntry(ActiveEntry* entry, bool success) { | 986 for (auto* transaction : list) |
904 DCHECK(entry->readers.empty()); | 987 transaction->io_callback().Run(net::ERR_CACHE_RACE); |
905 | 988 } |
906 entry->writer = NULL; | 989 |
907 | 990 void HttpCache::ProcessQueuedTransactions(ActiveEntry* entry) { |
908 if (success) { | 991 // Multiple readers may finish with an entry at once, so we want to batch up |
909 ProcessPendingQueue(entry); | 992 // calls to OnProcessQueuedTransactions. This flag also tells us that we |
993 // should not delete the entry before OnProcessQueuedTransactions runs. | |
994 if (entry->will_process_queued_transactions) | |
995 return; | |
996 | |
997 entry->will_process_queued_transactions = true; | |
998 | |
999 // Post a task instead of invoking the io callback of another transaction here | |
1000 // to avoid re-entrancy. | |
1001 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
1002 FROM_HERE, | |
1003 base::Bind(&HttpCache::OnProcessQueuedTransactions, GetWeakPtr(), entry)); | |
1004 } | |
1005 | |
1006 void HttpCache::ProcessAddToEntryQueue(ActiveEntry* entry) { | |
1007 DCHECK(!entry->add_to_entry_queue.empty()); | |
1008 | |
1009 // Note the entry may be new or may already have a response body written to | |
1010 // it. In both cases, a transaction needs to wait since only one transaction | |
1011 // can be in the headers phase at a time. | |
1012 if (entry->headers_transaction) { | |
1013 return; | |
1014 } | |
1015 Transaction* transaction = entry->add_to_entry_queue.front(); | |
1016 entry->add_to_entry_queue.erase(entry->add_to_entry_queue.begin()); | |
1017 entry->headers_transaction = transaction; | |
1018 | |
1019 transaction->io_callback().Run(OK); | |
1020 } | |
1021 | |
1022 void HttpCache::ProcessDoneHeadersQueue(ActiveEntry* entry) { | |
1023 DCHECK(!entry->writer); | |
1024 DCHECK(!entry->done_headers_queue.empty()); | |
1025 | |
1026 Transaction* transaction = entry->done_headers_queue.front(); | |
1027 | |
1028 // If this transaction is responsible for writing the response body. | |
1029 if (transaction->mode() & Transaction::WRITE) { | |
1030 entry->writer = transaction; | |
910 } else { | 1031 } else { |
911 DCHECK(!entry->will_process_pending_queue); | 1032 // If a transaction is in front of this queue with only read mode set and |
912 | 1033 // there is no writer, it implies response body is already written, convert |
913 // We failed to create this entry. | 1034 // to a reader. |
914 TransactionList pending_queue; | 1035 auto return_val = entry->readers.insert(transaction); |
915 pending_queue.swap(entry->pending_queue); | 1036 DCHECK_EQ(return_val.second, true); |
916 | 1037 } |
917 entry->disk_entry->Doom(); | 1038 |
918 DestroyEntry(entry); | 1039 // Post another task to give a chance to more transactions to either join |
919 | 1040 // readers or another transaction to start parallel validation. |
920 // We need to do something about these pending entries, which now need to | 1041 ProcessQueuedTransactions(entry); |
921 // be added to a new entry. | 1042 |
922 while (!pending_queue.empty()) { | 1043 entry->done_headers_queue.erase(entry->done_headers_queue.begin()); |
923 // ERR_CACHE_RACE causes the transaction to restart the whole process. | 1044 transaction->io_callback().Run(OK); |
924 pending_queue.front()->io_callback().Run(ERR_CACHE_RACE); | 1045 } |
925 pending_queue.pop_front(); | 1046 |
926 } | 1047 bool HttpCache::CanTransactionWriteResponseHeaders(ActiveEntry* entry, |
927 } | 1048 Transaction* transaction, |
928 } | 1049 int response_code) const { |
929 | 1050 if (transaction != entry->headers_transaction) |
930 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans) { | 1051 return false; |
931 DCHECK(!entry->writer); | 1052 |
932 | 1053 if (!(transaction->mode() & Transaction::WRITE)) |
933 auto it = entry->readers.find(trans); | 1054 return false; |
934 DCHECK(it != entry->readers.end()); | 1055 |
935 | 1056 // If its not a match then check if it is the transaction responsible for |
936 entry->readers.erase(it); | 1057 // writing the response body. |
937 | 1058 if (response_code != 304) { |
jkarlin
2017/04/20 15:05:29
Seems like we don't need the response code, but in
shivanisha
2017/04/21 23:06:01
Changed to use is_match boolean.
| |
938 ProcessPendingQueue(entry); | 1059 return !entry->writer && entry->done_headers_queue.empty() && |
939 } | 1060 entry->readers.empty(); |
940 | 1061 } |
941 void HttpCache::ConvertWriterToReader(ActiveEntry* entry) { | 1062 |
942 DCHECK(entry->writer); | 1063 return true; |
943 DCHECK(entry->writer->mode() == Transaction::READ_WRITE); | 1064 } |
944 DCHECK(entry->readers.empty()); | 1065 |
945 | 1066 bool HttpCache::IsTransactionWritingIncomplete( |
946 Transaction* trans = entry->writer; | 1067 ActiveEntry* entry, |
947 | 1068 Transaction* transaction, |
948 entry->writer = NULL; | 1069 const std::string& method) const { |
949 entry->readers.insert(trans); | 1070 if (transaction == entry->writer) |
950 | 1071 return true; |
951 ProcessPendingQueue(entry); | 1072 |
1073 if (method == "HEAD" || method == "DELETE") | |
1074 return false; | |
1075 | |
1076 // Check if transaction is about to start writing to the cache. | |
1077 | |
1078 // Transaction's mode may have been set to NONE if StopCaching was invoked. | |
1079 if (!(transaction->mode() & Transaction::WRITE || | |
1080 transaction->mode() == Transaction::NONE)) { | |
1081 return false; | |
1082 } | |
1083 | |
1084 // If a transaction is completing headers or done with headers phase with | |
1085 // write mode then it should be the future writer. Just checking the front of | |
1086 // done_headers_queue since the rest should anyways be READ mode transactions | |
1087 // as they would be a result of validation match. | |
1088 if (transaction == entry->headers_transaction || | |
jkarlin
2017/04/20 15:05:29
return transaction == entry->headers_transaction |
shivanisha
2017/04/21 23:06:01
done
| |
1089 transaction == entry->done_headers_queue.front()) { | |
1090 return true; | |
1091 } | |
1092 | |
1093 return false; | |
1094 } | |
1095 | |
1096 bool HttpCache::IsWritingInProgress(ActiveEntry* entry) const { | |
1097 return entry->writer ? true : false; | |
952 } | 1098 } |
953 | 1099 |
954 LoadState HttpCache::GetLoadStateForPendingTransaction( | 1100 LoadState HttpCache::GetLoadStateForPendingTransaction( |
955 const Transaction* trans) { | 1101 const Transaction* trans) { |
956 auto i = active_entries_.find(trans->key()); | 1102 auto i = active_entries_.find(trans->key()); |
957 if (i == active_entries_.end()) { | 1103 if (i == active_entries_.end()) { |
958 // If this is really a pending transaction, and it is not part of | 1104 // If this is really a pending transaction, and it is not part of |
959 // active_entries_, we should be creating the backend or the entry. | 1105 // active_entries_, we should be creating the backend or the entry. |
960 return LOAD_STATE_WAITING_FOR_CACHE; | 1106 return LOAD_STATE_WAITING_FOR_CACHE; |
961 } | 1107 } |
(...skipping 29 matching lines...) Expand all Loading... | |
991 | 1137 |
992 for (auto k = doomed_entries_.begin(); k != doomed_entries_.end() && !found; | 1138 for (auto k = doomed_entries_.begin(); k != doomed_entries_.end() && !found; |
993 ++k) { | 1139 ++k) { |
994 found = RemovePendingTransactionFromEntry(k->first, trans); | 1140 found = RemovePendingTransactionFromEntry(k->first, trans); |
995 } | 1141 } |
996 | 1142 |
997 DCHECK(found) << "Pending transaction not found"; | 1143 DCHECK(found) << "Pending transaction not found"; |
998 } | 1144 } |
999 | 1145 |
1000 bool HttpCache::RemovePendingTransactionFromEntry(ActiveEntry* entry, | 1146 bool HttpCache::RemovePendingTransactionFromEntry(ActiveEntry* entry, |
1001 Transaction* trans) { | 1147 Transaction* transaction) { |
1002 TransactionList& pending_queue = entry->pending_queue; | 1148 TransactionList& add_to_entry_queue = entry->add_to_entry_queue; |
1003 | 1149 |
1004 auto j = find(pending_queue.begin(), pending_queue.end(), trans); | 1150 auto j = |
1005 if (j == pending_queue.end()) | 1151 find(add_to_entry_queue.begin(), add_to_entry_queue.end(), transaction); |
1152 if (j == add_to_entry_queue.end()) | |
1006 return false; | 1153 return false; |
1007 | 1154 |
1008 pending_queue.erase(j); | 1155 add_to_entry_queue.erase(j); |
1009 return true; | 1156 return true; |
1010 } | 1157 } |
1011 | 1158 |
1012 bool HttpCache::RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 1159 bool HttpCache::RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
1013 Transaction* trans) { | 1160 Transaction* trans) { |
1014 if (pending_op->writer->Matches(trans)) { | 1161 if (pending_op->writer->Matches(trans)) { |
1015 pending_op->writer->ClearTransaction(); | 1162 pending_op->writer->ClearTransaction(); |
1016 pending_op->writer->ClearEntry(); | 1163 pending_op->writer->ClearEntry(); |
1017 return true; | 1164 return true; |
1018 } | 1165 } |
1019 WorkItemList& pending_queue = pending_op->pending_queue; | 1166 WorkItemList& pending_queue = pending_op->pending_queue; |
1020 | 1167 |
1021 for (auto it = pending_queue.begin(); it != pending_queue.end(); ++it) { | 1168 for (auto it = pending_queue.begin(); it != pending_queue.end(); ++it) { |
1022 if ((*it)->Matches(trans)) { | 1169 if ((*it)->Matches(trans)) { |
1023 pending_queue.erase(it); | 1170 pending_queue.erase(it); |
1024 return true; | 1171 return true; |
1025 } | 1172 } |
1026 } | 1173 } |
1027 return false; | 1174 return false; |
1028 } | 1175 } |
1029 | 1176 |
1030 void HttpCache::ProcessPendingQueue(ActiveEntry* entry) { | 1177 void HttpCache::OnProcessQueuedTransactions(ActiveEntry* entry) { |
1031 // Multiple readers may finish with an entry at once, so we want to batch up | 1178 entry->will_process_queued_transactions = false; |
1032 // calls to OnProcessPendingQueue. This flag also tells us that we should | |
1033 // not delete the entry before OnProcessPendingQueue runs. | |
1034 if (entry->will_process_pending_queue) | |
1035 return; | |
1036 entry->will_process_pending_queue = true; | |
1037 | 1179 |
1038 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1180 // Note that this function should only invoke one transaction's IO callback |
1039 FROM_HERE, | 1181 // since its possible for IO callbacks' consumers to destroy the cache/entry. |
1040 base::Bind(&HttpCache::OnProcessPendingQueue, GetWeakPtr(), entry)); | |
1041 } | |
1042 | |
1043 void HttpCache::OnProcessPendingQueue(ActiveEntry* entry) { | |
1044 entry->will_process_pending_queue = false; | |
1045 DCHECK(!entry->writer); | |
1046 | 1182 |
1047 // If no one is interested in this entry, then we can deactivate it. | 1183 // If no one is interested in this entry, then we can deactivate it. |
1048 if (entry->HasNoTransactions()) { | 1184 if (entry->HasNoTransactions()) { |
1049 DestroyEntry(entry); | 1185 DestroyEntry(entry); |
1050 return; | 1186 return; |
1051 } | 1187 } |
1052 | 1188 |
1053 if (entry->pending_queue.empty()) | 1189 if (entry->done_headers_queue.empty() && entry->add_to_entry_queue.empty()) |
1054 return; | 1190 return; |
1055 | 1191 |
1056 // Promote next transaction from the pending queue. | 1192 // To maintain FIFO order of transactions, done_headers_queue should be |
1057 Transaction* next = entry->pending_queue.front(); | 1193 // checked for processing before add_to_entry_queue. |
1058 if ((next->mode() & Transaction::WRITE) && !entry->readers.empty()) | |
1059 return; // Have to wait. | |
1060 | 1194 |
1061 entry->pending_queue.erase(entry->pending_queue.begin()); | 1195 // If another transaction is writing the response, let validated transactions |
1196 // wait till the response is complete. If the response is not yet started, the | |
1197 // done_headers_queue transaction should start writing it. | |
1198 if (!entry->writer && !entry->done_headers_queue.empty()) { | |
1199 ProcessDoneHeadersQueue(entry); | |
1200 return; | |
1201 } | |
1062 | 1202 |
1063 int rv = AddTransactionToEntry(entry, next); | 1203 if (!entry->add_to_entry_queue.empty()) |
1064 if (rv != ERR_IO_PENDING) { | 1204 ProcessAddToEntryQueue(entry); |
1065 next->io_callback().Run(rv); | |
1066 } | |
1067 } | 1205 } |
1068 | 1206 |
1069 void HttpCache::OnIOComplete(int result, PendingOp* pending_op) { | 1207 void HttpCache::OnIOComplete(int result, PendingOp* pending_op) { |
1070 WorkItemOperation op = pending_op->writer->operation(); | 1208 WorkItemOperation op = pending_op->writer->operation(); |
1071 | 1209 |
1072 // Completing the creation of the backend is simpler than the other cases. | 1210 // Completing the creation of the backend is simpler than the other cases. |
1073 if (op == WI_CREATE_BACKEND) | 1211 if (op == WI_CREATE_BACKEND) |
1074 return OnBackendCreated(result, pending_op); | 1212 return OnBackendCreated(result, pending_op); |
1075 | 1213 |
1076 std::unique_ptr<WorkItem> item = std::move(pending_op->writer); | 1214 std::unique_ptr<WorkItem> item = std::move(pending_op->writer); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1203 building_backend_ = false; | 1341 building_backend_ = false; |
1204 DeletePendingOp(pending_op); | 1342 DeletePendingOp(pending_op); |
1205 } | 1343 } |
1206 | 1344 |
1207 // The cache may be gone when we return from the callback. | 1345 // The cache may be gone when we return from the callback. |
1208 if (!item->DoCallback(result, disk_cache_.get())) | 1346 if (!item->DoCallback(result, disk_cache_.get())) |
1209 item->NotifyTransaction(result, NULL); | 1347 item->NotifyTransaction(result, NULL); |
1210 } | 1348 } |
1211 | 1349 |
1212 } // namespace net | 1350 } // namespace net |
OLD | NEW |