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

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: Rebased till refs/heads/master@{#475981} 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 #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.
Randy Smith (Not in Mondays) 2017/06/06 12:54:58 I think of comments as being directions to the con
shivanisha 2017/06/07 15:19:19 done
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|.
Randy Smith (Not in Mondays) 2017/06/06 12:54:58 nit, suggestion: I think there's a bit of implemen
shivanisha 2017/06/07 15:19:19 done
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.
Randy Smith (Not in Mondays) 2017/06/06 12:54:58 This leaves me a little confused as to whether it'
shivanisha 2017/06/07 15:19:19 Rephrased
54 void AddTransaction(Transaction* transaction,
55 std::unique_ptr<HttpTransaction> network_transaction);
56
57 // Removes a transaction.
58 void RemoveTransaction(Transaction* transaction);
59
60 // Invoked when there is a change in a member transaction's priority or a
61 // member transaction is removed.
62 void PriorityChanged();
63
64 // Returns true if this object is empty.
65 bool IsEmpty() const { return all_writers_.empty(); }
66
67 // Returns true is |transaction| is part of writers.
68 bool IsPresent(Transaction* transaction) const {
69 return all_writers_.count(transaction) > 0;
70 }
71
72 // Returns true if |this| only contains idle writers.
73 bool ContainsOnlyIdleWriters() const;
74
75 // Move any idle writers to entry_->readers. Should only be invoked when a
76 // response is completely written and when ContainesOnlyIdleWriters()
77 // returns true.
78 void MoveIdleWritersToReaders();
79
80 // Makes writing an exclusine operation implying that Writers can contain at
81 // most one transaction for the complete reading/writing of the response body.
82 void SetExclusive();
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 from HttpCache when it is notified of a transaction failing to
90 // write. |error| indicates network read error code or cache write error.
91 void ProcessFailure(Transaction* transaction, int error);
92
93 // Invoked to mark an entry as truncated. Should be invoked when reading is
94 // not currently in progress.
Randy Smith (Not in Mondays) 2017/06/06 12:54:58 How will the caller know that reading is not curre
shivanisha 2017/06/07 15:19:19 As a parallel with HC::T states this is not equiva
95 void TruncateEntry();
96
97 LoadState GetWriterLoadState();
Randy Smith (Not in Mondays) 2017/06/06 12:54:58 This currently requires a network transaction to b
shivanisha 2017/06/07 15:19:19 Done.
98
99 // For testing.
100
101 int CountTransactionsForTesting() const { return all_writers_.size(); }
102 bool IsTruncatedForTesting() const { return truncated_; }
103
104 private:
105 enum class State {
106 NONE,
107 NETWORK_READ,
108 NETWORK_READ_COMPLETE,
109 CACHE_WRITE_DATA,
110 CACHE_WRITE_DATA_COMPLETE,
111 CACHE_WRITE_TRUNCATED_RESPONSE,
112 CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
113 };
114
115 // These transactions are waiting on Read. After the active transaction
116 // completes writing the data to the cache, their buffer would be filled with
117 // the data and their callback will be invoked.
118 struct WaitingForRead {
119 Transaction* transaction;
120 scoped_refptr<IOBuffer> read_buf;
121 int read_buf_len;
122 int write_len;
123 const CompletionCallback callback;
124 WaitingForRead(Transaction* transaction,
125 scoped_refptr<IOBuffer> read_buf,
126 int len,
127 const CompletionCallback& consumer_callback);
128 ~WaitingForRead();
129 WaitingForRead(const WaitingForRead&);
130 };
131 using WaitingForReadList = std::list<WaitingForRead>;
132
133 // Runs the state transition loop. Resets and calls |callback_| on exit,
134 // unless the return value is ERR_IO_PENDING.
135 int DoLoop(int result);
136
137 // State machine functions.
138 int DoNetworkRead();
139 int DoNetworkReadComplete(int result);
140 int DoCacheWriteData(int num_bytes);
141 int DoCacheWriteDataComplete(int result);
142 int DoCacheWriteTruncatedResponse();
143 int DoCacheWriteTruncatedResponseComplete(int result);
144
145 // Helper functions for callback.
146
147 void OnNetworkReadFailure(int result);
148 void OnCacheWriteFailure();
149 void OnDataReceived(int result);
150
151 // Helper function for writing to cache.
152 int WriteToEntry(int index,
153 int offset,
154 IOBuffer* data,
155 int data_len,
156 const CompletionCallback& callback);
157
158 // Notifies the transactions waiting on Read of the result, by posting a task
159 // for each of them.
160 void ProcessWaitingForReadTransactions(int result);
161
162 // Sets the state of idle writers so that they can fail any subsequent
163 // Read.
164 void SetIdleWritersFailState(int result);
165
166 // Called to reset state when all transaction references are removed from
167 // |this|.
168 void ResetStateForEmptyWriters();
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_ = nullptr;
180
181 // Owner of this object.
182 ActiveEntry* entry_ = nullptr;
183
184 std::unique_ptr<HttpTransaction> network_transaction_ = nullptr;
185
186 scoped_refptr<IOBuffer> read_buf_ = nullptr;
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_; // Callback for active_transaction_.
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

Powered by Google App Engine
This is Rietveld 408576698