Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use redis-py to replace asyncio-redis #498

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading