Skip to content

Commit

Permalink
Use redis-py to replace asyncio-redis (#498)
Browse files Browse the repository at this point in the history
* Use redis-py to replace asyncio-redis

* Also remove unused package

* lint fix

* trigger workflow

* fix lint

* clean error commit files
  • Loading branch information
calmzhu committed Sep 26, 2023
1 parent a7176dc commit 152d5fa
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import asyncio

import aioredis
import redis.asyncio as redis
from opal_common.logger import logger


async def run_locked(
redis: aioredis.Redis, lock_name: str, coro: asyncio.coroutine, timeout: int = 10
_redis: redis.Redis, lock_name: str, coro: asyncio.coroutine, timeout: int = 10
):
"""This function runs a coroutine wrapped in a redis lock, in a way that
prevents hanging locks. Hanging locks can happen when a process crashes
while holding a lock.
This function sets a redis enforced timeout, and reacquires the lock every timeout * 0.8 (as long as it runs)
"""
lock = redis.lock(lock_name, timeout=timeout)
lock = _redis.lock(lock_name, timeout=timeout)
try:
logger.debug(f"Trying to acquire redis lock: {lock_name}")
await lock.acquire()
Expand Down
2 changes: 0 additions & 2 deletions packages/opal-server/opal_server/git_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import asyncio
import hashlib
import shutil
from pathlib import Path
from typing import Optional, cast

import aiofiles.os
import aioredis
import pygit2
from git import Repo
from opal_common.async_utils import run_sync
Expand Down
6 changes: 3 additions & 3 deletions packages/opal-server/opal_server/redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Generator

import aioredis
import redis.asyncio as redis
from opal_common.logger import logger
from pydantic import BaseModel

Expand All @@ -12,10 +12,10 @@ def __init__(self, redis_url):
self._url = redis_url
logger.debug("Connecting to Redis: {url}", url=self._url)

self._redis = aioredis.from_url(self._url)
self._redis = redis.Redis.from_url(self._url)

@property
def redis_connection(self) -> aioredis.Redis:
def redis_connection(self) -> redis.Redis:
return self._redis

async def set(self, key: str, value: BaseModel):
Expand Down
1 change: 0 additions & 1 deletion packages/opal-server/requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ websockets>=10.3,<11
ddtrace>=1.1.4,<2
slowapi>=0.1.5,<1
# slowapi is stuck on and old `redis`, so fix that and switch from aioredis to redis
aioredis>=2.0.1,<3
pygit2>=1.9.2,<2
asgiref>=3.5.2,<4
redis>=4.3.4,<5

0 comments on commit 152d5fa

Please sign in to comment.