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

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

Issue 2886483002: Adds a new class HttpCache::Writers for multiple cache transactions reading from the network. (Closed)
Patch Set: rdsmith feedback Created 3 years, 7 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 | « net/http/http_cache_transaction.cc ('k') | net/http/http_cache_writers.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_HTTP_HTTP_CACHE_WRITERS_H_
6 #define NET_HTTP_HTTP_CACHE_WRITERS_H_
7 #include <list>
8 #include <memory>
9 #include "base/memory/weak_ptr.h"
10 #include "net/base/completion_callback.h"
11 #include "net/http/http_cache.h"
12
13 // If multiple HttpCache::Transactions are accessing the same cache entry
14 // simultaneously, their access to the data read from network is synchronized
15 // by HttpCache::Writers. This enables each of those transactions to drive
16 // reading the response body from the network ensuring a slow consumer does not
17 // starve other consumers of the same resource.
18
19 namespace net {
20
21 // Writer represents the set of all HttpCache::Transactions that are
22 // reading from the network using the same network transaction and writing to
23 // the same cache entry. It is owned by the ActiveEntry.
24 class HttpCache::Writers {
25 public:
26 // |*entry| and |*cache| should always be valid when accessed from Writers.
27 Writers(HttpCache* cache, ActiveEntry* entry);
28 ~Writers();
29
30 // Invokes Read on network transaction if a read is not already in progress.
31 // In case a read is already in progress then this transaction is added to
32 // a waiting queue and ERR_IO_PENDING is returned. If ERR_IO_PENDING is
33 // returned, the result of the read will be later communicated to the consumer
34 // via the |callback|.
35 int Read(scoped_refptr<IOBuffer> buf,
36 int buf_len,
37 const CompletionCallback& callback,
38 Transaction* transaction);
39
40 // Invoked when StopCaching is called on a member transaction.
41 // It stops caching only if there are no other transactions. Returns true if
42 // caching can be stopped.
43 bool StopCaching(Transaction* transaction);
44
45 // Membership functions.
46
47 // Adds an HttpCache::Transaction to Writers. Should only be invoked if
48 // CanAddTransaction() returns true. If |network_transaction| is non-null, it
49 // will be assigned to |network_transaction_|. The first transaction added
50 // should definitely pass a non-null |network_transaction|. |transaction| can
51 // be destroyed at any point and it should invoke RemoveTransaction() during
52 // its destruction.
53 void AddTransaction(Transaction* transaction,
54 HttpTransaction* network_transaction);
55
56 // Removes a transaction.
57 void RemoveTransaction(Transaction* transaction);
58
59 // Removes all transactions.
60 void RemoveAllTransactions();
61
62 // Invoked when there is a change in a member transaction's priority or a
63 // member transaction is removed.
64 void PriorityChanged();
65
66 // Returns true if this object is empty.
67 bool IsEmpty() const { return all_writers_.empty(); }
68
69 // Returns true is |transaction| is part of writers.
70 bool IsPresent(Transaction* transaction) const {
71 return all_writers_.count(transaction) > 0;
72 }
73
74 // When response is completely written, any idle writers are moved to
75 // entry_->readers.
76 void MoveIdleWritersToReaders();
77
78 // Makes writing an exclusine operation implying that Writers can contain at
79 // most one transaction for the complete reading/writing of the response body.
80 void SetExclusive() { is_exclusive_ = true; }
81
82 // Returns true if more writers can be added for shared writing.
83 bool CanAddWriters() const;
84
85 HttpTransaction* network_transaction() { return network_transaction_.get(); }
86
87 // Invoked from HttpCache when it is notified of a transaction failing to
88 // write. |error| indicates network read error code or cache write error.
89 void ProcessFailure(Transaction* transaction, int error);
90
91 // Invoked to mark an entry as truncated. Should be invoked when reading is
92 // not currently in progress.
Randy Smith (Not in Mondays) 2017/05/18 01:02:54 Is it possible for a consumer to know this? Naive
93 void TruncateEntry();
94
95 LoadState GetWriterLoadState();
96
97 // For testing.
98
99 int CountTransactionsForTesting() const { return all_writers_.size(); }
100 bool IsTruncatedForTesting() const { return truncated_; }
101
102 private:
103 enum class State {
104 NONE,
105 NETWORK_READ,
106 NETWORK_READ_COMPLETE,
107 CACHE_WRITE_DATA,
108 CACHE_WRITE_DATA_COMPLETE,
109 CACHE_WRITE_TRUNCATED_RESPONSE,
110 CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
111 };
112
113 // These transactions are waiting on Read. After the active transaction
114 // completes writing the data to the cache, their buffer would be filled with
115 // the data and their callback will be invoked.
116 struct WaitingForRead {
117 Transaction* transaction;
118 scoped_refptr<IOBuffer> read_buf;
119 int read_buf_len;
120 int write_len;
121 const CompletionCallback callback;
122 WaitingForRead(Transaction* transaction,
123 scoped_refptr<IOBuffer> read_buf,
124 int len,
125 const CompletionCallback& consumer_callback);
126 ~WaitingForRead();
127 WaitingForRead(const WaitingForRead&);
128 };
129 using WaitingForReadList = std::list<WaitingForRead>;
130
131 // Runs the state transition loop. Resets and calls |callback_| on exit,
132 // unless the return value is ERR_IO_PENDING.
133 int DoLoop(int result);
134
135 // State machine functions.
136 int DoNetworkRead();
137 int DoNetworkReadComplete(int result);
138 int DoCacheWriteData(int num_bytes);
139 int DoCacheWriteDataComplete(int result);
140 int DoCacheWriteTruncatedResponse();
141 int DoCacheWriteTruncatedResponseComplete(int result);
142
143 // Helper functions for callback.
144
145 void OnNetworkReadFailure(int result);
146 void OnCacheWriteFailure();
147 void OnDataReceived(int result);
148
149 // Helper function for writing to cache.
150 int WriteToEntry(int index,
151 int offset,
152 IOBuffer* data,
153 int data_len,
154 const CompletionCallback& callback);
155
156 // Notifies the transactions waiting on Read of the result, by posting a task
157 // for each of them.
158 void ProcessWaitingForReadTransactions(int result);
159
160 // Sets the state of idle writers so that they can fail any subsequent
161 // Read.
162 void SetIdleWritersFailState(int result);
163
164 RequestPriority GetCurrentHighestPriority();
165
166 bool IsResponseCompleted();
167
168 int WriteResponseInfo(bool truncated);
169
170 // IO Completion callback function.
171 void OnIOComplete(int result);
172
173 State next_state_ = State::NONE;
174
175 // True if only reading from network and not writing to cache.
176 bool network_read_only_ = false;
177
178 // Http Cache.
179 HttpCache* cache_;
180
181 // Owner of this object.
182 ActiveEntry* entry_ = nullptr;
183
184 std::unique_ptr<HttpTransaction> network_transaction_;
185
186 scoped_refptr<IOBuffer> read_buf_;
187
188 int io_buf_len_ = 0;
189 int write_len_ = 0;
190
191 // The cache transaction that is the current consumer of network_transaction_
192 // ::Read or writing to the entry and is waiting for the operation to be
193 // completed. This is used to ensure there is at most one consumer of
194 // network_transaction_ at a time.
195 Transaction* active_transaction_ = nullptr;
196
197 // Transactions whose consumers have invoked Read, but another transaction is
198 // currently the |active_transaction_|. After the network read and cache write
199 // is complete, the waiting transactions will be notified.
200 WaitingForReadList waiting_for_read_;
201
202 // Includes all transactions.
203 TransactionSet all_writers_;
204
205 // True if multiple transactions are not allowed e.g. for partial requests.
206 bool is_exclusive_ = false;
207
208 // Current priority of the request. If a higher priority transaction is
209 // added, the priority of network transaction will be increased.
210 RequestPriority priority_ = MINIMUM_PRIORITY;
211
212 bool truncated_ = false; // used for testing.
213
214 CompletionCallback callback_; // Consumer's callback.
215 CompletionCallback io_callback_;
216 base::WeakPtrFactory<Writers> weak_factory_;
217 DISALLOW_COPY_AND_ASSIGN(Writers);
218 };
219
220 } // namespace net
221 #endif // NET_HTTP_HTTP_CACHE_WRITERS_H_
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_cache_writers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698