Skip to content

Commit

Permalink
run guard client in app context; use thread lock on singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebCourier committed Aug 15, 2024
1 parent af02055 commit 940f937
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
12 changes: 7 additions & 5 deletions guardrails_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def create_app(
cache_client.initialize(app)

from guardrails_api.blueprints.root import root_bp
from guardrails_api.blueprints.guards import guards_bp, guard_client
from guardrails_api.blueprints.guards import guards_bp

app.register_blueprint(root_bp)
app.register_blueprint(guards_bp)
Expand All @@ -111,11 +111,13 @@ def create_app(

console.print(":green_circle: Active guards and OpenAI compatible endpoints:")

for g in guard_client.get_guards():
g = g.to_dict()
console.print(f"- Guard: [bold white]{g.get('name')}[/bold white] {self_endpoint}/guards/{g.get('name')}/openai/v1")
with app.app_context():
from guardrails_api.blueprints.guards import guard_client
for g in guard_client.get_guards():
g = g.to_dict()
console.print(f"- Guard: [bold white]{g.get('name')}[/bold white] {self_endpoint}/guards/{g.get('name')}/openai/v1")

console.print("")
console.print(Rule("[bold grey]Server Logs[/bold grey]", characters="=", style="white"))

return app
return app
5 changes: 4 additions & 1 deletion guardrails_api/clients/cache_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import threading
from flask_caching import Cache


# TODO: Add option to connect to Redis or MemCached backend with environment variables
class CacheClient:
_instance = None
_lock = threading.Lock()

def __new__(cls):
if cls._instance is None:
cls._instance = super(CacheClient, cls).__new__(cls)
with cls._lock:
cls._instance = super(CacheClient, cls).__new__(cls)
return cls._instance

def initialize(self, app):
Expand Down
5 changes: 4 additions & 1 deletion guardrails_api/clients/postgres_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boto3
import json
import os
import threading
from flask import Flask
from sqlalchemy import text
from typing import Tuple
Expand All @@ -13,10 +14,12 @@ def postgres_is_enabled() -> bool:

class PostgresClient:
_instance = None
_lock = threading.Lock()

def __new__(cls):
if cls._instance is None:
cls._instance = super(PostgresClient, cls).__new__(cls)
with cls._lock:
cls._instance = super(PostgresClient, cls).__new__(cls)
return cls._instance

def fetch_pg_secret(self, secret_arn: str) -> dict:
Expand Down

0 comments on commit 940f937

Please sign in to comment.