Skip to content

Commit

Permalink
ndb: Merge pull request svinota#1156 from C2Devel/bugfix/ndb-sources-…
Browse files Browse the repository at this point in the history
…count

Fix NDB Source object's counting and update documentation

Bug-Url: svinota#1156
  • Loading branch information
svinota authored Jan 10, 2024
2 parents 7fc0cb0 + 1573e6b commit e552e6b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pyroute2/ndb/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@
starts with one local RTNL source names `localhost`::
>>> ndb = NDB()
>>> ndb.sources.details()
{'kind': u'local', u'nlm_generator': 1, 'target': u'localhost'}
>>> ndb.sources.summary().format("json")
[
{
"name": "localhost",
"spec": "{'target': 'localhost', 'nlm_generator': 1}",
"state": "running"
},
{
"name": "localhost/nsmanager",
"spec": "{'target': 'localhost/nsmanager'}",
"state": "running"
}
]
>>> ndb.sources['localhost']
[running] <IPRoute {'nlm_generator': 1}>
[running] <IPRoute {'target: 'localhost', 'nlm_generator': 1}>
The `localhost` RTNL source starts an additional async cache thread.
The `nlm_generator` option means that instead of collections the
`IPRoute` object returns generators, so `IPRoute` responses will not
consume memory regardless of the RTNL objects number::
>>> ndb.sources['localhost'].nl.link('dump')
<generator object _match at 0x7fa444961e10>
<generator object RTNL_API.filter_messages at 0x7f61a99a34a0>
See also: :ref:`iproute`
Expand Down Expand Up @@ -180,6 +191,12 @@ def __init__(self, ndb, **spec):
self.ndb.task_manager.db_add_nl_source(self.target, self.kind, spec)
self.load_sql()

@classmethod
def _count(cls, view):
return view.ndb.task_manager.db_fetchone(
"SELECT count(*) FROM %s" % view.table
)

@property
def must_restart(self):
if self.max_errors < 0 or self.errors_counter <= self.max_errors:
Expand Down
1 change: 1 addition & 0 deletions tests/test_linux/test_ndb/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_multiple_sources(context):
assert len(list(ndb.neighbours.dump()))
assert len(list(ndb.addresses.dump()))
assert len(list(ndb.routes.dump()))
assert len(ndb.sources) == len(sources)
# here NDB() gets closed
#

Expand Down

0 comments on commit e552e6b

Please sign in to comment.