zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
U
£ý°dˆTã@s¸UdZddlmZddlZddlZddlZddlZddlmZddl    m
Z
m Z m Z m Z mZmZmZddlmZzddlZddlZWnek
ršdZYnXdZzd    d
lmZWnek
rÈdZYnXd    d lmZd    d lmZmZd    d lm Z m!Z!m"Z"m#Z#m$Z$d    dl%m&Z&e '¡Z(dZ)dZ*e +d¡Z,dZ-edkr:eneefZ.de/d<iZ0de/d<dddœdd„Z1dddœdd„Z2ddddœd d!„Z3dd"dœd#d$„Z4Gd%d„dƒZ5Gd&d'„d'ƒZ6dS)(z5Implementing pooling of connections to MySQL servers.é)Ú annotationsN)Ú TracebackType)ÚAnyÚDictÚNoReturnÚOptionalÚTupleÚTypeÚUnion)Úuuid4FTé)ÚCMySQLConnection)ÚMySQLConnection)Ú CNX_POOL_ARGSÚDEFAULT_CONFIGURATION)ÚErrorÚInterfaceErrorÚNotSupportedErrorÚ    PoolErrorÚProgrammingError)Úread_option_filesé é@z[^a-zA-Z0-9._:\-*$#]z0MySQL Connector/Python C Extension not availablezUnion[type, Tuple[type, ...]]ÚMYSQL_CNX_CLASSzDict[str, MySQLConnectionPool]Ú_CONNECTION_POOLSrÚPooledMySQLConnection©ÚkwargsÚreturnc    KsÎd|kr|dntf|Ž}| d¡dkr8tdkr8ttƒ‚tR|tkrVtf|Žt|<n4tt|tƒrŠt|j    }d|krŠ|d|krŠt
dƒ‚W5QRXzt|  ¡WSt k
rÈt d|›dƒd‚YnXdS)    z!Return a pooled MySQL connection.Ú    pool_nameÚuse_pureFNÚ    pool_sizez)Size can not be changed for active pools.z%Failed getting connection from pool 'ú')Úgenerate_pool_nameÚgetr Ú ImportErrorÚ ERROR_NO_CEXTÚCONNECTION_POOL_LOCKrÚMySQLConnectionPoolÚ
isinstancer!rÚget_connectionÚAttributeErrorr)rrZ
check_size©r,úNd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\mysql/connector/pooling.pyÚ_get_pooled_connectionMs&ÿ
 
ÿþr.z?Union[PooledMySQLConnection, MySQLConnection, CMySQLConnection]c
Ks<| ¡}z |d}Wntk
r2tdƒd‚YnX|d=tdddddd    d
d d g    ƒ}d }|D]¶}t| ¡ƒ|}|r¦t|ƒdkr„dnd}d |¡}td|›d|›ƒ‚t|d ƒr¸|d7}| d d¡|d <|d d ksà|d dkròt    d|d ›ƒ‚t
|d t ƒs\t    d|d ›ƒ‚q\d |kr0t|ƒkr<nnt dƒ‚i}    g}
t |dd„ddD]D}|d |    kr†|g|    |d <|
 |d ¡n|    |d  |¡qV|
D]Ž} |    | } tt| ƒƒD]r} t| ƒd}t d |¡}|  |¡}| ¡}| |¡| d d¡ztf|ŽWStk
r&YnXq¸q t    dƒ‚dS)aªReturn a MySQL connection and try to failover if needed.
 
    An InterfaceError is raise when no MySQL is available. ValueError is
    raised when the failover server configuration contains an illegal
    connection argument. Supported arguments are user, password, host, port,
    unix_socket and database. ValueError is also raised when the failover
    argument was not provided.
 
    Returns MySQLConnection instance.
    Úfailoverzfailover argument not providedNÚuserÚpasswordÚhostÚportÚ unix_socketÚdatabaserr!Úpriorityrr ÚsÚz, z Unsupported connection argument z in failover: édz9Priority value should be in the range of 0 to 100, got : zDPriority value should be an integer in the range of 0 to 100, got : z\You must either assign no priority to any of the routers or give a priority for every routercSs|dS)Nr6r,©Úxr,r,r-Ú<lambda>®óz*_get_failover_connection.<locals>.<lambda>T)ÚkeyÚreversez,Unable to connect to any of the target hosts)ÚcopyÚKeyErrorÚ
ValueErrorÚsetÚkeysÚlenÚjoinÚhasattrr$rr)ÚintrÚsortedÚappendÚrangeÚrandomÚrandintÚpopÚupdateÚconnectr)rÚconfigr/Zsupport_cnx_argsZpriority_countÚserverZdiffÚargÚlstZserver_directoryZserver_priority_listr6Z failover_listÚ_ÚlastÚindexÚ
new_configr,r,r-Ú_get_failover_connectionjs~  ÷ÿ
ÿ
 ÿ ÿ ÿ  
 
 rY)Úargsrrcsþdˆkrˆ d¡nd}t|tƒs(tdƒ‚|rts:tdƒ‚dˆkrJtdƒ‚dˆkrZtdƒ‚d    ˆkrjtd
ƒ‚d ˆkr~td ˆd <ztj ˆd d ¡}Wn.tj    j
k
rÂtd ˆd ›dƒd‚YnXg}|D](}|  |j j dd|j|j|jdœ¡qÌ|jdd„ddd„|Dƒˆd    <dˆkr8ˆdˆd<ˆ d¡dˆkrVtfˆŽ}tf|ŽSd    ˆkrjtfˆŽSz(t‡fdd„tDƒƒrtfˆŽWSWntk
r¨YnXˆ dd¡}dˆkrވd=|sÞtdkrÞttƒ‚trô|sôt|ˆŽSt|ˆŽS)aCreate or get a MySQL connection object.
 
    In its simpliest form, connect() will open a connection to a
    MySQL server and return a MySQLConnection object.
 
    When any connection pooling arguments are given, for example pool_name
    or pool_size, a pool is created or a previously one is used to return
    a PooledMySQLConnection.
 
    Returns MySQLConnection or PooledMySQLConnection.
    Údns_srvFz(The value of 'dns-srv' must be a booleanztMySQL host configuration requested DNS SRV. This requires the Python dnspython module. Please refer to documentationr4z<Using Unix domain sockets with DNS SRV lookup is not allowedr3z;Specifying a port number with DNS SRV lookup is not allowedr/zASpecifying multiple hostnames with DNS SRV look up is not allowedr2ZSRVz Unable to locate any hosts for 'r"NT)Zomit_final_dot)r2r3r6ÚweightcSs|d|d fS)Nr6r\r,r:r,r,r-r<r=zconnect.<locals>.<lambda>)r>cSsg|]}|d|ddœ‘qS)r2r3)r2r3r,)Ú.0Úsrvr,r,r-Ú
<listcomp>szconnect.<locals>.<listcomp>Zread_default_fileZ option_filesc3s|]}|ˆkVqdS©Nr,)r]r>©rr,r-Ú    <genexpr>szconnect.<locals>.<genexpr>r )rNr)ÚboolrÚHAVE_DNSPYTHONrÚdnsÚresolverÚqueryÚ    exceptionZ DNSExceptionrJÚtargetZto_textr3r6r\ÚsortrrPrYÚanyrr.Ú    NameErrorr$r r%r&r)rZrr[Z srv_recordsr/r^rXr r,rar-rPÇs€
ÿÿÿÿ ÿþ üÿ    ÿ
 
 
 
 
 
 
 
 
 
rPÚstrc    KsPg}dD]0}z| t||ƒ¡Wqtk
r6YqXq|sFtdƒ‚d |¡S)zøGenerate a pool name
 
    This function takes keyword arguments, usually the connection
    arguments for MySQLConnection, and tries to generate a name for
    a pool.
 
    Raises PoolError when no name can be generated.
 
    Returns a string.
    )r2r3r0r5z.Failed generating pool name; specify pool_namerU)rJrmrArrF)rÚpartsr>r,r,r-r#)s r#c@sˆeZdZdZddddœdd„Zddœd    d
„Zd d d ddœdd„Zdddœdd„Zddœdd„Ze    dddœdd„ƒZ
e ddœdd„ƒZ dS)raClass holding a MySQL Connection in a pool
 
    PooledMySQLConnection is used by MySQLConnectionPool to return an
    instance holding a MySQL connection. It works like a MySQLConnection
    except for methods like close() and config().
 
    The close()-method will add the connection back to the pool rather
    than disconnecting from the MySQL server.
 
    Configuring the connection have to be done through the MySQLConnectionPool
    method set_config(). Using config() on pooled connection will raise a
    PoolError.
    r(ú(Union[MySQLConnection, CMySQLConnection]ÚNone)ÚpoolÚcnxrcCs4t|tƒstdƒ‚t|tƒs$tdƒ‚||_||_dS)z‰Initialize
 
        The pool argument must be an instance of MySQLConnectionPoll. cnx
        if an instance of MySQLConnection.
        z$pool should be a MySQLConnectionPoolzcnx should be a MySQLConnectionN)r)r(r+rÚ    _cnx_poolÚ_cnx)Úselfrqrrr,r,r-Ú__init__Ps 
 
zPooledMySQLConnection.__init__©rcCs|Sr`r,©rur,r,r-Ú    __enter___szPooledMySQLConnection.__enter__zType[BaseException]Ú BaseExceptionr)Úexc_typeÚ    exc_valueÚ    tracebackrcCs | ¡dSr`)Úclose)rur{r|r}r,r,r-Ú__exit__bszPooledMySQLConnection.__exit__r)ÚattrrcCs t|j|ƒS)z0Calls attributes of the MySQLConnection instance)Úgetattrrt)rur€r,r,r-Ú __getattr__jsz!PooledMySQLConnection.__getattr__c    Cs4z|j}|jjr| ¡W5|j |¡d|_XdS)aYDo not close, but add connection back to pool
 
        The close() method does not close the connection with the
        MySQL server. The connection is added back to the pool so it
        can be reused.
 
        When the pool is configured to reset the session, the session
        state will be cleared by re-authenticating the user.
        N)rsÚadd_connectionrtÚ reset_session©rurrr,r,r-r~ns
  zPooledMySQLConnection.closerrcKs tdƒ‚dS)z&Configuration is done through the poolzKConfiguration for pooled connections should be done through the pool itselfN)rrar,r,r-rQ€sÿzPooledMySQLConnection.configrmcCs|jjS©z&Return the name of the connection pool)rsrrxr,r,r-rˆszPooledMySQLConnection.pool_nameN) Ú__name__Ú
__module__Ú __qualname__Ú__doc__rvryrr‚r~Ú staticmethodrQÚpropertyrr,r,r,r-rAsc@sÌeZdZdZd*ddddd    d
œd d „Zed dœdd„ƒZeddœdd„ƒZeddœdd„ƒZdd    dœdd„Z    dd    dœdd„Z
d d    dœdd„Z dd    dœd d!„Z d+d"d    dœd#d$„Z d%dœd&d'„Zddœd(d)„ZdS),r(z*Class defining a pool of MySQL connectionséNTrHz Optional[str]rcrrp)r!rÚpool_reset_sessionrrcKs€d|_d|_||_| |¡| |p,tf|Ž¡i|_t |j¡|_    t
ƒ|_ |r||j f|Žd}||jkr||  ¡|d7}q`dS)zõInitialize
 
        Initialize a MySQL connection pool with a maximum number of
        connections set to pool_size. The rest of the keywords
        arguments, kwargs, are configuration arguments for MySQLConnection
        instances.
        Nrr )Ú
_pool_sizeÚ
_pool_nameÚ_reset_sessionÚ_set_pool_sizeÚ_set_pool_namer#Ú _cnx_configÚqueueÚQueueÚ
_cnx_queuer Ú_config_versionÚ
set_configrƒ)rur!rrŽrÚcntr,r,r-rv‘s
 
þ 
zMySQLConnectionPool.__init__rmrwcCs|jSr†)rrxr,r,r-r±szMySQLConnectionPool.pool_namecCs|jS)z0Return number of connections managed by the pool)rrxr,r,r-r!¶szMySQLConnectionPool.pool_sizecCs|jS)zReturn whether to reset session)r‘rxr,r,r-r„»sz!MySQLConnectionPool.reset_sessionrc Kst|sdSt^z$tƒ}|jf|Ž||_tƒ|_Wn2tk
rd}ztd|›ƒ|‚W5d}~XYnXW5QRXdS)aTSet the connection configuration for MySQLConnection instances
 
        This method sets the configuration used for creating MySQLConnection
        instances. See MySQLConnection for valid connection arguments.
 
        Raises PoolError when a connection argument is not valid, missing
        or not supported by MySQLConnection.
        Nz$Connection configuration not valid: )r'rPrQr”r r˜r+r)rurZtest_cnxÚerrr,r,r-r™Às      zMySQLConnectionPool.set_config)r!rcCs(|dks|tkrtdt›ƒ‚||_dS)aSet the size of the pool
 
        This method sets the size of the pool but it will not resize the pool.
 
        Raises an AttributeError when the pool_size is not valid. Invalid size
        is 0, negative or higher than pooling.CNX_POOL_MAXSIZE.
        rz8Pool size should be higher than 0 and lower or equal to N)ÚCNX_POOL_MAXSIZEr+r)rur!r,r,r-r’Õs
ÿz"MySQLConnectionPool._set_pool_size)rrcCs@t |¡rtd|›dƒ‚t|ƒtkr6td|›dƒ‚||_dS)aSet the name of the pool.
 
        This method checks the validity and sets the name of the pool.
 
        Raises an AttributeError when pool_name contains illegal characters
        ([^a-zA-Z0-9._\-*$#]) or is longer than pooling.CNX_POOL_MAXNAMESIZE.
        z Pool name 'z' contains illegal charactersz ' is too longN)ÚCNX_POOL_NAMEREGEXÚsearchr+rEÚCNX_POOL_MAXNAMESIZEr)rurr,r,r-r“äs
 
 z"MySQLConnectionPool._set_pool_namero)rrrc
CsZt|tƒstdƒ‚z|jj|ddWn.tjk
rT}ztdƒ|‚W5d}~XYnXdS)zôPut connection back in the queue
 
        This method is putting a connection back in the queue. It will not
        acquire a lock as the methods using _queue_connection() will have it
        set.
 
        Raises PoolError on errors.
        ú3Connection instance not subclass of MySQLConnectionF©Úblockú'Failed adding connection; queue is fullN)r)rrr—Úputr•ÚFull©rurrr›r,r,r-Ú_queue_connectionòs     
z%MySQLConnectionPool._queue_connectionz2Optional[Union[MySQLConnection, CMySQLConnection]]c    Cs¨tš|jstdƒ‚|j ¡r&tdƒ‚|s~tf|jŽ}z(|jr\|jdr\| ¡dkr\tdƒ‚Wnt    k
rrYnX|j
|_ nt |t ƒstdƒ‚| |¡W5QRXdS)aáAdd a connection to the pool
 
        This method instantiates a MySQLConnection using the configuration
        passed when initializing the MySQLConnectionPool instance or using
        the set_config() method.
        If cnx is a MySQLConnection instance, it will be added to the
        queue.
 
        Raises PoolError when no configuration is set, when no more
        connection can be added (maximum reached) or when the connection
        can not be instantiated.
        z&Connection configuration not availabler£Úcompress)rééz^Pool reset session is not supported with compression for MySQL server version 5.7.2 or earlierr N)r'r”rr—ÚfullrPr‘Zget_server_versionrrAr˜Úpool_config_versionr)rr§r…r,r,r-rƒs2
 ÿþ
ýÿ
 
ÿz"MySQLConnectionPool.add_connectionrc CsÀt²z|jjdd}Wn.tjk
rF}ztdƒ|‚W5d}~XYnX| ¡r\|j|jkr |j    f|j
Žz |  ¡Wn t k
r–|  |¡‚YnX|j|_t||ƒW5QR£SQRXdS)awGet a connection from the pool
 
        This method returns an PooledMySQLConnection instance which
        has a reference to the pool that created it, and the next available
        MySQL connection.
 
        When the MySQL connection is not connect, a reconnect is attempted.
 
        Raises PoolError on errors.
 
        Returns a PooledMySQLConnection instance.
        Fr¡z)Failed getting connection; pool exhaustedN)r'r—r$r•ÚEmptyrZ is_connectedr˜r¬rQr”Z    reconnectrr§rr¦r,r,r-r*2s" ÿ
þ 
z"MySQLConnectionPool.get_connectionc
Cs¢t”d}|j}| ¡rˆz |jdd}| ¡|d7}Wqtjk
r^|YW5QR£Stk
rr‚Yqtk
r„YqXq|W5QR£SQRXdS)z¹Close all connections
 
        This method closes all connections. It returns the number
        of connections it closed.
 
        Used mostly for tests.
 
        Returns int.
        rFr¡r N)    r'r—Úqsizer$Z
disconnectr•r­rr)ruršZcnxqrrr,r,r-Ú_remove_connectionsTs
  z'MySQLConnectionPool._remove_connections)rNT)N)r‡rˆr‰rŠrvrŒrr!r„r™r’r“r§rƒr*r¯r,r,r,r-r(Žs&ü ÿ/"r()7rŠÚ
__future__rr•rLÚreÚ    threadingÚtypesrÚtypingrrrrrr    r
Úuuidr Z dns.exceptionreZ dns.resolverr%rdZconnection_cextr Ú
connectionrÚ    constantsrrÚerrorsrrrrrZ optionfilesrÚRLockr'rœrŸÚcompilerr&rÚ__annotations__rr.rYrPr#rr(r,r,r,r-Ú<module>sH  $  
 
 
ÿ
 ]bM