Skip to content

Commit deab0bc

Browse files
committed
Use db in token redis plugin
1 parent 82175db commit deab0bc

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

llmstack/common/runner/server.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,18 @@ def main():
317317
redis_client.ping()
318318

319319
display_pool = VirtualDisplayPool(
320-
redis_client, hostname=args.hostname, max_displays=args.max_displays, start_display=args.start_display, display_res=args.display_res, rfb_start_port=args.rfb_start_port)
320+
redis_client, hostname=args.hostname, max_displays=args.max_displays,
321+
start_display=args.start_display, display_res=args.display_res,
322+
rfb_start_port=args.rfb_start_port)
321323

322324
# Start websockify server
323-
websockify_process = subprocess.Popen(['websockify', f'{args.wss_port}', '--web', '/usr/share/www/html', '--token-plugin=TokenRedis', f'--token-source={args.redis_host}:{args.redis_port}',
324-
'-v', '--auth-plugin=llmstack.common.runner.auth.BasicHTTPAuthWithRedis', f'--auth-source={args.redis_host}:{args.redis_port}{f":{args.redis_password}" if args.redis_password else ""}'], close_fds=True)
325+
websockify_process = subprocess.Popen(
326+
['websockify', f'{args.wss_port}', '--web', '/usr/share/www/html',
327+
'--token-plugin=llmstack.common.runner.token.TokenRedis',
328+
f'--token-source={args.redis_host}:{args.redis_port}:{args.redis_db}{f":{args.redis_password}" if args.redis_password else ""}',
329+
'-v', '--auth-plugin=llmstack.common.runner.auth.BasicHTTPAuthWithRedis',
330+
f'--auth-source={args.redis_host}:{args.redis_port}:{args.redis_db}{f":{args.redis_password}" if args.redis_password else ""}'],
331+
close_fds=True)
325332

326333
server = grpc_server(futures.ThreadPoolExecutor(max_workers=10))
327334
runner = Runner(display_pool=display_pool)

llmstack/common/runner/token.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json
2+
import logging
3+
import re
4+
import sys
5+
6+
import redis
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class TokenRedis():
12+
def __init__(self, src):
13+
try:
14+
# Split parts and use defaults for missing fields
15+
parts = src.split(":")
16+
self._server = parts[0]
17+
self._port = int(parts[1]) if len(parts) > 1 and parts[1] else 6379
18+
self._db = int(parts[2]) if len(parts) > 2 and parts[2] else 0
19+
self._password = parts[3] if len(parts) > 3 else None
20+
21+
logger.info(
22+
f'TokenRedis initialized ({self._server}:{self._port})')
23+
except (ValueError, IndexError):
24+
logger.error(f'Invalid format: {src}')
25+
sys.exit()
26+
27+
def lookup(self, token):
28+
logger.info(f'resolving token "{token}"')
29+
client = redis.Redis(host=self._server, port=self._port,
30+
db=self._db, password=self._password)
31+
32+
stuff = client.get(token)
33+
if stuff is None:
34+
return None
35+
36+
response_str = stuff.decode("utf-8").strip()
37+
logger.debug(f'response from redis: {response_str}')
38+
39+
try:
40+
# Attempt to load JSON
41+
if response_str.startswith("{"):
42+
data = json.loads(response_str)
43+
host, port = data["host"].split(":")
44+
# Or parse simple format
45+
elif re.match(r'\S+:\S+', response_str):
46+
host, port = response_str.split(":")
47+
else:
48+
raise ValueError('Unable to parse token')
49+
return [host, port]
50+
except (ValueError, json.JSONDecodeError) as e:
51+
logger.error(f'Unable to process token: {response_str}')
52+
return None

runner/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ ENV DEBIAN_FRONTEND=noninteractive
66
RUN apt-get update && apt-get install -y \
77
python3 \
88
python3-pip \
9-
xdotool \
109
x11vnc \
11-
websockify \
1210
novnc \
1311
redis-server \
1412
&& rm -rf /var/lib/apt/lists/*

0 commit comments

Comments
 (0)