-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep global registry of SNMP session objects
Before this, every Task instance would create its own SNMP session instance. This might not mean much under PySNMP, but is wasteful using netsnmp-cffi, as each open session takes up a socket resource. Having multiple simultaneous sessions/sockets per poll device makes no sense, so `get_snmp_session()` utilizes a `WeakValueDictionary` to keep track of the existing session for each device and returns and existing session if there is one.
- Loading branch information
1 parent
7ebf942
commit 1af8f28
Showing
2 changed files
with
22 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
"""Zino SNMP back-ends""" | ||
|
||
from weakref import WeakValueDictionary | ||
|
||
from zino.config.models import PollDevice | ||
|
||
# There is only one at the moment: | ||
# from .pysnmp_backend import * # noqa | ||
from .netsnmpy_backend import * # noqa | ||
from .netsnmpy_backend import SNMP | ||
|
||
_snmp_sessions = WeakValueDictionary() | ||
|
||
|
||
def get_snmp_session(device: PollDevice) -> SNMP: | ||
"""Factory function for creating an SNMP session. | ||
This keeps a registry of existing/re-usable SNMP sessions so we avoid duplicate instances and over-use of sockets | ||
""" | ||
# We generate a session key based on every attribute that affects how the SNMP session is set up: | ||
key = (device.address, device.community, device.hcounters) | ||
session = _snmp_sessions.get(key) | ||
if not session: | ||
session = _snmp_sessions[key] = SNMP(device) | ||
return session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters