Show More
@@ -1,90 +1,89 b'' | |||
|
1 | 1 | # connectionpool.py - class for pooling peer connections for reuse |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2017 Facebook, Inc. |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | from mercurial import ( |
|
11 | extensions, | |
|
12 | 11 | hg, |
|
13 | 12 | pycompat, |
|
14 | 13 | sshpeer, |
|
15 | 14 | util, |
|
16 | 15 | ) |
|
17 | 16 | |
|
18 | 17 | _sshv1peer = sshpeer.sshv1peer |
|
19 | 18 | |
|
20 | 19 | |
|
21 | 20 | class connectionpool(object): |
|
22 | 21 | def __init__(self, repo): |
|
23 | 22 | self._repo = repo |
|
24 | 23 | self._pool = dict() |
|
25 | 24 | |
|
26 | 25 | def get(self, path): |
|
27 | 26 | pathpool = self._pool.get(path) |
|
28 | 27 | if pathpool is None: |
|
29 | 28 | pathpool = list() |
|
30 | 29 | self._pool[path] = pathpool |
|
31 | 30 | |
|
32 | 31 | conn = None |
|
33 | 32 | if len(pathpool) > 0: |
|
34 | 33 | try: |
|
35 | 34 | conn = pathpool.pop() |
|
36 | 35 | peer = conn.peer |
|
37 | 36 | # If the connection has died, drop it |
|
38 | 37 | if isinstance(peer, _sshv1peer): |
|
39 | 38 | if peer._subprocess.poll() is not None: |
|
40 | 39 | conn = None |
|
41 | 40 | except IndexError: |
|
42 | 41 | pass |
|
43 | 42 | |
|
44 | 43 | if conn is None: |
|
45 | 44 | |
|
46 | 45 | peer = hg.peer(self._repo.ui, {}, path) |
|
47 | 46 | if util.safehasattr(peer, '_cleanup'): |
|
48 | 47 | |
|
49 | 48 | class mypeer(peer.__class__): |
|
50 | 49 | def _cleanup(self, warn=None): |
|
51 | 50 | # close pipee first so peer.cleanup reading it won't |
|
52 | 51 | # deadlock, if there are other processes with pipeo |
|
53 | 52 | # open (i.e. us). |
|
54 | 53 | if util.safehasattr(self, 'pipee'): |
|
55 | 54 | self.pipee.close() |
|
56 | 55 | return super(mypeer, self)._cleanup() |
|
57 | 56 | |
|
58 | 57 | peer.__class__ = mypeer |
|
59 | 58 | |
|
60 | 59 | conn = connection(pathpool, peer) |
|
61 | 60 | |
|
62 | 61 | return conn |
|
63 | 62 | |
|
64 | 63 | def close(self): |
|
65 | 64 | for pathpool in pycompat.itervalues(self._pool): |
|
66 | 65 | for conn in pathpool: |
|
67 | 66 | conn.close() |
|
68 | 67 | del pathpool[:] |
|
69 | 68 | |
|
70 | 69 | |
|
71 | 70 | class connection(object): |
|
72 | 71 | def __init__(self, pool, peer): |
|
73 | 72 | self._pool = pool |
|
74 | 73 | self.peer = peer |
|
75 | 74 | |
|
76 | 75 | def __enter__(self): |
|
77 | 76 | return self |
|
78 | 77 | |
|
79 | 78 | def __exit__(self, type, value, traceback): |
|
80 | 79 | # Only add the connection back to the pool if there was no exception, |
|
81 | 80 | # since an exception could mean the connection is not in a reusable |
|
82 | 81 | # state. |
|
83 | 82 | if type is None: |
|
84 | 83 | self._pool.append(self) |
|
85 | 84 | else: |
|
86 | 85 | self.close() |
|
87 | 86 | |
|
88 | 87 | def close(self): |
|
89 | 88 | if util.safehasattr(self.peer, 'cleanup'): |
|
90 | 89 | self.peer.cleanup() |
General Comments 0
You need to be logged in to leave comments.
Login now