2
* Copyright 2011 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: sligocki@google.com (Shawn Ligocki)
19
#ifndef NET_INSTAWEB_HTTP_PUBLIC_CACHE_URL_ASYNC_FETCHER_H_
20
#define NET_INSTAWEB_HTTP_PUBLIC_CACHE_URL_ASYNC_FETCHER_H_
22
#include "net/instaweb/http/public/url_async_fetcher.h"
23
#include "net/instaweb/util/public/basictypes.h"
24
#include "net/instaweb/util/public/string.h"
26
namespace net_instaweb {
33
class NamedLockManager;
36
// Composes an asynchronous URL fetcher with an http cache, to
37
// generate an asynchronous caching URL fetcher.
39
// This fetcher will asynchronously check the cache. If the url
40
// is found in cache and is still valid, the fetch's callback will be
41
// called right away. Otherwise (if fetcher != NULL) an async fetch
42
// will be performed in the fetcher, the result of which will be written
43
// into the cache. In case the fetch fails and there is a stale response
44
// in the cache, we serve the stale response.
46
// If fetcher == NULL, this will only perform a cache lookup and then call
47
// the callback immediately.
49
// In case of cache hit and resource is about to expire (80% of TTL or 5 mins
50
// which ever is minimum), it will trigger background fetch to freshen the value
51
// in cache. Background fetch only be triggered only if async_op_hooks_ != NULL,
52
// otherwise, fetcher object accessed by BackgroundFreshenFetch may be deleted
53
// by the time origin fetch finishes.
55
// TODO(sligocki): In order to use this for fetching resources for rewriting
56
// we'd need to integrate resource locking in this class. Do we want that?
57
class CacheUrlAsyncFetcher : public UrlAsyncFetcher {
59
// Interface for managing async operations in CacheUrlAsyncFetcher. It helps
60
// to protect the lifetime of the injected objects.
64
virtual ~AsyncOpHooks();
66
// Called when CacheUrlAsyncFetcher is about to start async operation.
67
virtual void StartAsyncOp() = 0;
68
// Called when async operation is ended.
69
virtual void FinishAsyncOp() = 0;
72
// None of these are owned by CacheUrlAsyncFetcher.
73
CacheUrlAsyncFetcher(const Hasher* lock_hasher,
74
NamedLockManager* lock_manager,
76
const GoogleString& fragment,
77
AsyncOpHooks* async_op_hooks,
78
UrlAsyncFetcher* fetcher)
79
: lock_hasher_(lock_hasher),
80
lock_manager_(lock_manager),
84
async_op_hooks_(async_op_hooks),
85
backend_first_byte_latency_(NULL),
86
fallback_responses_served_(NULL),
87
fallback_responses_served_while_revalidate_(NULL),
88
num_conditional_refreshes_(NULL),
89
num_proactively_freshen_user_facing_request_(NULL),
91
ignore_recent_fetch_failed_(false),
92
serve_stale_if_fetch_error_(false),
93
default_cache_html_(false),
94
proactively_freshen_user_facing_request_(false),
95
serve_stale_while_revalidate_threshold_sec_(0) {
97
virtual ~CacheUrlAsyncFetcher();
99
virtual bool SupportsHttps() const { return fetcher_->SupportsHttps(); }
101
virtual void Fetch(const GoogleString& url,
102
MessageHandler* message_handler,
103
AsyncFetch* base_fetch);
105
// HTTP status code used to indicate that we failed the Fetch because
106
// result was not found in cache. (Only happens if fetcher_ == NULL).
107
static const int kNotInCacheStatus;
109
HTTPCache* http_cache() const { return http_cache_; }
110
UrlAsyncFetcher* fetcher() const { return fetcher_; }
112
void set_backend_first_byte_latency_histogram(Histogram* x) {
113
backend_first_byte_latency_ = x;
116
Histogram* backend_first_byte_latency_histogram() const {
117
return backend_first_byte_latency_;
120
void set_fallback_responses_served(Variable* x) {
121
fallback_responses_served_ = x;
124
Variable* fallback_responses_served() const {
125
return fallback_responses_served_;
128
void set_fallback_responses_served_while_revalidate(Variable* x) {
129
fallback_responses_served_while_revalidate_ = x;
132
Variable* fallback_responses_served_while_revalidate() const {
133
return fallback_responses_served_while_revalidate_;
136
void set_num_conditional_refreshes(Variable* x) {
137
num_conditional_refreshes_ = x;
140
Variable* num_conditional_refreshes() const {
141
return num_conditional_refreshes_;
144
void set_num_proactively_freshen_user_facing_request(Variable* x) {
145
num_proactively_freshen_user_facing_request_ = x;
148
Variable* num_proactively_freshen_user_facing_request() const {
149
return num_proactively_freshen_user_facing_request_;
152
void set_respect_vary(bool x) { respect_vary_ = x; }
153
bool respect_vary() const { return respect_vary_; }
155
void set_ignore_recent_fetch_failed(bool x) {
156
ignore_recent_fetch_failed_ = x;
158
bool ignore_recent_fetch_failed() const {
159
return ignore_recent_fetch_failed_;
162
void set_serve_stale_if_fetch_error(bool x) {
163
serve_stale_if_fetch_error_ = x;
166
bool serve_stale_if_fetch_error() const {
167
return serve_stale_if_fetch_error_;
170
void set_serve_stale_while_revalidate_threshold_sec(int64 x) {
171
serve_stale_while_revalidate_threshold_sec_ = x;
174
int64 serve_stale_while_revalidate_threshold_sec() const {
175
return serve_stale_while_revalidate_threshold_sec_;
178
void set_default_cache_html(bool x) { default_cache_html_ = x; }
179
bool default_cache_html() const { return default_cache_html_; }
181
void set_proactively_freshen_user_facing_request(bool x) {
182
proactively_freshen_user_facing_request_ = x;
184
bool proactively_freshen_user_facing_request() const {
185
return proactively_freshen_user_facing_request_;
189
// Not owned by CacheUrlAsyncFetcher.
190
const Hasher* lock_hasher_;
191
NamedLockManager* lock_manager_;
192
HTTPCache* http_cache_;
193
GoogleString fragment_;
194
UrlAsyncFetcher* fetcher_; // may be NULL.
195
AsyncOpHooks* async_op_hooks_;
197
Histogram* backend_first_byte_latency_; // may be NULL.
198
Variable* fallback_responses_served_; // may be NULL.
199
Variable* fallback_responses_served_while_revalidate_; // may be NULL.
200
Variable* num_conditional_refreshes_; // may be NULL.
201
Variable* num_proactively_freshen_user_facing_request_; // may be NULL.
204
bool ignore_recent_fetch_failed_;
205
bool serve_stale_if_fetch_error_;
206
bool default_cache_html_;
207
bool proactively_freshen_user_facing_request_;
208
int64 serve_stale_while_revalidate_threshold_sec_;
210
DISALLOW_COPY_AND_ASSIGN(CacheUrlAsyncFetcher);
213
} // namespace net_instaweb
215
#endif // NET_INSTAWEB_HTTP_PUBLIC_CACHE_URL_ASYNC_FETCHER_H_