This is an automated email from the ASF dual-hosted git repository. shuaijinchao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/apisix-python-plugin-runner.git
commit 11666cea4c7842416654142b52b92d45b0832455 Author: Janko <[email protected]> AuthorDate: Tue Aug 3 10:43:02 2021 +0800 feat: add socket handle module for runner. --- src/runner/socket/handle.py | 78 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/runner/socket/handle.py b/src/runner/socket/handle.py new file mode 100644 index 0000000..14942ce --- /dev/null +++ b/src/runner/socket/handle.py @@ -0,0 +1,78 @@ +# +# 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. +# +from a6pluginproto.Err import Resp as A6ErrResp +from a6pluginproto.Err import Code as A6ErrCode +import runner.plugin.cache as RunnerPluginCache +import runner.plugin.execute as RunnerPluginExecute +import runner.http.request as RunnerHttpRequest +import runner.http.response as RunnerHttpResponse +import runner.http.protocol as RunnerHttpProtocol + + +class Handle: + + def __init__(self, req_type, req_data): + self.req_type = req_type + self.req_data = req_data + + def RpcPrepareConf(self): + # init request + req = RunnerHttpRequest.Request(RunnerHttpProtocol.RPC_PREPARE_CONF, self.req_data) + # generate token + token = RunnerPluginCache.generateToken() + # get plugins config + configs = req.getConfigs() + # cache plugins config + RunnerPluginCache.setConfigByToken(token, configs) + # init response + reps = RunnerHttpResponse.Response(RunnerHttpProtocol.RPC_PREPARE_CONF) + + return reps.setToken(token).responseToFlatBuffers() + + def RpcHttpReqCall(self): + # init request + req = RunnerHttpRequest.Request(RunnerHttpProtocol.RPC_HTTP_REQ_CALL, self.req_data) + # get request token + token = req.getConfToken() + # get plugins + configs = RunnerPluginCache.getConfigByToken(token) + # init response + reps = RunnerHttpResponse.Response(RunnerHttpProtocol.RPC_HTTP_REQ_CALL) + # execute plugins + RunnerPluginExecute.executeFilter(configs, req, reps) + + return reps.responseToFlatBuffers() + + def RpcTest(self): + pass + + def RpcUnknown(self): + builder = RunnerHttpProtocol.newBuilder() + A6ErrResp.Start(builder) + A6ErrResp.AddCode(builder, A6ErrCode.Code.BAD_REQUEST) + res = A6ErrResp.End(builder) + builder.Finish(res) + return builder + + def dispatch(self): + handler = { + RunnerHttpProtocol.RPC_UNKNOWN: self.RpcUnknown, + RunnerHttpProtocol.RPC_TEST: self.RpcTest, + RunnerHttpProtocol.RPC_PREPARE_CONF: self.RpcPrepareConf, + RunnerHttpProtocol.RPC_HTTP_REQ_CALL: self.RpcHttpReqCall, + } + return {"type": self.req_type, "data": handler.get(self.req_type, self.RpcUnknown)().Output()}
