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

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: AddTransaction to take unique_ptr of network transaction. 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
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 NET_EXPORT_PRIVATE 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 and if its the first transaction
48 // added, transfers the ownership of the network transaction to Writers.
49 // Should only be invoked if CanAddTransaction() returns true. If
50 // |network_transaction| is non-null, it will be assigned to
51 // |network_transaction_|. The first transaction added should definitely pass
52 // a non-null |network_transaction|. |transaction| can be destroyed at any
53 // point and it should invoke RemoveTransaction() during its destruction.
54 void AddTransaction(Transaction* transaction,
55 std::unique_ptr<HttpTransaction> network_transaction);
56
57 // Removes a transaction.
58 void RemoveTransaction(Transaction* transaction);
59
60 // Removes all transactions.
61 void RemoveAllTransactions();
62
63 // Invoked when there is a change in a member transaction's priority or a
64 // member transaction is removed.
65 void PriorityChanged();
66
67 // Returns true if this object is empty.
68 bool IsEmpty() const { return all_writers_.empty(); }
69
70 // Returns true is |transaction| is part of writers.
71 bool IsPresent(Transaction* transaction) const {
72 return all_writers_.count(transaction) > 0;
73 }
74
75 // When response is completely written, any idle writers are moved to
76 // entry_->readers.
77 void MoveIdleWritersToReaders();
78
79 // Makes writing an exclusine operation implying that Writers can contain at
80 // most one transaction for the complete reading/writing of the response body.
81 void SetExclusive() { is_exclusive_ = true; }
Randy Smith (Not in Mondays) 2017/05/18 01:02:56 What happens if this is called with a lot of trans
Randy Smith (Not in Mondays) 2017/05/22 01:56:55 Ping?
shivanisha 2017/05/31 19:21:26 Added a dcheck that only 0 or 1 transactions shoul
Randy Smith (Not in Mondays) 2017/06/06 12:54:57 SG. Just so I'm tracking, what's the use case for
shivanisha 2017/06/07 15:19:18 Actually, adding an is_exclusive argument to AddTr
82
83 // Returns true if more writers can be added for shared writing.
84 bool CanAddWriters() const;
85
86 HttpTransaction* network_transaction() { return network_transaction_.get(); }
87
88 // Invoked from HttpCache when it is notified of a transaction failing to
89 // write. |error| indicates network read error code or cache write error.
90 void ProcessFailure(Transaction* transaction, int error);
91
92 // Invoked to mark an entry as truncated. Should be invoked when reading is
93 // not currently in progress.
94 void TruncateEntry();
95
96 LoadState GetWriterLoadState();
97
98 // For testing.
99
100 int CountTransactionsForTesting() const { return all_writers_.size(); }
101 bool IsTruncatedForTesting() const { return truncated_; }
102
103 private:
104 enum class State {
105 NONE,
106 NETWORK_READ,
107 NETWORK_READ_COMPLETE,
108 CACHE_WRITE_DATA,
109 CACHE_WRITE_DATA_COMPLETE,
110 CACHE_WRITE_TRUNCATED_RESPONSE,
111 CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
112 };
113
114 // These transactions are waiting on Read. After the active transaction
115 // completes writing the data to the cache, their buffer would be filled with
116 // the data and their callback will be invoked.
117 struct WaitingForRead {
118 Transaction* transaction;
119 scoped_refptr<IOBuffer> read_buf;
120 int read_buf_len;
121 int write_len;
122 const CompletionCallback callback;
123 WaitingForRead(Transaction* transaction,
124 scoped_refptr<IOBuffer> read_buf,
125 int len,
126 const CompletionCallback& consumer_callback);
127 ~WaitingForRead();
128 WaitingForRead(const WaitingForRead&);
129 };
130 using WaitingForReadList = std::list<WaitingForRead>;
131
132 // Runs the state transition loop. Resets and calls |callback_| on exit,
133 // unless the return value is ERR_IO_PENDING.
134 int DoLoop(int result);
135
136 // State machine functions.
137 int DoNetworkRead();
138 int DoNetworkReadComplete(int result);
139 int DoCacheWriteData(int num_bytes);
140 int DoCacheWriteDataComplete(int result);
141 int DoCacheWriteTruncatedResponse();
142 int DoCacheWriteTruncatedResponseComplete(int result);
143
144 // Helper functions for callback.
145
146 void OnNetworkReadFailure(int result);
147 void OnCacheWriteFailure();
148 void OnDataReceived(int result);
149
150 // Helper function for writing to cache.
151 int WriteToEntry(int index,
152 int offset,
153 IOBuffer* data,
154 int data_len,
155 const CompletionCallback& callback);
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 of idle writers so that they can fail any subsequent
162 // Read.
163 void SetIdleWritersFailState(int result);
164
165 RequestPriority GetCurrentHighestPriority();
166
167 bool IsResponseCompleted();
168
169 int WriteResponseInfo(bool truncated);
170
171 // IO Completion callback function.
172 void OnIOComplete(int result);
173
174 State next_state_ = State::NONE;
175
176 // True if only reading from network and not writing to cache.
177 bool network_read_only_ = false;
178
179 // Http Cache.
180 HttpCache* cache_;
181
182 // Owner of this object.
183 ActiveEntry* entry_ = nullptr;
Randy Smith (Not in Mondays) 2017/05/18 01:02:55 My understanding is that Chromium prefers to keep
Randy Smith (Not in Mondays) 2017/05/22 01:56:56 Ping?
shivanisha 2017/05/31 19:21:26 Changed the header file such that all fields are i
184
185 std::unique_ptr<HttpTransaction> network_transaction_;
186
187 scoped_refptr<IOBuffer> read_buf_;
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.
204 TransactionSet all_writers_;
205
206 // True if multiple transactions are not allowed e.g. for partial requests.
207 bool is_exclusive_ = false;
208
209 // Current priority of the request. If a higher priority transaction is
210 // added, the priority of network transaction will be increased.
211 RequestPriority priority_ = MINIMUM_PRIORITY;
212
213 bool truncated_ = false; // used for testing.
214
215 CompletionCallback callback_; // Consumer's callback.
Randy Smith (Not in Mondays) 2017/05/18 01:02:55 Suggestion: Change comment to "Callback for active
Randy Smith (Not in Mondays) 2017/05/22 01:56:56 Ping?
shivanisha 2017/05/31 19:21:26 done
216 CompletionCallback io_callback_;
Randy Smith (Not in Mondays) 2017/05/18 01:02:56 Suggestion: In my ideal world this would be const,
Randy Smith (Not in Mondays) 2017/05/22 01:56:56 Ping?
shivanisha 2017/05/31 19:21:26 It seems nothing should come after WeakPtrFactory
Randy Smith (Not in Mondays) 2017/06/06 12:54:57 If you're doing the initialization at the call sit
shivanisha 2017/06/07 15:19:18 Removed the member variable.
217 base::WeakPtrFactory<Writers> weak_factory_;
218 DISALLOW_COPY_AND_ASSIGN(Writers);
219 };
220
221 } // namespace net
222 #endif // NET_HTTP_HTTP_CACHE_WRITERS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698