~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/net/instaweb/http/public/cache_url_async_fetcher.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Author: sligocki@google.com (Shawn Ligocki)
18
 
 
19
 
#ifndef NET_INSTAWEB_HTTP_PUBLIC_CACHE_URL_ASYNC_FETCHER_H_
20
 
#define NET_INSTAWEB_HTTP_PUBLIC_CACHE_URL_ASYNC_FETCHER_H_
21
 
 
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"
25
 
 
26
 
namespace net_instaweb {
27
 
 
28
 
class AsyncFetch;
29
 
class Hasher;
30
 
class Histogram;
31
 
class HTTPCache;
32
 
class MessageHandler;
33
 
class NamedLockManager;
34
 
class Variable;
35
 
 
36
 
// Composes an asynchronous URL fetcher with an http cache, to
37
 
// generate an asynchronous caching URL fetcher.
38
 
//
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.
45
 
//
46
 
// If fetcher == NULL, this will only perform a cache lookup and then call
47
 
// the callback immediately.
48
 
//
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.
54
 
//
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 {
58
 
 public:
59
 
  // Interface for managing async operations in CacheUrlAsyncFetcher. It helps
60
 
  // to protect the lifetime of the injected objects.
61
 
  class AsyncOpHooks {
62
 
   public:
63
 
    AsyncOpHooks() {}
64
 
    virtual ~AsyncOpHooks();
65
 
 
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;
70
 
  };
71
 
 
72
 
  // None of these are owned by CacheUrlAsyncFetcher.
73
 
  CacheUrlAsyncFetcher(const Hasher* lock_hasher,
74
 
                       NamedLockManager* lock_manager,
75
 
                       HTTPCache* cache,
76
 
                       const GoogleString& fragment,
77
 
                       AsyncOpHooks* async_op_hooks,
78
 
                       UrlAsyncFetcher* fetcher)
79
 
      : lock_hasher_(lock_hasher),
80
 
        lock_manager_(lock_manager),
81
 
        http_cache_(cache),
82
 
        fragment_(fragment),
83
 
        fetcher_(fetcher),
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),
90
 
        respect_vary_(false),
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) {
96
 
  }
97
 
  virtual ~CacheUrlAsyncFetcher();
98
 
 
99
 
  virtual bool SupportsHttps() const { return fetcher_->SupportsHttps(); }
100
 
 
101
 
  virtual void Fetch(const GoogleString& url,
102
 
                     MessageHandler* message_handler,
103
 
                     AsyncFetch* base_fetch);
104
 
 
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;
108
 
 
109
 
  HTTPCache* http_cache() const { return http_cache_; }
110
 
  UrlAsyncFetcher* fetcher() const { return fetcher_; }
111
 
 
112
 
  void set_backend_first_byte_latency_histogram(Histogram* x) {
113
 
    backend_first_byte_latency_ = x;
114
 
  }
115
 
 
116
 
  Histogram* backend_first_byte_latency_histogram() const {
117
 
    return backend_first_byte_latency_;
118
 
  }
119
 
 
120
 
  void set_fallback_responses_served(Variable* x) {
121
 
    fallback_responses_served_ = x;
122
 
  }
123
 
 
124
 
  Variable* fallback_responses_served() const {
125
 
    return fallback_responses_served_;
126
 
  }
127
 
 
128
 
  void set_fallback_responses_served_while_revalidate(Variable* x) {
129
 
    fallback_responses_served_while_revalidate_ = x;
130
 
  }
131
 
 
132
 
  Variable* fallback_responses_served_while_revalidate() const {
133
 
    return fallback_responses_served_while_revalidate_;
134
 
  }
135
 
 
136
 
  void set_num_conditional_refreshes(Variable* x) {
137
 
    num_conditional_refreshes_ = x;
138
 
  }
139
 
 
140
 
  Variable* num_conditional_refreshes() const {
141
 
    return num_conditional_refreshes_;
142
 
  }
143
 
 
144
 
  void set_num_proactively_freshen_user_facing_request(Variable* x) {
145
 
    num_proactively_freshen_user_facing_request_ = x;
146
 
  }
147
 
 
148
 
  Variable* num_proactively_freshen_user_facing_request() const {
149
 
    return num_proactively_freshen_user_facing_request_;
150
 
  }
151
 
 
152
 
  void set_respect_vary(bool x) { respect_vary_ = x; }
153
 
  bool respect_vary() const { return respect_vary_; }
154
 
 
155
 
  void set_ignore_recent_fetch_failed(bool x) {
156
 
    ignore_recent_fetch_failed_ = x;
157
 
  }
158
 
  bool ignore_recent_fetch_failed() const {
159
 
    return ignore_recent_fetch_failed_;
160
 
  }
161
 
 
162
 
  void set_serve_stale_if_fetch_error(bool x) {
163
 
    serve_stale_if_fetch_error_ = x;
164
 
  }
165
 
 
166
 
  bool serve_stale_if_fetch_error() const {
167
 
    return serve_stale_if_fetch_error_;
168
 
  }
169
 
 
170
 
  void set_serve_stale_while_revalidate_threshold_sec(int64 x) {
171
 
    serve_stale_while_revalidate_threshold_sec_ = x;
172
 
  }
173
 
 
174
 
  int64 serve_stale_while_revalidate_threshold_sec() const {
175
 
    return serve_stale_while_revalidate_threshold_sec_;
176
 
  }
177
 
 
178
 
  void set_default_cache_html(bool x) { default_cache_html_ = x; }
179
 
  bool default_cache_html() const { return default_cache_html_; }
180
 
 
181
 
  void set_proactively_freshen_user_facing_request(bool x) {
182
 
    proactively_freshen_user_facing_request_ = x;
183
 
  }
184
 
  bool proactively_freshen_user_facing_request() const {
185
 
    return proactively_freshen_user_facing_request_;
186
 
  }
187
 
 
188
 
 private:
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_;
196
 
 
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.
202
 
 
203
 
  bool respect_vary_;
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_;
209
 
 
210
 
  DISALLOW_COPY_AND_ASSIGN(CacheUrlAsyncFetcher);
211
 
};
212
 
 
213
 
}  // namespace net_instaweb
214
 
 
215
 
#endif  // NET_INSTAWEB_HTTP_PUBLIC_CACHE_URL_ASYNC_FETCHER_H_