11import inspect
22import itertools
33import time
4+ import warnings
45
56import asyncpg
67from sqlalchemy import util , exc , sql
2223
2324from . import base
2425
26+ try :
27+ import click
28+ except ImportError :
29+ click = None
2530JSON_COLTYPE = 114
2631JSONB_COLTYPE = 3802
2732
@@ -229,6 +234,56 @@ async def release(self, conn):
229234 async def close (self ):
230235 await self ._pool .close ()
231236
237+ def repr (self , color ):
238+ if color and not click :
239+ warnings .warn ("Install click to get colorful repr." , ImportWarning )
240+
241+ if color and click :
242+ # noinspection PyProtectedMember
243+ return "<{classname} max={max} min={min} cur={cur} use={use}>" .format (
244+ classname = click .style (
245+ self ._pool .__class__ .__module__
246+ + "."
247+ + self ._pool .__class__ .__name__ ,
248+ fg = "green" ,
249+ ),
250+ max = click .style (repr (self ._pool ._maxsize ), fg = "cyan" ),
251+ min = click .style (repr (self ._pool ._minsize ), fg = "cyan" ),
252+ cur = click .style (
253+ repr (
254+ len (
255+ [
256+ 0
257+ for con in self ._pool ._holders
258+ if con ._con and not con ._con .is_closed ()
259+ ]
260+ )
261+ ),
262+ fg = "cyan" ,
263+ ),
264+ use = click .style (
265+ repr (len ([0 for con in self ._pool ._holders if con ._in_use ])),
266+ fg = "cyan" ,
267+ ),
268+ )
269+ else :
270+ # noinspection PyProtectedMember
271+ return "<{classname} max={max} min={min} cur={cur} use={use}>" .format (
272+ classname = self ._pool .__class__ .__module__
273+ + "."
274+ + self ._pool .__class__ .__name__ ,
275+ max = self ._pool ._maxsize ,
276+ min = self ._pool ._minsize ,
277+ cur = len (
278+ [
279+ 0
280+ for con in self ._pool ._holders
281+ if con ._con and not con ._con .is_closed ()
282+ ]
283+ ),
284+ use = len ([0 for con in self ._pool ._holders if con ._in_use ]),
285+ )
286+
232287
233288class NullPool (base .Pool ):
234289 # TODO: generic NullPool, abstracting connection part
@@ -247,6 +302,7 @@ def __init__(self, url, loop, **kwargs):
247302 password = url .password ,
248303 )
249304 )
305+ self ._count = 0
250306
251307 def __await__ (self ):
252308 async def return_self ():
@@ -262,14 +318,33 @@ async def acquire(self, *, timeout=None):
262318 args = self ._kwargs .copy ()
263319 if timeout is not None :
264320 args .update (timeout = timeout )
265- return await asyncpg .connect (loop = self ._loop , ** args )
321+ rv = await asyncpg .connect (loop = self ._loop , ** args )
322+ self ._count += 1
323+ return rv
266324
267325 async def release (self , conn ):
326+ self ._count -= 1
268327 await conn .close ()
269328
270329 async def close (self ):
271330 pass
272331
332+ def repr (self , color ):
333+ if color and not click :
334+ warnings .warn ("Install click to get colorful repr." , ImportWarning )
335+
336+ if color and click :
337+ return "<{classname} max={max} min={min} cur={cur} use={cur}>" .format (
338+ classname = click .style ("NullPool" , fg = "green" ),
339+ max = click .style ("inf" , fg = "cyan" ),
340+ min = click .style ("0" , fg = "cyan" ),
341+ cur = click .style (repr (self ._count ), fg = "cyan" ),
342+ )
343+ else :
344+ return "<NullPool max=inf min=0 cur={cur} use={cur}>" .format (
345+ cur = self ._count
346+ )
347+
273348
274349class Transaction (base .Transaction ):
275350 def __init__ (self , tx ):
0 commit comments