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

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: jkarlin@ feedback addressed. Created 3 years, 5 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
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
8 #include <list>
9 #include <memory>
10
11 #include "base/memory/weak_ptr.h"
12 #include "net/base/completion_callback.h"
13 #include "net/http/http_cache.h"
14
15 namespace net {
16
17 // If multiple HttpCache::Transactions are accessing the same cache entry
18 // simultaneously, their access to the data read from network is synchronized
19 // by HttpCache::Writers. This enables each of those transactions to drive
20 // reading the response body from the network ensuring a slow consumer does not
21 // starve other consumers of the same resource.
22
jkarlin 2017/07/06 18:50:04 Add a // on this line since it's part of the same
shivanisha 2017/07/11 02:10:26 done
23 // Writers represents the set of all HttpCache::Transactions that are
24 // reading from the network using the same network transaction and writing to
25 // the same cache entry. It is owned by the ActiveEntry.
26 class NET_EXPORT_PRIVATE HttpCache::Writers {
27 public:
28 // |*disk_entry| must outlive this object.
jkarlin 2017/07/06 18:50:04 s/|*disk_entry|/|entry|/
shivanisha 2017/07/11 02:10:26 done
29 Writers(disk_cache::Entry* entry);
30 ~Writers();
31
32 // Retrieves data from the network transaction associated with the Writers
33 // object. This may be done directly (via a network read into |*buf->data()|)
34 // or indirectly (by copying from another transactions buffer into
35 // |*buf->data()| on network read completion) depending on whether or not a
36 // read is currently in progress. May return the result synchronously or
37 // return ERR_IO_PENDING: if ERR_IO_PENDING is returned, |callback| will be
38 // run to inform the consumer of the result of the Read().
39 // |transaction| may be removed while Read() is ongoing. In that case Writers
40 // will still complete the Read() processing but will not invoke the
41 // |callback|.
42 int Read(scoped_refptr<IOBuffer> buf,
43 int buf_len,
44 const CompletionCallback& callback,
45 Transaction* transaction);
46
47 // Invoked when StopCaching is called on a member transaction.
48 // It stops caching only if there are no other transactions. Returns true if
49 // caching can be stopped.
50 // TODO(shivanisha@) Also document this conditional stopping in
51 // HttpTransaction on integration.
52 bool StopCaching(Transaction* transaction);
53
54 // Adds an HttpCache::Transaction to Writers and if it's the first transaction
55 // added, transfers the ownership of the network transaction to Writers.
56 // Should only be invoked if CanAddWriters() returns true.
57 // |network_transaction| should be non-null only for the first transaction
58 // and it will be assigned to |network_transaction_|. If |is_exclusive| is
59 // true, it makes writing an exclusive operation implying that Writers can
60 // contain at most one transaction till the completion of the response body.
61 // |transaction| can be destroyed at any point and it should invoke
62 // RemoveTransaction() during its destruction.
63 void AddTransaction(Transaction* transaction,
64 std::unique_ptr<HttpTransaction> network_transaction,
65 bool is_exclusive);
66
67 // Removes a transaction. Should be invoked when this transaction is
68 // destroyed.
69 void RemoveTransaction(Transaction* transaction);
70
71 // Invoked when there is a change in a member transaction's priority or a
72 // member transaction is removed.
73 void UpdatePriority();
74
75 // Returns true if this object is empty.
76 bool IsEmpty() const { return all_writers_.empty(); }
77
78 // Returns true if |transaction| is part of writers.
79 bool HasTransaction(Transaction* transaction) const {
80 return all_writers_.count(transaction) > 0;
81 }
82
83 // Remove and return any idle writers. Should only be invoked when a
84 // response is completely written and when ContainesOnlyIdleWriters()
85 // returns true.
86 TransactionSet RemoveAllIdleWriters();
87
88 // Returns true if more writers can be added for shared writing.
89 bool CanAddWriters();
90
91 // TODO(shivanisha), Check if this function gets invoked in the integration
92 // CL. Remove if not.
93 HttpTransaction* network_transaction() { return network_transaction_.get(); }
94
95 // Invoked to mark an entry as truncated. This must only be invoked when there
96 // is no ongoing Read() call.
97 void TruncateEntry();
98
99 // Should be invoked only when writers has transactions attached to it and
100 // thus has a valid network transaction.
101 LoadState GetWriterLoadState();
102
103 // For testing.
104 int CountTransactionsForTesting() const { return all_writers_.size(); }
105 bool IsTruncatedForTesting() const { return truncated_; }
106
107 private:
108 friend class WritersTest;
109
110 enum class State {
111 UNSET,
112 NONE,
113 NETWORK_READ,
114 NETWORK_READ_COMPLETE,
115 CACHE_WRITE_DATA,
116 CACHE_WRITE_DATA_COMPLETE,
117 CACHE_WRITE_TRUNCATED_RESPONSE,
118 CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
119 };
120
121 // These transactions are waiting on Read. After the active transaction
122 // completes writing the data to the cache, their buffer would be filled with
123 // the data and their callback will be invoked.
124 struct WaitingForRead {
125 Transaction* transaction;
126 scoped_refptr<IOBuffer> read_buf;
127 int read_buf_len;
128 int write_len;
129 const CompletionCallback callback;
130 WaitingForRead(Transaction* transaction,
131 scoped_refptr<IOBuffer> read_buf,
132 int len,
133 const CompletionCallback& consumer_callback);
134 ~WaitingForRead();
135 WaitingForRead(const WaitingForRead&);
136 };
137 using WaitingForReadList = std::list<WaitingForRead>;
138
139 // Runs the state transition loop. Resets and calls |callback_| on exit,
140 // unless the return value is ERR_IO_PENDING.
141 int DoLoop(int result);
142
143 // State machine functions.
144 int DoNetworkRead();
145 int DoNetworkReadComplete(int result);
146 int DoCacheWriteData(int num_bytes);
147 int DoCacheWriteDataComplete(int result);
148 int DoCacheWriteTruncatedResponse();
149 int DoCacheWriteTruncatedResponseComplete(int result);
150
151 // Helper functions for callback.
152
jkarlin 2017/07/06 18:50:04 Remove newline, the comment will apply to all meth
shivanisha 2017/07/11 02:10:26 done
153 void OnNetworkReadFailure(int result);
154 void OnCacheWriteFailure();
155 void OnDataReceived(int result);
156
157 // Notifies the transactions waiting on Read of the result, by posting a task
158 // for each of them.
159 void ProcessWaitingForReadTransactions(int result);
160
161 // Sets the state to FAIL_READ so that any subsequent Read on an idle
162 // transaction fails.
163 void SetIdleWritersFailState(int result);
164
165 // Called to reset state when all transaction references are removed from
166 // |this|.
167 void ResetStateForEmptyWriters();
168
169 // Invoked when |active_transaction_| fails to read from network or write to
170 // cache. |error| indicates network read error code or cache write error.
171 void ProcessFailure(Transaction* transaction, int error);
172
173 // Returns true if |this| only contains idle writers.
174 bool ContainsOnlyIdleWriters() const;
jkarlin 2017/07/06 18:50:04 Define what idle writers are.
shivanisha 2017/07/11 02:10:26 done
175
176 // IO Completion callback function.
177 void OnIOComplete(int result);
178
179 State next_state_ = State::NONE;
180
181 // True if only reading from network and not writing to cache.
182 bool network_read_only_ = false;
183
184 // TODO(shivanisha) Add HttpCache* cache_ = nullptr; on integration.
185
186 disk_cache::Entry* disk_entry_ = nullptr;
187
188 std::unique_ptr<HttpTransaction> network_transaction_ = nullptr;
189
190 scoped_refptr<IOBuffer> read_buf_ = nullptr;
191
192 int io_buf_len_ = 0;
193 int write_len_ = 0;
194
195 // The cache transaction that is the current consumer of network_transaction_
196 // ::Read or writing to the entry and is waiting for the operation to be
197 // completed. This is used to ensure there is at most one consumer of
198 // network_transaction_ at a time.
199 Transaction* active_transaction_ = nullptr;
200
201 // Transactions whose consumers have invoked Read, but another transaction is
202 // currently the |active_transaction_|. After the network read and cache write
203 // is complete, the waiting transactions will be notified.
204 WaitingForReadList waiting_for_read_;
205
206 // Includes all transactions. ResetStateForEmptyWriters should be invoked
207 // whenever all_writers_ becomes empty.
208 TransactionSet all_writers_;
209
210 // True if multiple transactions are not allowed e.g. for partial requests.
211 bool is_exclusive_ = false;
212
213 // Current priority of the request. It is always the maximum of all the writer
214 // transactions.
215 RequestPriority priority_ = MINIMUM_PRIORITY;
216
217 bool truncated_ = false; // used for testing.
218
219 CompletionCallback callback_; // Callback for active_transaction_.
220
221 base::WeakPtrFactory<Writers> weak_factory_;
222 DISALLOW_COPY_AND_ASSIGN(Writers);
223 };
224
225 } // namespace net
226
227 #endif // NET_HTTP_HTTP_CACHE_WRITERS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698