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

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 feebdack 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
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.
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 bool StopCaching(Transaction* transaction);
51
52 // Adds an HttpCache::Transaction to Writers and if it's the first transaction
53 // added, transfers the ownership of the network transaction to Writers.
54 // Should only be invoked if CanAddWriters() returns true.
55 // |network_transaction| should be non-null only for the first transaction
56 // and it will be assigned to |network_transaction_|. If |is_exclusive| is
57 // true, it makes writing an exclusive operation implying that Writers can
58 // contain at most one transaction till the completion of the response body.
59 // |transaction| can be destroyed at any point and it should invoke
60 // RemoveTransaction() during its destruction.
61 void AddTransaction(Transaction* transaction,
62 std::unique_ptr<HttpTransaction> network_transaction,
63 bool is_exclusive);
64
65 // Removes a transaction. Should be invoked when this transaction is
66 // destroyed.
67 void RemoveTransaction(Transaction* transaction);
68
69 // Invoked when there is a change in a member transaction's priority or a
70 // member transaction is removed.
71 void UpdatePriority();
72
73 // Returns true if this object is empty.
74 bool IsEmpty() const { return all_writers_.empty(); }
75
76 // Returns true if |transaction| is part of writers.
77 bool HasTransaction(Transaction* transaction) const {
78 return all_writers_.count(transaction) > 0;
79 }
80
81 // Remove and return any idle writers. Should only be invoked when a
82 // response is completely written and when ContainesOnlyIdleWriters()
83 // returns true.
84 TransactionSet RemoveAllIdleWriters();
85
86 // Returns true if more writers can be added for shared writing.
87 bool CanAddWriters();
88
89 // TODO(shivanisha), Check if this function gets invoked in the integration
90 // CL. Remove if not.
91 HttpTransaction* network_transaction() { return network_transaction_.get(); }
92
93 // Invoked to mark an entry as truncated.
94 void TruncateEntry();
95
96 // Should be invoked only when writers has transactions attached to it and
97 // thus has a valid network transaction.
98 LoadState GetWriterLoadState();
99
100 // For testing.
101 int CountTransactionsForTesting() const { return all_writers_.size(); }
102 bool IsTruncatedForTesting() const { return truncated_; }
103
104 private:
105 friend class WritersTest;
106
107 enum class State {
108 UNSET,
109 NONE,
110 NETWORK_READ,
111 NETWORK_READ_COMPLETE,
112 CACHE_WRITE_DATA,
113 CACHE_WRITE_DATA_COMPLETE,
114 CACHE_WRITE_TRUNCATED_RESPONSE,
115 CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
116 };
117
118 // These transactions are waiting on Read. After the active transaction
119 // completes writing the data to the cache, their buffer would be filled with
120 // the data and their callback will be invoked.
121 struct WaitingForRead {
122 Transaction* transaction;
123 scoped_refptr<IOBuffer> read_buf;
124 int read_buf_len;
125 int write_len;
126 const CompletionCallback callback;
127 WaitingForRead(Transaction* transaction,
128 scoped_refptr<IOBuffer> read_buf,
129 int len,
130 const CompletionCallback& consumer_callback);
131 ~WaitingForRead();
132 WaitingForRead(const WaitingForRead&);
133 };
134 using WaitingForReadList = std::list<WaitingForRead>;
135
136 // Runs the state transition loop. Resets and calls |callback_| on exit,
137 // unless the return value is ERR_IO_PENDING.
138 int DoLoop(int result);
139
140 // State machine functions.
141 int DoNetworkRead();
142 int DoNetworkReadComplete(int result);
143 int DoCacheWriteData(int num_bytes);
144 int DoCacheWriteDataComplete(int result);
145 int DoCacheWriteTruncatedResponse();
146 int DoCacheWriteTruncatedResponseComplete(int result);
147
148 // Helper functions for callback.
149
150 void OnNetworkReadFailure(int result);
151 void OnCacheWriteFailure();
152 void OnDataReceived(int result);
153
154 // Notifies the transactions waiting on Read of the result, by posting a task
155 // for each of them.
156 void ProcessWaitingForReadTransactions(int result);
157
158 // Sets the state to FAIL_READ so that any subsequent Read on an idle
159 // transaction fails.
160 void SetIdleWritersFailState(int result);
161
162 // Called to reset state when all transaction references are removed from
163 // |this|.
164 void ResetStateForEmptyWriters();
165
166 // Invoked when |active_transaction_| fails to read from network or write to
167 // cache. |error| indicates network read error code or cache write error.
168 void ProcessFailure(Transaction* transaction, int error);
169
170 // Returns true if |this| only contains idle writers.
171 bool ContainsOnlyIdleWriters() const;
172
173 // IO Completion callback function.
174 void OnIOComplete(int result);
175
176 State next_state_ = State::NONE;
177
178 // True if only reading from network and not writing to cache.
179 bool network_read_only_ = false;
180
181 // TODO(shivanisha) Add HttpCache* cache_ = nullptr; on integration.
182
183 disk_cache::Entry* disk_entry_ = nullptr;
184
185 std::unique_ptr<HttpTransaction> network_transaction_ = nullptr;
186
187 scoped_refptr<IOBuffer> read_buf_ = nullptr;
188
189 int io_buf_len_ = 0;
190 int write_len_ = 0;
191
192 // The cache transaction that is the current consumer of network_transaction_
193 // ::Read or writing to the entry and is waiting for the operation to be
194 // completed. This is used to ensure there is at most one consumer of
195 // network_transaction_ at a time.
196 Transaction* active_transaction_ = nullptr;
197
198 // Transactions whose consumers have invoked Read, but another transaction is
199 // currently the |active_transaction_|. After the network read and cache write
200 // is complete, the waiting transactions will be notified.
201 WaitingForReadList waiting_for_read_;
202
203 // Includes all transactions. ResetStateForEmptyWriters should be invoked
204 // whenever all_writers_ becomes empty.
205 TransactionSet all_writers_;
206
207 // True if multiple transactions are not allowed e.g. for partial requests.
208 bool is_exclusive_ = false;
209
210 // Current priority of the request. It is always the maximum of all the writer
211 // transactions.
212 RequestPriority priority_ = MINIMUM_PRIORITY;
213
214 bool truncated_ = false; // used for testing.
215
216 CompletionCallback callback_; // Callback for active_transaction_.
217
218 base::WeakPtrFactory<Writers> weak_factory_;
219 DISALLOW_COPY_AND_ASSIGN(Writers);
220 };
221
222 } // namespace net
223
224 #endif // NET_HTTP_HTTP_CACHE_WRITERS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698