You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2020/06/01 19:21:14 UTC

[trafficserver] branch 9.0.x updated: Drastically improve generator.so performance for /nocache (#6834)

This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 07ee8c7  Drastically improve generator.so performance for /nocache (#6834)
07ee8c7 is described below

commit 07ee8c71b54639dddd1c1350bb41ef5768bef5c0
Author: Leif Hedstrom <zw...@apache.org>
AuthorDate: Mon Jun 1 13:19:59 2020 -0600

    Drastically improve generator.so performance for /nocache (#6834)
    
    (cherry picked from commit d7345f675012c04e4d7ec36b4eaf6fc825bdf7f8)
---
 plugins/generator/generator.cc | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/plugins/generator/generator.cc b/plugins/generator/generator.cc
index 0401fc7..751a253 100644
--- a/plugins/generator/generator.cc
+++ b/plugins/generator/generator.cc
@@ -594,6 +594,21 @@ GeneratorInterceptHook(TSCont contp, TSEvent event, void *edata)
   }
 }
 
+// Little helper function, to turn off the cache on requests which aren't cacheable to begin with.
+// This helps performance, a lot.
+static void
+CheckCacheable(TSHttpTxn txnp, TSMLoc url, TSMBuffer bufp)
+{
+  int pathsz       = 0;
+  const char *path = TSUrlPathGet(bufp, url, &pathsz);
+
+  if (path && (pathsz >= 8) && (0 == memcmp(path, "nocache/", 8))) {
+    // It's not cacheable, so, turn off the cache. This avoids major serialization and performance issues.
+    VDEBUG("turning off the cache, uncacehable");
+    TSHttpTxnConfigIntSet(txnp, TS_CONFIG_HTTP_CACHE_HTTP, 0);
+  }
+}
+
 // Handle events that occur on the TSHttpTxn.
 static int
 GeneratorTxnHook(TSCont contp, TSEvent event, void *edata)
@@ -603,6 +618,19 @@ GeneratorTxnHook(TSCont contp, TSEvent event, void *edata)
   VDEBUG("contp=%p, event=%s (%d), edata=%p", contp, TSHttpEventNameLookup(event), event, edata);
 
   switch (event) {
+  case TS_EVENT_HTTP_READ_REQUEST_HDR: {
+    TSMBuffer recp;
+    TSMLoc url_loc;
+    TSMLoc hdr_loc;
+
+    TSReleaseAssert(TSHttpTxnClientReqGet(arg.txn, &recp, &hdr_loc) == TS_SUCCESS);
+    TSReleaseAssert(TSHttpHdrUrlGet(recp, hdr_loc, &url_loc) == TS_SUCCESS);
+    CheckCacheable(arg.txn, url_loc, recp);
+    TSHandleMLocRelease(recp, hdr_loc, url_loc);
+    TSHandleMLocRelease(recp, TS_NULL_MLOC, hdr_loc);
+    break;
+  }
+
   case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE: {
     int status;
 
@@ -612,7 +640,6 @@ GeneratorTxnHook(TSCont contp, TSEvent event, void *edata)
       VDEBUG("intercepting origin server request for txn=%p", arg.txn);
       TSHttpTxnServerIntercept(TSContCreate(GeneratorInterceptHook, TSMutexCreate()), arg.txn);
     }
-
     break;
   }
 
@@ -656,6 +683,10 @@ TSPluginInit(int /* argc */, const char * /* argv */[])
 
   GeneratorInitialize();
 
+  // We want to check early on if the request is cacheable or not, and if it's not cacheable,
+  // we benefit signifciantly from turning off the cache completely.
+  TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TxnHook);
+
   // Wait until after the cache lookup to decide whether to
   // intercept a request. For cache hits we will never intercept.
   TSHttpHookAdd(TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, TxnHook);
@@ -669,8 +700,10 @@ TSRemapInit(TSRemapInterface * /* api_info */, char * /* errbuf */, int /* errbu
 }
 
 TSRemapStatus
-TSRemapDoRemap(void * /* ih */, TSHttpTxn txn, TSRemapRequestInfo * /* rri ATS_UNUSED */)
+TSRemapDoRemap(void * /* ih */, TSHttpTxn txn, TSRemapRequestInfo *rri)
 {
+  // Check if we should turn off the cache before doing anything else ...
+  CheckCacheable(txn, rri->requestUrl, rri->requestBufp);
   TSHttpTxnHookAdd(txn, TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, TxnHook);
   return TSREMAP_NO_REMAP; // This plugin never rewrites anything.
 }