Hi everyone!
I’m have a big problem when try to use Redis in Python Custom Plugin. Example code of my plugin below:
import kong_pdk.pdk.kong as kong
import redis
Schema = (
{"message": {"type": "string"}},
)
version = '0.1.0'
priority = 0
def example_access_phase(kong: kong.kong):
kong.log.debug("Hello!")
data = redis_string()
if not data:
kong.response.exit(500, "Error")
kong.response.exit(200, f"Data: {data}")
def redis_string():
redis_host = '127.0.0.1'
redis_port = 6379
r = redis.StrictRedis(host=redis_host, port=redis_port, decode_responses=True)
r.set("message", "Hello world!")
mes = r.get("message")
return mes
class Plugin(object):
def __init__(self, config):
self.config = config
def access(self, kong: kong.kong):
example_access_phase(kong)
if __name__ == "__main__":
from kong_pdk.cli import start_dedicated_server
start_dedicated_server("py-hello", Plugin, version, priority)
and then i send a request through an example upstream service and receive error like below content:
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] Exception in thread Thread-3 (_handler_event_func):, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] Traceback (most recent call last):, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/connection.py", line 264, in connect, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] sock = self.retry.call_with_retry(, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/retry.py", line 46, in call_with_retry, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] return do(), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/connection.py", line 265, in <lambda>, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] lambda: self._connect(), lambda error: self.disconnect(error), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/connection.py", line 627, in _connect, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] raise err, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/connection.py", line 615, in _connect, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] sock.connect(socket_address), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] ConnectionRefusedError: [Errno 111] Connection refused, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] During handling of the above exception, another exception occurred:, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] Traceback (most recent call last):, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] self.run(), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/lib/python3.10/threading.py", line 953, in run, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] self._target(*self._args, **self._kwargs), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/opt/kong-python-pdk/kong_pdk/server.py", line 62, in _handler_event_func, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] cls_phase(Kong(ch, lua_style).kong), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/opt/conf/kong-py-plugins/demo-plugin.py", line 35, in access, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] example_access_phase(kong), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/opt/conf/kong-py-plugins/demo-plugin.py", line 13, in example_access_phase, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] data = redis_string(), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/opt/conf/kong-py-plugins/demo-plugin.py", line 25, in redis_string, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] r.set("message", "Hello world!"), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/commands/core.py", line 2341, in set, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] return self.execute_command("SET", *pieces, **options), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/client.py", line 533, in execute_command, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] conn = self.connection or pool.get_connection(command_name, **options), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/connection.py", line 1086, in get_connection, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] connection.connect(), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] File "/usr/local/lib/python3.10/dist-packages/redis/connection.py", line 270, in connect, context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] raise ConnectionError(self._error_message(e)), context: ngx.timer
2023-12-25 08:30:56 2023/12/25 01:30:56 [info] 1295#0: *606 [py:1313] redis.exceptions.ConnectionError: Error 111 connecting to 127.0.0.1:6379. Connection refused., context: ngx.timer
I try them apart and know that Redis work good, and Kong work just ok too.
Anyone who solve this problem or know it please help me :<
Thanks!