mf,dZddlZddlZddlZddlZddlmZddlmZ ddl m Z ddl m Z ddl m Z n#e $r dxZ xZ Z YnwxYwddlmZdd lmZdd lmZejd Zd ZGd deZeddZGddeZGddeeZGddeZGddeeZ ddlmZGddeeZn #e $rdZYnwxYwGddeZ Gdde eZ! dd l"m#Z#Gd!d"e e#Z$n #e $rdZ$YnwxYw dd#l"m%Z%Gd$d%e e%Z&dS#e $rdZ&YdSwxYw)&aG Lightweight connection pooling for peewee. In a multi-threaded application, up to `max_connections` will be opened. Each thread (or, if using gevent, greenlet) will have it's own connection. In a single-threaded application, only one connection will be created. It will be continually recycled until either it exceeds the stale timeout or is closed explicitly (using `.manual_close()`). By default, all your application needs to do is ensure that connections are closed when you are finished with them, and they will be returned to the pool. For web applications, this typically means that at the beginning of a request, you will open a connection, and when you return a response, you will close the connection. Simple Postgres pool example code: # Use the special postgresql extensions. from playhouse.pool import PooledPostgresqlExtDatabase db = PooledPostgresqlExtDatabase( 'my_app', max_connections=32, stale_timeout=300, # 5 minutes. user='postgres') class BaseModel(Model): class Meta: database = db That's it! N) namedtuple)chain)TRANSACTION_STATUS_IDLE)TRANSACTION_STATUS_INERROR)TRANSACTION_STATUS_UNKNOWN) MySQLDatabase)PostgresqlDatabase)SqliteDatabasez peewee.poolc`|+t|ttfst|S|SN) isinstanceintfloat)vals E/opt/imunify360/venv/lib64/python3.11/site-packages/playhouse/pool.pymake_intr9s) z#U|<<3xx JceZdZdS)MaxConnectionsExceededN__name__ __module__ __qualname__rrrr?srrPoolConnection) timestamp connection checked_outceZdZ dfd Z dfd Zdfd ZfdZdZd Zd Z dfd Z d Z d Z ddZ dZxZS)PooledDatabaseNc 4t||_t||_t||_|jdkrt d|_g|_i|_t|_tt|j |fi|dSNrinf) r_max_connections_stale_timeout _wait_timeoutr _connections_in_useidconn_keysuperr __init__)selfdatabasemax_connections stale_timeouttimeoutkwargs __class__s rr-zPooledDatabase.__init__Gs ( 9 9&}55%g..   " "!&uD    ,nd##,X@@@@@@@rc tt|j|fi||t||_|t||_|5t||_|jdkrtd|_dSdSdSr#)r,r initrr%r&r'r)r.r/r0r1r2connect_kwargsr4s rr6zPooledDatabase.init_s(nd##(DD^DDD  &$,_$=$=D !  $"*="9"9D   !)'!2!2D !Q&&%*5\\"""  &&rFc|js(tt||St j|jz}|t jkrf tt||}|S#t $rt jdYnwxYw|t jkft d)Ng?z:Max connections exceeded, timed out attempting to connect.)r'r,r connecttimersleep)r. reuse_if_openexpiresretr4s rr9zPooledDatabase.connectks! F..66}EE E)++ 22 ## ND1199-HH *    3  ##%&>?? ?s$(BB/.B/c tj|j\}}||}||r t d|dx}}n|jrK||r6t d|| |ddx}}n/n/#t$r!dx}}t dYnwxYw||j r,t|j |j krtdtt |}t%jt'jdz z }||}t d|t)||t%j|j |<|S)NTzConnection %s was closed.z!Connection %s was stale, closing.z No connection available in pool.zExceeded maximum connections.izCreated new connection %s.)heapqheappopr(r+ _is_closedloggerdebugr& _is_stale_close IndexErrorr%lenr)rr,r _connectr:randomr)r.tsconnkeyr4s rrIzPooledDatabase._connectzs   =):;;DmmD)) ??4(( LL!>>,-LMMM..7799Dv}55B--%%C LL5s ; ; ;*2tTY[[AA S s1B==(C('C(c@tj|z |jkSr )r:r&)r.rs rrEzPooledDatabase._is_stales i'4+>>>rcdS)NFrr.rLs rrBzPooledDatabase._is_closedsurcdS)NTrrPs r _can_reusezPooledDatabase._can_reusestrc|||}|r*tt||dS||jvr|j|}|jr_||jrEt d|tt||dS| |r>t d|tj |j|j|fdSt d|dSdS)NzClosing stale connection %s.zReturning %s to pool.z Closed %s.)r+r,r rFr)popr&rErrCrDrRr@heappushr()r.rL close_connrM pool_connr4s rrFzPooledDatabase._closes*mmD!!  0 .$ ' ' . .t 4 4 4 4 4 DL  ((--I" 0t~~i6I'J'J 0 ;SAAAnd++22488888&& 0 4c:::t093F2MNNNNN \3/////! rc |rdS|}|j||d|||ddS)zS Close the underlying connection without returning it to the pool. FNTrV) is_closedrr)rTr+closerFrPs r manual_closezPooledDatabase.manual_closes{ >>   5   t,,d333  DT *****rc|j5|jD]\}}||dg|_ddddS#1swxYwYdSNTrY)_lockr(rF)r._rLs r close_idlezPooledDatabase.close_idles Z # #, 3 34 DT 2222 "D  # # # # # # # # # # # # # # # # # #s,AAAXc(|j5i}tj|z }d}|jD]7\}}|j|kr"||jd|dz }2|||<8||_dddn #1swxYwY|S)NrTrY)r_r:r)itemsrrFr)r.agein_usecutoffnrMrWs r close_stalezPooledDatabase.close_stalesZ " "FY[[3&FA"&,"4"4"6"6 , ,Y(611KK 4KFFFFAA"+F3KK!DL " " " " " " " " " " " " " " "sA3BB B cB||j5|jD]\}}||d|jD]}||jdg|_i|_ddddS#1swxYwYdSr^)r[r_r(rFr)valuesr)r.r`rLrWs r close_allzPooledDatabase.close_alls Z  , 3 34 DT 2222!\0022 C C  I0T BBBB "D DL                   sA+BBB)r!NN)NNN)F)rb)rrrr-r6r9rIrErBrRrFr\rarjrm __classcell__)r4s@rr r Fs5CGAAAAAA0BF 2 2 2 2 2 2 ? ? ? ? ? ?(((((T???  0 0 0 0 0 0+++$###       rr ceZdZdZdS)PooledMySQLDatabasecB |ddS#YdSxYwNFT)pingrPs rrBzPooledMySQLDatabase._is_closeds0  IIe   5 44sNrrrrBrrrrprp#rrpceZdZdZdZdS)_PooledPostgresqlDatabasec|jrdS|}|tkrdS|tkr|dS)NTF)closedget_transaction_statusrrrollbackr.rL txn_statuss rrBz$_PooledPostgresqlDatabase._is_closedsQ ; 40022 3 3 34 2 2 2 MMOOOurc|}|tkrdS|tkr|n|tkr|dSrr)rzrrresetrr{r|s rrRz$_PooledPostgresqlDatabase._can_reuse s_0022  3 3 35 5 5 5 JJLLLL 2 2 2 MMOOOtrN)rrrrBrRrrrrwrws2        rrwceZdZdS)PooledPostgresqlDatabaseNrrrrrrDrr)PostgresqlExtDatabaseceZdZdS)PooledPostgresqlExtDatabaseNrrrrrr rrceZdZdZdS)_PooledSqliteDatabasec& |jdS#YdSxYwrr) total_changesrPs rrBz _PooledSqliteDatabase._is_closed%s'    5 44s Nrtrrrrr$rurrceZdZdS)PooledSqliteDatabaseNrrrrrr-rrr)SqliteExtDatabaseceZdZdS)PooledSqliteExtDatabaseNrrrrrr3rrr)CSqliteExtDatabaseceZdZdS)PooledCSqliteExtDatabaseNrrrrrr;rrr)'__doc__r@loggingrJr: collectionsr itertoolsrpsycopg2.extensionsrrr ImportErrorpeeweerr r getLoggerrCr ValueErrorrrobjectr rprwrplayhouse.postgres_extrrrrplayhouse.sqlite_extrrrrrrrrs  B  """""".;;;;;;>>>>>>>>>>>>>...*... & & & &. ! %%%%%%!!!!!!  = ) ) /....Z...,/>??lllllVlll^.-2     8:L   '<<<<<<     &?AV    '''"&'N     0.   #666666     "79J    ###"#$777777     #8:L     $$$#$sE3 AA>CCC?DDD#D::EE