File size: 7,345 Bytes
67c9f93
 
a3a0745
67c9f93
 
a3a0745
67c9f93
a3a0745
 
67c9f93
 
 
 
 
a3a0745
67c9f93
 
 
 
 
 
 
 
 
 
 
a3a0745
 
 
67c9f93
a3a0745
 
 
 
 
 
 
67c9f93
a3a0745
 
 
 
 
 
 
 
 
 
 
 
 
 
260407e
a3a0745
 
 
260407e
a3a0745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
067af89
 
 
 
 
 
 
 
260407e
 
 
067af89
260407e
 
067af89
 
 
 
 
 
 
 
 
 
 
 
 
a3a0745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67c9f93
 
 
 
 
 
 
 
 
067af89
 
 
 
 
 
 
 
67c9f93
 
 
 
a3a0745
 
67c9f93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
from contextlib import contextmanager
from queue import Empty, Full, Queue
from threading import Lock
from typing import Iterator, Optional
from urllib.parse import parse_qs, unquote, urlparse

import pymysql
from pymysql.connections import Connection

from dotenv import load_dotenv

load_dotenv()

_pool: Optional["MySQLConnectionPool"] = None
_pool_lock = Lock()


def _get_env(name: str, default: Optional[str] = None) -> Optional[str]:
    value = os.getenv(name)
    if value is None:
        return default
    trimmed = value.strip()
    return trimmed or default


def _str_to_bool(value: str) -> bool:
    return value.lower() in {"1", "true", "yes", "on"}


def _parse_database_url(database_url: str) -> dict:
    parsed = urlparse(database_url)
    scheme = parsed.scheme.lower()
    if scheme not in {"mysql", "mysql+pymysql"}:
        raise RuntimeError(
            "DATABASE_URL must use the mysql scheme when PyMySQL is in use"
        )

    database = parsed.path.lstrip("/")
    if not database:
        raise RuntimeError("DATABASE_URL must include the database name")

    connect_kwargs = {
        "host": parsed.hostname or "localhost",
        "port": parsed.port or 3306,
        "user": unquote(parsed.username or ""),
        "password": unquote(parsed.password or ""),
        "database": database,
        "charset": _get_env("DB_CHARSET", "utf8mb4"),
        "autocommit": True,
    }

    connect_timeout = _get_env("DB_CONNECT_TIMEOUT")
    if connect_timeout:
        connect_kwargs["connect_timeout"] = float(connect_timeout)

    ssl_root_cert = _get_env("SSL_ROOT_CERT")
    if ssl_root_cert:
        connect_kwargs["ssl"] = {"ca": ssl_root_cert}

    query_params = parse_qs(parsed.query, keep_blank_values=True)
    for key, values in query_params.items():
        if not values:
            continue
        value = values[-1]
        if key == "autocommit":
            connect_kwargs["autocommit"] = _str_to_bool(value)
        elif key == "charset":
            connect_kwargs["charset"] = value
        elif key == "connect_timeout":
            connect_kwargs["connect_timeout"] = float(value)
        else:
            connect_kwargs[key] = value

    return {key: val for key, val in connect_kwargs.items() if val not in {None, ""}}


def _build_connect_kwargs_from_env() -> Optional[dict]:
    host = _get_env("DB_HOST")
    database = _get_env("DB_NAME") or _get_env("DB_DATABASE")
    if not host or not database:
        return None

    connect_kwargs = {
        "host": host,
        "port": int(_get_env("DB_PORT", "3306")),
        "user": _get_env("DB_USER", ""),
        "password": _get_env("DB_PASSWORD", ""),
        "database": database,
        "charset": _get_env("DB_CHARSET", "utf8mb4"),
        "autocommit": _str_to_bool(_get_env("DB_AUTOCOMMIT", "true")),
    }

    connect_timeout = _get_env("DB_CONNECT_TIMEOUT")
    if connect_timeout:
        connect_kwargs["connect_timeout"] = float(connect_timeout)

    ssl_root_cert = _get_env("SSL_ROOT_CERT")
    if ssl_root_cert:
        connect_kwargs["ssl"] = {"ca": ssl_root_cert}

    return {key: val for key, val in connect_kwargs.items() if val not in {None, ""}}


class MySQLConnectionPool:
    """Simple thread-safe connection pool for PyMySQL."""

    def __init__(self, connect_kwargs: dict, min_size: int, max_size: int) -> None:
        if min_size < 0:
            raise ValueError("min_size must be non-negative")
        if max_size < 1:
            raise ValueError("max_size must be at least 1")
        if min_size > max_size:
            raise ValueError("min_size cannot exceed max_size")

        self._connect_kwargs = connect_kwargs
        self._available: Queue[Connection] = Queue(maxsize=max_size)
        self._lock = Lock()
        self._max_size = max_size
        self._total_created = 0
        self._closed = False

        for _ in range(min_size):
            conn = self._create_connection()
            self._available.put(conn)
            self._total_created += 1

    def _create_connection(self) -> Connection:
        if self._closed:
            raise RuntimeError("Connection pool is closed")
        return pymysql.connect(**self._connect_kwargs)

    def connection(self):
        return _PooledConnectionContext(self)

    def acquire(self) -> Connection:
        while True:
            try:
                conn = self._available.get_nowait()
            except Empty:
                with self._lock:
                    if self._total_created < self._max_size:
                        conn = self._create_connection()
                        self._total_created += 1
                        return conn
                conn = self._available.get()

            if conn.open:
                conn.ping(reconnect=True)
                conn.autocommit(True)
                return conn
            self._discard_connection(conn)

    def release(self, conn: Connection) -> None:
        if self._closed:
            self._discard_connection(conn)
            return

        if not conn.open:
            self._discard_connection(conn)
            return

        try:
            self._available.put_nowait(conn)
        except Full:
            self._discard_connection(conn)

    def close(self) -> None:
        self._closed = True
        while True:
            try:
                conn = self._available.get_nowait()
            except Empty:
                break
            self._discard_connection(conn)

    def _discard_connection(self, conn: Connection) -> None:
        try:
            conn.close()
        finally:
            with self._lock:
                if self._total_created > 0:
                    self._total_created -= 1


class _PooledConnectionContext:
    def __init__(self, pool: MySQLConnectionPool) -> None:
        self._pool = pool
        self._conn: Optional[Connection] = None

    def __enter__(self) -> Connection:
        self._conn = self._pool.acquire()
        return self._conn

    def __exit__(self, exc_type, exc, tb) -> None:
        if self._conn is not None:
            self._pool.release(self._conn)
            self._conn = None


def _ensure_pool() -> MySQLConnectionPool:
    global _pool
    if _pool is not None:
        return _pool

    with _pool_lock:
        if _pool is not None:
            return _pool

        database_url = _get_env("DATABASE_URL")
        if database_url:
            connect_kwargs = _parse_database_url(database_url)
        else:
            connect_kwargs = _build_connect_kwargs_from_env()
            if connect_kwargs is None:
                raise RuntimeError(
                    "DATABASE_URL or DB_HOST and DB_NAME must be set in the environment"
                )

        min_size = int(_get_env("DB_POOL_MIN_SIZE", "1"))
        max_size = int(_get_env("DB_POOL_MAX_SIZE", "5"))

        pool = MySQLConnectionPool(
            connect_kwargs=connect_kwargs,
            min_size=min_size,
            max_size=max_size,
        )
        _pool = pool
        return pool


@contextmanager
def get_connection() -> Iterator[Connection]:
    pool = _ensure_pool()
    with pool.connection() as conn:
        yield conn


def close_pool() -> None:
    global _pool
    with _pool_lock:
        if _pool is not None:
            _pool.close()
            _pool = None