Page Speed Optimization Libraries  1.8.31.3
net/instaweb/http/public/request_context.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
00020 #define NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
00021 
00022 #include <set>
00023 
00024 #include "pagespeed/kernel/base/basictypes.h"
00025 #include "pagespeed/kernel/base/ref_counted_ptr.h"
00026 #include "pagespeed/kernel/base/scoped_ptr.h"
00027 #include "pagespeed/kernel/base/string.h"
00028 #include "pagespeed/kernel/base/string_util.h"
00029 
00030 namespace net_instaweb {
00031 
00032 class AbstractLogRecord;
00033 class AbstractMutex;
00034 class RequestContext;
00035 class RequestTrace;
00036 class ThreadSystem;
00037 class Timer;
00038 
00039 typedef RefCountedPtr<RequestContext> RequestContextPtr;
00040 
00048 class RequestContext : public RefCounted<RequestContext> {
00049  public:
00051   enum SplitRequestType {
00052     SPLIT_FULL,
00053     SPLIT_ABOVE_THE_FOLD,
00054     SPLIT_BELOW_THE_FOLD,
00055   };
00056 
00062   RequestContext(AbstractMutex* logging_mutex, Timer* timer);
00063 
00066   static RequestContextPtr NewTestRequestContext(ThreadSystem* thread_system) {
00067     return NewTestRequestContextWithTimer(thread_system, NULL);
00068   }
00069   static RequestContextPtr NewTestRequestContextWithTimer(
00070       ThreadSystem* thread_system, Timer* timer);
00071   static RequestContextPtr NewTestRequestContext(AbstractLogRecord* log_record);
00072 
00076   virtual AbstractLogRecord* NewSubordinateLogRecord(
00077       AbstractMutex* logging_mutex);
00078 
00083   RequestTrace* root_trace_context() { return root_trace_context_.get(); }
00085   void set_root_trace_context(RequestTrace* x);
00086 
00097   virtual RequestTrace* CreateDependentTraceContext(const StringPiece& label) {
00098     return NULL;
00099   }
00100 
00112   virtual void ReleaseDependentTraceContext(RequestTrace* t);
00113 
00115   virtual AbstractLogRecord* log_record();
00116 
00118   bool using_spdy() const { return using_spdy_; }
00119   void set_using_spdy(bool x) { using_spdy_ = x; }
00120 
00133   const GoogleString& minimal_private_suffix() const {
00134     return minimal_private_suffix_;
00135   }
00136   void set_minimal_private_suffix(StringPiece minimal_private_suffix) {
00137     minimal_private_suffix.CopyToString(&minimal_private_suffix_);
00138   }
00139 
00142   void set_accepts_webp(bool x) { accepts_webp_ = x; }
00143   bool accepts_webp() const { return accepts_webp_; }
00144 
00146   SplitRequestType split_request_type() const {
00147     return split_request_type_;
00148   }
00149   void set_split_request_type(SplitRequestType type) {
00150     split_request_type_ = type;
00151   }
00152 
00153   int64 request_id() const {
00154     return request_id_;
00155   }
00156   void set_request_id(int64 x) {
00157     request_id_ = x;
00158   }
00159 
00160   const GoogleString& sticky_query_parameters_token() const {
00161     return sticky_query_parameters_token_;
00162   }
00163   void set_sticky_query_parameters_token(StringPiece x) {
00164     x.CopyToString(&sticky_query_parameters_token_);
00165   }
00166 
00174   void AddSessionAuthorizedFetchOrigin(const GoogleString& origin) {
00175     session_authorized_fetch_origins_.insert(origin);
00176   }
00177 
00180   bool IsSessionAuthorizedFetchOrigin(const GoogleString& origin) const {
00181     return session_authorized_fetch_origins_.find(origin)
00182            != session_authorized_fetch_origins_.end();
00183   }
00184 
00188   void PrepareLogRecordForOutput();
00189 
00191   void WriteBackgroundRewriteLog();
00192 
00195   AbstractLogRecord* GetBackgroundRewriteLog(
00196       ThreadSystem* thread_system,
00197       bool log_urls,
00198       bool log_url_indices,
00199       int max_rewrite_info_log_size);
00200 
00215   class TimingInfo {
00216    public:
00220     TimingInfo(Timer* timer, AbstractMutex* mutex);
00221 
00225     void RequestStarted();
00226 
00229     void ProcessingStarted() { SetToNow(&processing_start_ts_ms_); }
00230 
00232     void ParsingStarted() { SetToNow(&parsing_start_ts_ms_); }
00233 
00235     void FirstByteReturned();
00236 
00238     void PropertyCacheLookupStarted() {
00239       SetToNow(&pcache_lookup_start_ts_ms_);
00240     }
00241 
00243     void PropertyCacheLookupFinished() { SetToNow(&pcache_lookup_end_ts_ms_); }
00244 
00247     void RequestFinished() { SetToNow(&end_ts_ms_); }
00248 
00254     void FetchStarted();
00255     void FetchHeaderReceived();
00256     void FetchFinished();
00257 
00260     void SetHTTPCacheLatencyMs(int64 latency_ms);
00261     void SetL2HTTPCacheLatencyMs(int64 latency_ms);
00262 
00264     int64 GetElapsedMs() const;
00265 
00267     bool GetTimeToStartProcessingMs(int64* elapsed_ms) const {
00268       return GetTimeFromStart(processing_start_ts_ms_, elapsed_ms);
00269     }
00270 
00275     bool GetProcessingElapsedMs(int64* elapsed_ms) const;
00276 
00278     bool GetTimeToPropertyCacheLookupStartMs(int64* elapsed_ms) const {
00279       return GetTimeFromStart(pcache_lookup_start_ts_ms_, elapsed_ms);
00280     }
00281 
00283     bool GetTimeToPropertyCacheLookupEndMs(int64* elapsed_ms) const {
00284       return GetTimeFromStart(pcache_lookup_end_ts_ms_, elapsed_ms);
00285     }
00286 
00288     bool GetHTTPCacheLatencyMs(int64* latency_ms) const;
00289     bool GetL2HTTPCacheLatencyMs(int64* latency_ms) const;
00290 
00292     bool GetTimeToStartFetchMs(int64* elapsed_ms) const;
00293 
00295     bool GetFetchHeaderLatencyMs(int64* latency_ms) const;
00296 
00298     bool GetFetchLatencyMs(int64* latency_ms) const;
00299 
00302     bool GetTimeToFirstByte(int64* latency_ms) const;
00303 
00305     bool GetTimeToStartParseMs(int64* elapsed_ms) const {
00306       return GetTimeFromStart(parsing_start_ts_ms_, elapsed_ms);
00307     }
00308 
00309     int64 init_ts_ms() const { return init_ts_ms_; }
00310 
00311     int64 start_ts_ms() const { return start_ts_ms_; }
00312 
00313    private:
00314     int64 NowMs() const;
00315 
00317     void SetToNow(int64* ts_ms) const;
00318 
00321     bool GetTimeFromStart(int64 ts_ms, int64* elapsed_ms) const;
00322 
00323 
00324     Timer* timer_;
00325 
00331     int64 init_ts_ms_;
00332     int64 start_ts_ms_;
00333     int64 processing_start_ts_ms_;
00334     int64 pcache_lookup_start_ts_ms_;
00335     int64 pcache_lookup_end_ts_ms_;
00336     int64 parsing_start_ts_ms_;
00337     int64 end_ts_ms_;
00338 
00339     AbstractMutex* mu_; 
00340 
00341     int64 fetch_start_ts_ms_;
00342     int64 fetch_header_ts_ms_;
00343     int64 fetch_end_ts_ms_;
00344     int64 first_byte_ts_ms_;
00345 
00347     int64 http_cache_latency_ms_;
00348     int64 l2http_cache_latency_ms_;
00349 
00350     DISALLOW_COPY_AND_ASSIGN(TimingInfo);
00351   };
00352 
00353   const TimingInfo& timing_info() const { return timing_info_; }
00354   TimingInfo* mutable_timing_info() { return &timing_info_; }
00355 
00356  protected:
00360   RequestContext(AbstractMutex* mutex, Timer* timer,
00361                  AbstractLogRecord* log_record);
00363   virtual ~RequestContext();
00364   REFCOUNT_FRIEND_DECLARATION(RequestContext);
00365 
00366  private:
00368   scoped_ptr<AbstractLogRecord> log_record_;
00369 
00370   TimingInfo timing_info_;
00371 
00373   scoped_ptr<RequestTrace> root_trace_context_;
00374 
00376   scoped_ptr<AbstractLogRecord> background_rewrite_log_record_;
00377 
00378   StringSet session_authorized_fetch_origins_;
00379 
00380   bool using_spdy_;
00381   bool accepts_webp_;
00382   GoogleString minimal_private_suffix_;
00383 
00384   SplitRequestType split_request_type_;
00385   int64 request_id_;
00386 
00389   GoogleString sticky_query_parameters_token_;
00390 
00391   DISALLOW_COPY_AND_ASSIGN(RequestContext);
00392 };
00393 
00394 }  
00395 
00396 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines