spacewander commented on a change in pull request #8: URL: https://github.com/apache/apisix-python-plugin-runner/pull/8#discussion_r684599120
########## File path: src/runner/socket/error.py ########## @@ -0,0 +1,53 @@ +# +# 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 __future__ import annotations +from a6pluginproto.Err import Code as A6ErrCode + +RUNNER_SUCCESS_CODE = 200 +RUNNER_SUCCESS_MESSAGE = "OK" +RUNNER_ERROR_CODE = 500 +RUNNER_ERR_MESSAGE = "ERR" Review comment: When to use RUNNER_ERR_MESSAGE? ########## File path: src/runner/socket/protocol.py ########## @@ -0,0 +1,82 @@ +# +# 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 src.runner.socket.error import Error as NewServerError +from src.runner.socket.error import RUNNER_ERROR_CODE +from src.runner.socket.error import RUNNER_SUCCESS_CODE + + +class Protocol(object): + + def __init__(self, buffer: bytes = b'', ty: int = 0): + self.__buffer = buffer + self.__type = ty + self.__length = 0 + + def length(self) -> int: + """ + get buffer length + :return: + """ + return self.__length + + def type(self) -> int: + """ + get protocol type + :return: + """ + return self.__type + + def buffer(self) -> bytes: + """ + get buffer data + :return: + """ + return self.__buffer + + def encode(self) -> NewServerError: + """ + encode protocol buffer data + :return: + """ + if len(self.__buffer) == 0: + return NewServerError(RUNNER_ERROR_CODE, "ERR: send buffer is empty") + response_len = len(self.__buffer) + response_header = response_len.to_bytes(4, byteorder="big") + response_header = bytearray(response_header) + response_header[0] = self.__type + response_header = bytes(response_header) + self.__buffer = response_header + self.__buffer + self.__length = len(self.__buffer) + return NewServerError(code=RUNNER_SUCCESS_CODE, message="OK") Review comment: Which is preferred, "code=xxx, message=yyy" or "xxx, yyy"? ########## File path: src/runner/socket/error.py ########## @@ -0,0 +1,53 @@ +# +# 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 __future__ import annotations +from a6pluginproto.Err import Code as A6ErrCode + +RUNNER_SUCCESS_CODE = 200 +RUNNER_SUCCESS_MESSAGE = "OK" +RUNNER_ERROR_CODE = 500 +RUNNER_ERR_MESSAGE = "ERR" + +errorCodes = [ + A6ErrCode.Code.CONF_TOKEN_NOT_FOUND, + A6ErrCode.Code.BAD_REQUEST, + A6ErrCode.Code.SERVICE_UNAVAILABLE, +] + + +class Error(object): Review comment: The name here is confusing. Why success is a type of Error? ########## File path: src/runner/socket/protocol.py ########## @@ -0,0 +1,82 @@ +# +# 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 src.runner.socket.error import Error as NewServerError +from src.runner.socket.error import RUNNER_ERROR_CODE +from src.runner.socket.error import RUNNER_SUCCESS_CODE + + +class Protocol(object): + + def __init__(self, buffer: bytes = b'', ty: int = 0): + self.__buffer = buffer + self.__type = ty + self.__length = 0 + + def length(self) -> int: + """ + get buffer length + :return: + """ + return self.__length + + def type(self) -> int: + """ + get protocol type + :return: + """ + return self.__type + + def buffer(self) -> bytes: + """ + get buffer data + :return: + """ + return self.__buffer + + def encode(self) -> NewServerError: + """ + encode protocol buffer data + :return: + """ + if len(self.__buffer) == 0: + return NewServerError(RUNNER_ERROR_CODE, "ERR: send buffer is empty") + response_len = len(self.__buffer) + response_header = response_len.to_bytes(4, byteorder="big") + response_header = bytearray(response_header) + response_header[0] = self.__type + response_header = bytes(response_header) + self.__buffer = response_header + self.__buffer + self.__length = len(self.__buffer) + return NewServerError(code=RUNNER_SUCCESS_CODE, message="OK") + + def decode(self) -> NewServerError: + """ + decode protocol buffer data + :return: + """ + if len(self.__buffer) == 0: + return NewServerError(RUNNER_ERROR_CODE, "ERR: recv buffer is empty") + length = len(self.__buffer) + if length != 4: + return NewServerError(RUNNER_ERROR_CODE, + "ERR: recv protocol type length is 4, got %d" % length) + + buf = bytearray(self.__buffer) + self.__type = buf[0] + buf[0] = 0 + self.__length = int.from_bytes(buf, byteorder="big") + return NewServerError(code=RUNNER_SUCCESS_CODE, message="OK") Review comment: Why not use RUNNER_SUCCESS_MESSAGE? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
