You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sh...@apache.org on 2021/08/03 02:48:53 UTC

[apisix-python-plugin-runner] branch master updated (31ca91b -> 1d353fd)

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

shuaijinchao pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-python-plugin-runner.git.


    from 31ca91b  feat: add plugin loading module for runner.
     new 11666ce  feat: add socket handle module for runner.
     new 1d353fd  feat: add socket server module for runner.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/runner/socket/handle.py |  78 ++++++++++++++++++++++++++++++++
 src/runner/socket/server.py | 105 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+)
 create mode 100644 src/runner/socket/handle.py
 create mode 100644 src/runner/socket/server.py

[apisix-python-plugin-runner] 02/02: feat: add socket server module for runner.

Posted by sh...@apache.org.
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 1d353fd03e912c1d70afb52e21612d671818a367
Author: Janko <sh...@gmail.com>
AuthorDate: Tue Aug 3 10:43:10 2021 +0800

    feat: add socket server module for runner.
---
 src/runner/socket/server.py | 105 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)

diff --git a/src/runner/socket/server.py b/src/runner/socket/server.py
new file mode 100644
index 0000000..37e66d4
--- /dev/null
+++ b/src/runner/socket/server.py
@@ -0,0 +1,105 @@
+#
+# 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.
+#
+import os
+import socket
+from _thread import start_new_thread
+from runner.socket.handle import Handle as A6ServerHandle
+
+
+def runner_protocol_decode(buf):
+    """
+    decode for runner protocol
+    :param buf:
+    :return:
+    """
+    if not buf:
+        return None, "runner protocol undefined."
+    if len(buf) != 4:
+        return None, "runner protocol invalid."
+
+    buf = bytearray(buf)
+    # request buf type
+    buf_type = buf[0]
+    buf[0] = 0
+    # request buf length
+    buf_len = int.from_bytes(buf, byteorder="big")
+    return {"type": buf_type, "len": buf_len}, None
+
+
+def runner_protocol_encode(reps_type, reps_data):
+    """
+    encode for runner protocol
+    :param reps_type:
+    :param reps_data:
+    :return:
+    """
+    reps_len = len(reps_data)
+    reps_header = reps_len.to_bytes(4, byteorder="big")
+    reps_header = bytearray(reps_header)
+    reps_header[0] = reps_type
+    reps_header = bytes(reps_header)
+    return reps_header + reps_data
+
+
+def threaded(conn):
+    while True:
+        header_buf = conn.recv(4)
+        protocol, err = runner_protocol_decode(header_buf)
+        if err:
+            print(err)
+            break
+
+        # rpc request type
+        req_type = protocol.get("type")
+        # rpc request length
+        req_len = protocol.get("len")
+
+        req_data = conn.recv(req_len)
+
+        rpc_handler = A6ServerHandle(req_type, req_data)
+        response = rpc_handler.dispatch()
+
+        reps_type = response.get("type")
+        reps_data = response.get("data")
+        reps = runner_protocol_encode(reps_type, reps_data)
+
+        err = conn.sendall(reps)
+        if err:
+            print(err)
+            break
+    conn.close()
+
+
+class Server:
+    def __init__(self, socket_address):
+        if os.path.exists(socket_address):
+            os.remove(socket_address)
+        self.socket_address = socket_address
+        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        self.sock.bind(socket_address)
+        self.sock.listen(1024)
+        print("listening on unix:%s" % socket_address)
+
+    def receive(self):
+        while True:
+            conn, address = self.sock.accept()
+
+            start_new_thread(threaded, (conn,))
+
+    def __del__(self):
+        self.sock.close()
+        print("Bye")

[apisix-python-plugin-runner] 01/02: feat: add socket handle module for runner.

Posted by sh...@apache.org.
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 <sh...@gmail.com>
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()}