This is an automated email from the ASF dual-hosted git repository. amc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit f04a98b16f5dacdf3db66c9e50822ca851274986 Author: David Calavera <[email protected]> AuthorDate: Thu Sep 5 11:47:20 2019 -0700 Add example plugin to show how to use TSRedoCacheLookup. Signed-off-by: David Calavera <[email protected]> --- plugins/Makefile.am | 1 + .../experimental/redo_cache_lookup/Makefile.inc | 27 +++++ plugins/experimental/redo_cache_lookup/README.md | 11 ++ .../redo_cache_lookup/redo_cache_lookup.cc | 111 +++++++++++++++++++++ 4 files changed, 150 insertions(+) diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 9818f9c..0f41f43 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -71,6 +71,7 @@ include experimental/memcache/Makefile.inc include experimental/metalink/Makefile.inc include experimental/money_trace/Makefile.inc include experimental/mp4/Makefile.inc +include experimental/redo_cache_lookup/Makefile.inc include experimental/server_push_preload/Makefile.inc include experimental/slice/Makefile.inc include experimental/sslheaders/Makefile.inc diff --git a/plugins/experimental/redo_cache_lookup/Makefile.inc b/plugins/experimental/redo_cache_lookup/Makefile.inc new file mode 100644 index 0000000..316d6db --- /dev/null +++ b/plugins/experimental/redo_cache_lookup/Makefile.inc @@ -0,0 +1,27 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +pkglib_LTLIBRARIES += experimental/redo_cache_lookup/redo_cache_lookup.la + +experimental_redo_cache_lookup_redo_cache_lookup_la_SOURCES = \ + experimental/redo_cache_lookup/redo_cache_lookup.cc + +experimental_redo_cache_lookup_redo_cache_lookup_la_LDFLAGS = \ + $(AM_LDFLAGS) + +experimental_redo_cache_lookup_redo_cache_lookup_la_LIBADD = \ + $(top_builddir)/src/tscpp/api/libtscppapi.la + diff --git a/plugins/experimental/redo_cache_lookup/README.md b/plugins/experimental/redo_cache_lookup/README.md new file mode 100644 index 0000000..7ad5dda --- /dev/null +++ b/plugins/experimental/redo_cache_lookup/README.md @@ -0,0 +1,11 @@ +# Redo Cache Lookup Plugin + +This plugin shows how to use the experimental `TSHttpTxnRedoCacheLookup` api. It works by checking the cache for a fallback url if the cache lookup failed for any given url. + +## Configuration + +Add this plugin to `plugin.config` with the `--fallback` option: + +``` +redo_cache_lookup.so --fallback http://example.com/fallback_url +``` \ No newline at end of file diff --git a/plugins/experimental/redo_cache_lookup/redo_cache_lookup.cc b/plugins/experimental/redo_cache_lookup/redo_cache_lookup.cc new file mode 100644 index 0000000..b294309 --- /dev/null +++ b/plugins/experimental/redo_cache_lookup/redo_cache_lookup.cc @@ -0,0 +1,111 @@ +/** @file + + A plugin to redo cache lookups with a fallback if cache lookups fail for specific urls. + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#include <iostream> +#include <regex> +#include <set> +#include <sstream> +#include <getopt.h> + +#include <ts/ts.h> +#include <ts/experimental.h> +#include "tscpp/api/GlobalPlugin.h" +#include "tscpp/api/utils.h" + +#define PLUGIN_NAME "redo_cache_lookup" + +using namespace atscppapi; + +namespace +{ +GlobalPlugin *plugin; +} + +class RedoCacheLookupPlugin : public GlobalPlugin +{ +public: + RedoCacheLookupPlugin(const char *fallback) : fallback(fallback) + { + TSDebug(PLUGIN_NAME, "registering transaction hooks"); + RedoCacheLookupPlugin::registerHook(HOOK_CACHE_LOOKUP_COMPLETE); + } + + void + handleReadCacheLookupComplete(Transaction &transaction) override + { + Transaction::CacheStatus status = transaction.getCacheStatus(); + + if (status == Transaction::CacheStatus::CACHE_LOOKUP_NONE || status == Transaction::CacheStatus::CACHE_LOOKUP_SKIPED || + status == Transaction::CacheStatus::CACHE_LOOKUP_MISS) { + TSDebug(PLUGIN_NAME, "rewinding to check for fallback url: %s", fallback); + TSHttpTxn txnp = static_cast<TSHttpTxn>(transaction.getAtsHandle()); + TSHttpTxnRedoCacheLookup(txnp, fallback, strlen(fallback)); + } + + transaction.resume(); + } + +private: + const char *fallback; +}; + +void +TSPluginInit(int argc, const char *argv[]) +{ + TSDebug(PLUGIN_NAME, "Init"); + if (!RegisterGlobalPlugin("RedoCacheLookupPlugin", PLUGIN_NAME, "[email protected]")) { + return; + } + + const char *fallback = nullptr; + + // Read options from plugin.config + static const struct option longopts[] = {{"fallback", required_argument, nullptr, 'f'}}; + + int opt = 0; + + while (opt >= 0) { + opt = getopt_long(argc, const_cast<char *const *>(argv), "f:", longopts, nullptr); + switch (opt) { + case 'f': + fallback = optarg; + break; + case -1: + case '?': + break; + default: + TSDebug(PLUGIN_NAME, "Unexpected option: %i", opt); + TSError("[%s] Unexpected options error.", PLUGIN_NAME); + return; + } + } + + if (nullptr == fallback) { + TSDebug(PLUGIN_NAME, "Missing fallback option."); + TSError("[%s] Missing fallback option", PLUGIN_NAME); + return; + } + TSDebug(PLUGIN_NAME, "Initialized with fallback: %s", fallback); + + plugin = new RedoCacheLookupPlugin(fallback); +}
