koi-net 1.1.0b4__py3-none-any.whl → 1.1.0b5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of koi-net might be problematic. Click here for more details.

koi_net/core.py CHANGED
@@ -174,6 +174,7 @@ class NodeInterface(Generic[T]):
174
174
  processor=self.processor,
175
175
  effector=self.effector,
176
176
  actor=self.actor,
177
+ handler_context=self.handler_context,
177
178
  use_kobj_processor_thread=use_kobj_processor_thread
178
179
  )
179
180
 
koi_net/lifecycle.py CHANGED
@@ -1,4 +1,7 @@
1
1
  import logging
2
+ from contextlib import contextmanager, asynccontextmanager
3
+
4
+ from koi_net.context import HandlerContext
2
5
 
3
6
  from .network.behavior import Actor
4
7
  from .effector import Effector
@@ -25,6 +28,7 @@ class NodeLifecycle:
25
28
  processor: ProcessorInterface,
26
29
  effector: Effector,
27
30
  actor: Actor,
31
+ handler_context: HandlerContext,
28
32
  use_kobj_processor_thread: bool
29
33
  ):
30
34
  self.config = config
@@ -33,9 +37,34 @@ class NodeLifecycle:
33
37
  self.processor = processor
34
38
  self.effector = effector
35
39
  self.actor = actor
40
+ self.handler_context = handler_context
36
41
  self.use_kobj_processor_thread = use_kobj_processor_thread
42
+
43
+ @contextmanager
44
+ def run(self):
45
+ try:
46
+ logger.info("Starting node lifecycle...")
47
+ self.start()
48
+ yield
49
+ except KeyboardInterrupt:
50
+ logger.info("Keyboard interrupt!")
51
+ finally:
52
+ logger.info("Stopping node lifecycle...")
53
+ self.stop()
54
+
55
+ @asynccontextmanager
56
+ async def async_run(self):
57
+ try:
58
+ logger.info("Starting async node lifecycle...")
59
+ self.start()
60
+ yield
61
+ except KeyboardInterrupt:
62
+ logger.info("Keyboard interrupt!")
63
+ finally:
64
+ logger.info("Stopping async node lifecycle...")
65
+ self.stop()
37
66
 
38
- def start(self) -> None:
67
+ def start(self):
39
68
  """Starts a node, call this method first.
40
69
 
41
70
  Starts the processor thread (if enabled). Loads event queues into memory. Generates network graph from nodes and edges in cache. Processes any state changes of node bundle. Initiates handshake with first contact (if provided) if node doesn't have any neighbors.
@@ -48,7 +77,7 @@ class NodeLifecycle:
48
77
 
49
78
  # refresh to reflect changes (if any) in config.yaml
50
79
  self.effector.deref(self.identity.rid, refresh_cache=True)
51
-
80
+
52
81
  logger.debug("Waiting for kobj queue to empty")
53
82
  if self.use_kobj_processor_thread:
54
83
  self.processor.kobj_queue.join()
@@ -66,9 +95,7 @@ class NodeLifecycle:
66
95
  """Stops a node, call this method last.
67
96
 
68
97
  Finishes processing knowledge object queue. Saves event queues to storage.
69
- """
70
- logger.info("Stopping node...")
71
-
98
+ """
72
99
  if self.use_kobj_processor_thread:
73
100
  logger.info(f"Waiting for kobj queue to empty ({self.processor.kobj_queue.unfinished_tasks} tasks remaining)")
74
101
  self.processor.kobj_queue.join()
koi_net/poller.py CHANGED
@@ -30,18 +30,11 @@ class NodePoller:
30
30
  self.processor.flush_kobj_queue()
31
31
 
32
32
  def run(self):
33
- try:
34
- self.lifecycle.start()
33
+ with self.lifecycle.run():
35
34
  while True:
36
35
  start_time = time.time()
37
36
  self.poll()
38
37
  elapsed = time.time() - start_time
39
38
  sleep_time = self.config.koi_net.polling_interval - elapsed
40
39
  if sleep_time > 0:
41
- time.sleep(sleep_time)
42
-
43
- except KeyboardInterrupt:
44
- logger.info("Polling interrupted by user.")
45
-
46
- finally:
47
- self.lifecycle.stop()
40
+ time.sleep(sleep_time)
koi_net/server.py CHANGED
@@ -54,8 +54,14 @@ class NodeServer:
54
54
  self._build_app()
55
55
 
56
56
  def _build_app(self):
57
+
58
+ @asynccontextmanager
59
+ async def lifespan(*args, **kwargs):
60
+ async with self.lifecycle.async_run():
61
+ yield
62
+
57
63
  self.app = FastAPI(
58
- lifespan=self.lifespan,
64
+ lifespan=lifespan,
59
65
  title="KOI-net Protocol API",
60
66
  version="1.0.0"
61
67
  )
@@ -85,12 +91,6 @@ class NodeServer:
85
91
  port=self.config.server.port
86
92
  )
87
93
 
88
- @asynccontextmanager
89
- async def lifespan(self, app: FastAPI):
90
- self.lifecycle.start()
91
- yield
92
- self.lifecycle.stop()
93
-
94
94
  def protocol_error_handler(self, request, exc: ProtocolError):
95
95
  logger.info(f"caught protocol error: {exc}")
96
96
  resp = ErrorResponse(error=exc.error_type)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: koi-net
3
- Version: 1.1.0b4
3
+ Version: 1.1.0b5
4
4
  Summary: Implementation of KOI-net protocol in Python
5
5
  Project-URL: Homepage, https://github.com/BlockScience/koi-net/
6
6
  Author-email: Luke Miller <luke@block.science>
@@ -1,14 +1,14 @@
1
1
  koi_net/__init__.py,sha256=b0Ze0pZmJAuygpWUFHM6Kvqo3DkU_uzmkptv1EpAArw,31
2
2
  koi_net/config.py,sha256=47XbQ59GRYFi4rlsoWKlnzMQATcnK70i3qmKTZAGOQk,4087
3
3
  koi_net/context.py,sha256=FwkzjmsyNqUeOeGCuZXtONqs5_MSl1R8-e3IxHuyzTI,1531
4
- koi_net/core.py,sha256=UFERsvnphUCWSQYunMgXlLpJ_XU5yD_1tcqlGO0BV9A,7100
4
+ koi_net/core.py,sha256=aO9caoS8dLafRGheJWzhbp_ym7o7bi_wxds683bly48,7150
5
5
  koi_net/default_actions.py,sha256=TkQR9oj9CpO37Gb5bZLmFNl-Q8n3OxGiX4dvxQR7SaA,421
6
6
  koi_net/effector.py,sha256=gSyZgRxQ91X04UL261e2pXWUfBHnQTGtjSHpc2JufxA,4097
7
7
  koi_net/identity.py,sha256=FvIWksGTqwM7HCevIwmo_6l-t-2tnYkaaR4CanZatL4,569
8
- koi_net/lifecycle.py,sha256=-J5y4i0JdernatLTed8hQbkK26Cs6rm1kZqlx3aQzZA,2727
9
- koi_net/poller.py,sha256=ZvQcZ0RQhViXVhbIpdTZb7_ql3nG0mTcdBamneZKLpA,1361
8
+ koi_net/lifecycle.py,sha256=GL2zltmh-aeBuNVg_rbIgsXMch672w7xkWAXCTjxst4,3550
9
+ koi_net/poller.py,sha256=bIrlqdac5vLQYAid35xiQJLDMR85GnOSPCXSTQ07-Mc,1173
10
10
  koi_net/secure.py,sha256=cGNF2assqCaYq0i0fhQBm7aREoAdpY-XVypDsE1ALaU,3970
11
- koi_net/server.py,sha256=5BVVrgn69r_HMC3c4npWywPsjBDqUnHFWLKdtnfRTbA,4250
11
+ koi_net/server.py,sha256=dZfSKrNHqIVD1qc9WkYRO30L4so-A7iW4IsX63oSmlE,4265
12
12
  koi_net/network/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  koi_net/network/behavior.py,sha256=NZLvWlrxR0uWriE3ZzCXmocUVccQthy7Xx8E_8KBwsg,1208
14
14
  koi_net/network/error_handler.py,sha256=CrmCpBY2oj4nl7uXrIYusUHDKxPZ1HDuQAtiBSZarRI,1623
@@ -32,7 +32,7 @@ koi_net/protocol/errors.py,sha256=A83QiYe_fJdxW2lsNsLCujWxDr5sk1UmYYd3TGbSNJA,60
32
32
  koi_net/protocol/event.py,sha256=eGgihEj1gliLoQRk8pVB2q_was0AGo-PbT3Hqnpn3oU,1379
33
33
  koi_net/protocol/node.py,sha256=7GQzHORFr9cP4BqJgir6EGSWCskL-yqmvJksIiLfcWU,409
34
34
  koi_net/protocol/secure.py,sha256=Reem9Z4le4uWXM9uczNOdmgVBg8p4YQav-7_c3pZ1CQ,3366
35
- koi_net-1.1.0b4.dist-info/METADATA,sha256=olgGAhiNBqMAwhtLYCaK0QNNS3s0LuNzHBwazVELAqk,37118
36
- koi_net-1.1.0b4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
37
- koi_net-1.1.0b4.dist-info/licenses/LICENSE,sha256=03mgCL5qth2aD9C3F3qNVs4sFJSpK9kjtYCyOwdSp7s,1069
38
- koi_net-1.1.0b4.dist-info/RECORD,,
35
+ koi_net-1.1.0b5.dist-info/METADATA,sha256=mEMpi26qrfQMeiULMmoDf3iBUVGb8bvgXhTVa9BqhvU,37118
36
+ koi_net-1.1.0b5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
37
+ koi_net-1.1.0b5.dist-info/licenses/LICENSE,sha256=03mgCL5qth2aD9C3F3qNVs4sFJSpK9kjtYCyOwdSp7s,1069
38
+ koi_net-1.1.0b5.dist-info/RECORD,,