From e1ede580942c1f3d6915a6cec6825a82d094e8eb Mon Sep 17 00:00:00 2001 From: Look_World Date: Tue, 21 Mar 2023 11:41:06 +0800 Subject: [PATCH 1/3] fix: Optimize session expiration --- common/expired_dict.py | 45 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/common/expired_dict.py b/common/expired_dict.py index ccf16c7..ab449ee 100644 --- a/common/expired_dict.py +++ b/common/expired_dict.py @@ -1,23 +1,50 @@ -from datetime import datetime, timedelta +import time +from collections import OrderedDict -class ExpiredDict(dict): + +class ExpiredDict(OrderedDict): def __init__(self, expires_in_seconds): super().__init__() + # 存储键值对的生存时间 self.expires_in_seconds = expires_in_seconds + # 获取当前时间的函数 + self.now = time.monotonic def __getitem__(self, key): - value, expiry_time = super().__getitem__(key) - if datetime.now() > expiry_time: + # 检查键是否存在 + if key not in self: + raise KeyError(key) + # 获取值和过期时间 + value, expiry_time = self[key] + # 如果过期时间早于当前时间,删除该键值对并引发 KeyError + if expiry_time is not None and self.now() > expiry_time: del self[key] - raise KeyError("expired {}".format(key)) - self.__setitem__(key, value) + raise KeyError(key) + # 如果存活时间不为 None,更新该键值对的过期时间 + if self.expires_in_seconds is not None: + self[key] = value, self.now() + self.expires_in_seconds + # 删除过期的键值对 + self._delete_expired_items() + # 返回值 return value def __setitem__(self, key, value): - expiry_time = datetime.now() + timedelta(seconds=self.expires_in_seconds) - super().__setitem__(key, (value, expiry_time)) + # 如果存活时间不为 None,设置该键值对的过期时间 + if self.expires_in_seconds is not None: + self[key] = value, self.now() + self.expires_in_seconds + else: + self[key] = value, None + # 删除过期的键值对 + self._delete_expired_items() + def get(self, key, default=None): try: return self[key] except KeyError: - return default \ No newline at end of file + return default + + def _delete_expired_items(self): + # 遍历所有键值对,删除过期的键值对 + for key, (value, expiry_time) in list(self.items()): + if expiry_time is not None and self.now() > expiry_time: + del self[key] From 695302d407f6003067da421858e123b1284532ad Mon Sep 17 00:00:00 2001 From: Look_World Date: Tue, 21 Mar 2023 14:03:28 +0800 Subject: [PATCH 2/3] Revert "fix: Optimize session expiration" This reverts commit e1ede580942c1f3d6915a6cec6825a82d094e8eb. --- common/expired_dict.py | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/common/expired_dict.py b/common/expired_dict.py index ab449ee..c20c27b 100644 --- a/common/expired_dict.py +++ b/common/expired_dict.py @@ -1,50 +1,28 @@ import time -from collections import OrderedDict -class ExpiredDict(OrderedDict): +class ExpiredDict(dict): def __init__(self, expires_in_seconds): super().__init__() - # 存储键值对的生存时间 self.expires_in_seconds = expires_in_seconds - # 获取当前时间的函数 - self.now = time.monotonic def __getitem__(self, key): - # 检查键是否存在 - if key not in self: - raise KeyError(key) - # 获取值和过期时间 - value, expiry_time = self[key] - # 如果过期时间早于当前时间,删除该键值对并引发 KeyError - if expiry_time is not None and self.now() > expiry_time: + value, expiry_time = super().__getitem__(key) + # 如果元素已过期,则从字典中删除该元素并抛出 KeyError 异常 + if time.monotonic() > expiry_time: del self[key] - raise KeyError(key) - # 如果存活时间不为 None,更新该键值对的过期时间 - if self.expires_in_seconds is not None: - self[key] = value, self.now() + self.expires_in_seconds - # 删除过期的键值对 - self._delete_expired_items() - # 返回值 + raise KeyError("expired {}".format(key)) return value def __setitem__(self, key, value): - # 如果存活时间不为 None,设置该键值对的过期时间 - if self.expires_in_seconds is not None: - self[key] = value, self.now() + self.expires_in_seconds + # 如果元素已存在,则不需要重新计算过期时间 + if key in self: + super().__setitem__(key, (value, self[key][1])) else: - self[key] = value, None - # 删除过期的键值对 - self._delete_expired_items() + super().__setitem__(key, (value, time.monotonic() + self.expires_in_seconds)) def get(self, key, default=None): try: return self[key] except KeyError: return default - - def _delete_expired_items(self): - # 遍历所有键值对,删除过期的键值对 - for key, (value, expiry_time) in list(self.items()): - if expiry_time is not None and self.now() > expiry_time: - del self[key] From ea5f7173bdd0468bb5a432649ad8484ba69857ab Mon Sep 17 00:00:00 2001 From: Look_World Date: Thu, 23 Mar 2023 16:27:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?1.Docker=E6=94=AF=E6=8C=81=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E6=9C=80=E6=96=B0=E7=89=88=E6=9C=AC;=202.=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=9E=84=E5=BB=BA=E9=80=9F=E5=BA=A6=EF=BC=8C=E6=97=A0?= =?UTF-8?q?=E9=A1=BB=E5=86=8D=E6=AC=A1=E4=B8=8B=E8=BD=BD=E5=8C=85;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/Dockerfile.latest | 35 +++++++++++++++++++++++++++++++++++ docker/build.latest.sh | 8 ++++++++ 2 files changed, 43 insertions(+) create mode 100644 docker/Dockerfile.latest create mode 100644 docker/build.latest.sh diff --git a/docker/Dockerfile.latest b/docker/Dockerfile.latest new file mode 100644 index 0000000..fed4011 --- /dev/null +++ b/docker/Dockerfile.latest @@ -0,0 +1,35 @@ +FROM python:3.7.9-alpine + +LABEL maintainer="foo@bar.com" +ARG TZ='Asia/Shanghai' + +ARG CHATGPT_ON_WECHAT_VER + +ENV BUILD_PREFIX=/app \ + BUILD_OPEN_AI_API_KEY='YOUR OPEN AI KEY HERE' + +COPY chatgpt-on-wechat.tar.gz ./chatgpt-on-wechat.tar.gz + +RUN apk add --no-cache \ + bash \ + && tar -xf chatgpt-on-wechat.tar.gz \ + && mv chatgpt-on-wechat ${BUILD_PREFIX} \ + && cd ${BUILD_PREFIX} \ + && cp config-template.json ${BUILD_PREFIX}/config.json \ + && sed -i "2s/YOUR API KEY/${BUILD_OPEN_AI_API_KEY}/" ${BUILD_PREFIX}/config.json \ + && /usr/local/bin/python -m pip install --no-cache --upgrade pip \ + && pip install --no-cache \ + itchat-uos==1.5.0.dev0 \ + openai + +WORKDIR ${BUILD_PREFIX} + +ADD ./entrypoint.sh /entrypoint.sh + +RUN chmod +x /entrypoint.sh \ + && adduser -D -h /home/noroot -u 1000 -s /bin/bash noroot \ + && chown noroot:noroot ${BUILD_PREFIX} + +USER noroot + +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/docker/build.latest.sh b/docker/build.latest.sh new file mode 100644 index 0000000..d80e84c --- /dev/null +++ b/docker/build.latest.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# move chatgpt-on-wechat +tar -zcf chatgpt-on-wechat.tar.gz --exclude=../../chatgpt-on-wechat/docker ../../chatgpt-on-wechat + +# build image +docker build -f Dockerfile.alpine \ + -t zhayujie/chatgpt-on-wechat . \ No newline at end of file