Repository: abhinavsingh/proxy.py
Pull request #238 (2019-12-20 23:02:14)

Share lock to acceptors via pool
Addresses #233

codecov[bot]: # [Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=h1) Report
> Merging [#238](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=desc) into [develop](https://codecov.io/gh/abhinavsingh/proxy.py/commit/625dc4d3e57df2654cf0cec94d0a07b18f8a1a82?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238/graphs/tree.svg?width=650&token=Zh9J7b4la2&height=150&src=pr)](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           develop     #238      +/-   ##
===========================================
+ Coverage    73.11%   73.13%   +0.01%     
===========================================
  Files           70       70              
  Lines         2786     2788       +2     
===========================================
+ Hits          2037     2039       +2     
  Misses         749      749
```


| [Impacted Files](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [proxy/core/acceptor/pool.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238/diff?src=pr&el=tree#diff-cHJveHkvY29yZS9hY2NlcHRvci9wb29sLnB5) | `80% <100%> (+0.27%)` | :arrow_up: |
| [proxy/core/acceptor/acceptor.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238/diff?src=pr&el=tree#diff-cHJveHkvY29yZS9hY2NlcHRvci9hY2NlcHRvci5weQ==) | `80.51% <100%> (+0.25%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=footer). Last update [625dc4d...33372a4](https://codecov.io/gh/abhinavsingh/proxy.py/pull/238?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/proxy/core/acceptor/acceptor.py b/proxy/core/acceptor/acceptor.py
index 648f51e..eeb2955 100644
--- a/proxy/core/acceptor/acceptor.py
+++ b/proxy/core/acceptor/acceptor.py
@@ -10,6 +10,7 @@
 """
 import logging
 import multiprocessing
+import multiprocessing.synchronize
 import selectors
 import socket
 import threading
@@ -33,20 +34,20 @@ class Acceptor(multiprocessing.Process):
     starts a new work thread.
     """
 
-    lock = multiprocessing.Lock()
-
     def __init__(
             self,
             idd: int,
             work_queue: connection.Connection,
             flags: Flags,
             work_klass: Type[ThreadlessWork],
+            lock: multiprocessing.synchronize.Lock,
             event_queue: Optional[EventQueue] = None) -> None:
         super().__init__()
         self.idd = idd
         self.work_queue: connection.Connection = work_queue
         self.flags = flags
         self.work_klass = work_klass
+        self.lock = lock
         self.event_queue = event_queue
 
         self.running = multiprocessing.Event()
@@ -101,12 +102,13 @@ class Acceptor(multiprocessing.Process):
             work_thread.start()
 
     def run_once(self) -> None:
-        assert self.selector and self.sock
         with self.lock:
+            assert self.selector and self.sock
             events = self.selector.select(timeout=1)
             if len(events) == 0:
                 return
             conn, addr = self.sock.accept()
+
         # now = time.time()
         # fileno: int = conn.fileno()
         self.start_work(conn, addr)
diff --git a/proxy/core/acceptor/pool.py b/proxy/core/acceptor/pool.py
index 4b83b9a..87d7158 100644
--- a/proxy/core/acceptor/pool.py
+++ b/proxy/core/acceptor/pool.py
@@ -24,6 +24,8 @@ from ...common.flags import Flags
 
 logger = logging.getLogger(__name__)
 
+LOCK = multiprocessing.Lock()
+
 
 class AcceptorPool:
     """AcceptorPool.
@@ -66,7 +68,8 @@ class AcceptorPool:
                 work_queue=work_queue[1],
                 flags=self.flags,
                 work_klass=self.work_klass,
-                event_queue=self.event_queue
+                lock=LOCK,
+                event_queue=self.event_queue,
             )
             acceptor.start()
             logger.debug(


Test patch:
diff --git a/tests/core/test_acceptor.py b/tests/core/test_acceptor.py
index 537339d..2b4dbd9 100644
--- a/tests/core/test_acceptor.py
+++ b/tests/core/test_acceptor.py
@@ -29,6 +29,7 @@ class TestAcceptor(unittest.TestCase):
             idd=self.acceptor_id,
             work_queue=self.pipe[1],
             flags=self.flags,
+            lock=multiprocessing.Lock(),
             work_klass=self.mock_protocol_handler)
 
     @mock.patch('selectors.DefaultSelector')