You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bc...@apache.org on 2017/07/20 08:22:55 UTC
[trafficserver] branch master updated: TS-4523 - Add pause
functionality to the TransformationPlugin interface.
This is an automated email from the ASF dual-hosted git repository.
bcall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new fb346e1 TS-4523 - Add pause functionality to the TransformationPlugin interface.
fb346e1 is described below
commit fb346e1397c9e5121eabb9603bdd3f679d70562b
Author: David Ben Zakai <da...@gmail.com>
AuthorDate: Sat Jun 10 11:34:32 2017 +0300
TS-4523 - Add pause functionality to the TransformationPlugin interface.
---
example/Makefile.am | 3 +
.../DelayTransformationPlugin.cc | 122 +++++++++++++++++++++
lib/cppapi/TransformationPlugin.cc | 44 +++++++-
.../include/atscppapi/TransformationPlugin.h | 11 ++
4 files changed, 179 insertions(+), 1 deletion(-)
diff --git a/example/Makefile.am b/example/Makefile.am
index c7d0f06..8cd7a49 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -67,6 +67,7 @@ example_Plugins += \
cppapi/ClientRequest.la \
cppapi/CustomErrorRemapPlugin.la \
cppapi/CustomResponse.la \
+ cppapi/DelayTransformationPlugin.la \
cppapi/GlobalHookPlugin.la \
cppapi/GzipTransformationPlugin.la \
cppapi/HelloWorldPlugin.la \
@@ -138,6 +139,7 @@ cppapi_ClientRedirect_la_SOURCES = cppapi/clientredirect/ClientRedirect.cc
cppapi_ClientRequest_la_SOURCES = cppapi/clientrequest/ClientRequest.cc
cppapi_CustomErrorRemapPlugin_la_SOURCES = cppapi/custom_error_remap_plugin/CustomErrorRemapPlugin.cc
cppapi_CustomResponse_la_SOURCES = cppapi/customresponse/CustomResponse.cc
+cppapi_DelayTransformationPlugin_la_SOURCES = cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc
cppapi_GlobalHookPlugin_la_SOURCES = cppapi/globalhook/GlobalHookPlugin.cc
cppapi_GzipTransformationPlugin_la_SOURCES = cppapi/gzip_transformation/GzipTransformationPlugin.cc
cppapi_HelloWorldPlugin_la_SOURCES = cppapi/helloworld/HelloWorldPlugin.cc
@@ -162,6 +164,7 @@ cppapi_ClientRedirect_la_LIBADD = $(libatscppapi)
cppapi_ClientRequest_la_LIBADD = $(libatscppapi)
cppapi_CustomErrorRemapPlugin_la_LIBADD = $(libatscppapi)
cppapi_CustomResponse_la_LIBADD = $(libatscppapi)
+cppapi_DelayTransformationPlugin_la_LIBADD = $(libatscppapi)
cppapi_GlobalHookPlugin_la_LIBADD = $(libatscppapi)
cppapi_GzipTransformationPlugin_la_LIBADD = $(libatscppapi)
cppapi_HelloWorldPlugin_la_LIBADD = $(libatscppapi)
diff --git a/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc b/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc
new file mode 100644
index 0000000..bd83445
--- /dev/null
+++ b/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc
@@ -0,0 +1,122 @@
+/**
+ 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 <atscppapi/GlobalPlugin.h>
+#include <atscppapi/TransactionPlugin.h>
+#include <atscppapi/TransformationPlugin.h>
+#include <atscppapi/PluginInit.h>
+#include <atscppapi/Logger.h>
+
+#include "ts/ts.h"
+
+using namespace atscppapi;
+using std::string;
+
+/*
+ * This example demonstrates how you can pause a transformation and resume it
+ * after doing some work simply by calling the pause method.
+ * To resume just schedule the return value of pause method (cont)
+ */
+
+namespace
+{
+#define TAG "delay_transformation"
+GlobalPlugin *plugin;
+}
+
+class DelayTransformationPlugin : public TransformationPlugin
+{
+public:
+ DelayTransformationPlugin(Transaction &transaction, TransformationPlugin::Type xformType)
+ : TransformationPlugin(transaction, xformType)
+ {
+ registerHook((xformType == TransformationPlugin::REQUEST_TRANSFORMATION) ? HOOK_SEND_REQUEST_HEADERS :
+ HOOK_SEND_RESPONSE_HEADERS);
+ }
+
+ void
+ handleSendRequestHeaders(Transaction &transaction)
+ {
+ transaction.getServerRequest().getHeaders()["X-Content-Delayed"] = "1";
+ transaction.resume();
+ }
+
+ void
+ handleSendResponseHeaders(Transaction &transaction)
+ {
+ transaction.getClientResponse().getHeaders()["X-Content-Delayed"] = "1";
+ transaction.resume();
+ }
+
+ void
+ consume(const string &data)
+ {
+ TS_DEBUG(TAG, "Consuming...");
+ produce(data);
+
+ TS_DEBUG(TAG, "Pausing...");
+ TSCont cont = pause();
+
+ if (cont) {
+ TS_DEBUG(TAG, "Resuming in 2ms...");
+ TSContSchedule(cont, 2, TS_THREAD_POOL_NET);
+ }
+ }
+
+ void
+ handleInputComplete()
+ {
+ TS_DEBUG(TAG, "handleInputComplete");
+ setOutputComplete();
+ }
+
+ virtual ~DelayTransformationPlugin() {}
+};
+
+class GlobalHookPlugin : public GlobalPlugin
+{
+public:
+ GlobalHookPlugin()
+ {
+ registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP);
+ registerHook(HOOK_READ_RESPONSE_HEADERS);
+ }
+
+ virtual void
+ handleReadRequestHeadersPostRemap(Transaction &transaction)
+ {
+ transaction.addPlugin(new DelayTransformationPlugin(transaction, TransformationPlugin::REQUEST_TRANSFORMATION));
+ transaction.resume();
+ }
+
+ virtual void
+ handleReadResponseHeaders(Transaction &transaction)
+ {
+ transaction.addPlugin(new DelayTransformationPlugin(transaction, TransformationPlugin::RESPONSE_TRANSFORMATION));
+ transaction.resume();
+ }
+};
+
+void
+TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED)
+{
+ RegisterGlobalPlugin("CPP_Example_DelayTransformation", "apache", "dev@trafficserver.apache.org");
+ TS_DEBUG(TAG, "TSPluginInit");
+ plugin = new GlobalHookPlugin();
+}
diff --git a/lib/cppapi/TransformationPlugin.cc b/lib/cppapi/TransformationPlugin.cc
index 7fc502d..fa9c6b9 100644
--- a/lib/cppapi/TransformationPlugin.cc
+++ b/lib/cppapi/TransformationPlugin.cc
@@ -49,6 +49,8 @@ struct atscppapi::TransformationPluginState : noncopyable {
TSIOBuffer output_buffer_;
TSIOBufferReader output_buffer_reader_;
int64_t bytes_written_;
+ bool paused_;
+ TSCont resume_cont_;
// We can only send a single WRITE_COMPLETE even though
// we may receive an immediate event after we've sent a
@@ -69,6 +71,8 @@ struct atscppapi::TransformationPluginState : noncopyable {
output_buffer_(nullptr),
output_buffer_reader_(nullptr),
bytes_written_(0),
+ paused_(false),
+ resume_cont_(nullptr),
input_complete_dispatched_(false)
{
output_buffer_ = TSIOBufferCreate();
@@ -86,6 +90,11 @@ struct atscppapi::TransformationPluginState : noncopyable {
TSIOBufferDestroy(output_buffer_);
output_buffer_ = nullptr;
}
+
+ // Cleanup pending cont
+ if (resume_cont_) {
+ TSContDataSet(resume_cont_, nullptr);
+ }
}
};
@@ -106,6 +115,11 @@ handleTransformationPluginRead(TSCont contp, TransformationPluginState *state)
// is actually the vio we read from.
TSVIO write_vio = TSVConnWriteVIOGet(contp);
if (write_vio) {
+ if (state->paused_) {
+ LOG_DEBUG("Transformation contp=%p write_vio=%p, is paused", contp, write_vio);
+ return 0;
+ }
+
int64_t to_read = TSVIONTodoGet(write_vio);
LOG_DEBUG("Transformation contp=%p write_vio=%p, to_read=%" PRId64, contp, write_vio, to_read);
@@ -242,7 +256,7 @@ handleTransformationPluginEvents(TSCont contp, TSEvent event, void *edata)
} /* anonymous namespace */
TransformationPlugin::TransformationPlugin(Transaction &transaction, TransformationPlugin::Type type)
- : TransactionPlugin(transaction)
+ : TransactionPlugin(transaction), state_(nullptr)
{
state_ = new TransformationPluginState(transaction, *this, type, static_cast<TSHttpTxn>(transaction.getAtsHandle()));
state_->vconn_ = TSTransformCreate(handleTransformationPluginEvents, state_->txn_);
@@ -259,6 +273,34 @@ TransformationPlugin::~TransformationPlugin()
delete state_;
}
+TSCont
+TransformationPlugin::pause()
+{
+ if (state_->input_complete_dispatched_) {
+ LOG_ERROR("Can not pause transformation (transformation completed) TransformationPlugin=%p (vconn)contp=%p tshttptxn=%p", this,
+ state_->vconn_, state_->txn_);
+ return nullptr;
+ } else {
+ state_->paused_ = true;
+ state_->resume_cont_ = TSContCreate(&resumeCallback, TSContMutexGet(reinterpret_cast<TSCont>(state_->txn_)));
+ TSContDataSet(state_->resume_cont_, static_cast<void *>(state_));
+ return state_->resume_cont_;
+ }
+}
+
+int
+TransformationPlugin::resumeCallback(TSCont cont, TSEvent event, void *edata)
+{
+ auto state = static_cast<TransformationPluginState *>(TSContDataGet(cont));
+ if (state) {
+ state->paused_ = false;
+ handleTransformationPluginRead(state->vconn_, state);
+ }
+
+ TSContDestroy(cont);
+ return TS_SUCCESS;
+}
+
size_t
TransformationPlugin::doProduce(const std::string &data)
{
diff --git a/lib/cppapi/include/atscppapi/TransformationPlugin.h b/lib/cppapi/include/atscppapi/TransformationPlugin.h
index 8073375..714d8e2 100644
--- a/lib/cppapi/include/atscppapi/TransformationPlugin.h
+++ b/lib/cppapi/include/atscppapi/TransformationPlugin.h
@@ -98,6 +98,16 @@ public:
virtual void consume(const std::string &data) = 0;
/**
+ * Call this method if you wish to pause the transformation.
+ * Schedule the return value continuation to resume the transforamtion.
+ * If the continuation is scheduled and called after the transform is destroyed it
+ * won't do anything beyond cleanups.
+ * Note: You must schedule the continuation or destroy it (using TSContDestroy) yourself,
+ * otherwise it will leak.
+ */
+ TSCont pause();
+
+ /**
* A method that you must implement when writing a TransformationPlugin, this method
* will be fired whenever the upstream TransformationPlugin has completed writing data.
*/
@@ -125,6 +135,7 @@ protected:
private:
TransformationPluginState *state_; /** Internal state for a TransformationPlugin */
size_t doProduce(const std::string &);
+ static int resumeCallback(TSCont cont, TSEvent event, void *edata); /** Resume callback*/
};
} /* atscppapi */
--
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>'].