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

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

Powered by Google App Engine
This is Rietveld 408576698