Peer
API reference for diameter.node.peer
.
PEER_RECV
module-attribute
¶
PEER_RECV = 1
Peer is a server, i.e. receives requests and sends answers.
PEER_SEND
module-attribute
¶
PEER_SEND = 2
Peer is a client, i.e. sends requests and receives answers.
PEER_CONNECTING
module-attribute
¶
PEER_CONNECTING = 16
Peer is in a state waiting for socket to become active.
PEER_CONNECTED
module-attribute
¶
PEER_CONNECTED = 17
Peer has established connection and is waiting for initial CER/CEA to complete.
PEER_READY_WAITING_DWA
module-attribute
¶
PEER_READY_WAITING_DWA = 19
Peer is ready to process messages, but is waiting for a DWA.
PEER_DISCONNECTING
module-attribute
¶
PEER_DISCONNECTING = 26
Peer has sent a Disconnect-Peer-Request and is waiting for DPA.
PEER_CLOSING
module-attribute
¶
PEER_CLOSING = 27
Peer is about to be closed; it will no longer read any messages and will close its socket as soon as the write buffer has been emptied. This state is not part of rfc6733, it is only an internal temporary flag.
DISCONNECT_REASON_DPR
module-attribute
¶
DISCONNECT_REASON_DPR = 32
Peer has been disconnected after receiving a peer disconnect request.
DISCONNECT_REASON_NODE_SHUTDOWN
module-attribute
¶
DISCONNECT_REASON_NODE_SHUTDOWN = 33
Peer has been disconnected, because node shutdown has been initiated.
DISCONNECT_REASON_CLEAN_DISCONNECT
module-attribute
¶
DISCONNECT_REASON_CLEAN_DISCONNECT = 34
Peer has been disconnected due to no errors.
DISCONNECT_REASON_SOCKET_FAIL
module-attribute
¶
DISCONNECT_REASON_SOCKET_FAIL = 48
Peer has been disconnected, because the underlying socket has failed.
DISCONNECT_REASON_GONE_AWAY
module-attribute
¶
DISCONNECT_REASON_GONE_AWAY = 49
Peer has been disconnected, because the socket endpoint has gone away (zero bytes read from socket).
DISCONNECT_REASON_FAILED_CONNECT
module-attribute
¶
DISCONNECT_REASON_FAILED_CONNECT = 50
Peer has been disconnected, because the initial socket failed to connect.
DISCONNECT_REASON_FAILED_CONNECT_CE
module-attribute
¶
DISCONNECT_REASON_FAILED_CONNECT_CE = 51
Peer has been disconnected, because the capabilities exchange failed to complete after initial socket connect, e.g. due to a timeout.
DISCONNECT_REASON_CER_REJECTED
module-attribute
¶
DISCONNECT_REASON_CER_REJECTED = 52
Peer has been disconnected, because the sent capabilities exchange request was rejected by the other peer.
DISCONNECT_REASON_DWA_TIMEOUT
module-attribute
¶
DISCONNECT_REASON_DWA_TIMEOUT = 53
Peer has been disconnected, because there was no response to a device watchdog request within the configured timeout.
DISCONNECT_REASON_UNKNOWN
module-attribute
¶
DISCONNECT_REASON_UNKNOWN = 64
Peer has been disconnected for unknown reasons.
PeerCounters
dataclass
¶
PeerCounters(
cer: int = 0,
cea: int = 0,
dwr: int = 0,
dwa: int = 0,
dpr: int = 0,
dpa: int = 0,
requests: int = 0,
answers: int = 0,
)
Peer message counters.
PeerStats ¶
PeerStats()
Peer statistics.
processed_req_per_second
property
¶
processed_req_per_second: dict[str, float]
Rate of requests processed per second, split by message type.
Returns:
Type | Description |
---|---|
dict[str, float]
|
A dictionary with keys of diameter command names (e.g. "Credit-Control", "Device-Watchdog") and values of requests processed per second, derived from the total sum of work over the past 1024 requests for each message type. |
processed_req_per_second_overall
property
¶
processed_req_per_second_overall: float
Overall rate of requests processed per second.
Derived from the total sum of work time over the past 1024 received requests.
avg_response_time
property
¶
avg_response_time: dict[str, float]
Average response time, split by message type.
Returns:
Type | Description |
---|---|
dict[str, float]
|
A dictionary with keys of diameter command names (e.g. "Credit-Control", "Device-Watchdog") and values of response times in seconds, as an average over the last 1024 processed answers. |
avg_response_time_overall
property
¶
avg_response_time_overall: float
Overall average response time.
Derived from the total sum of work time over the past 1024 processed answers.
Peer
dataclass
¶
Peer(
node_name: str,
realm_name: str,
transport: int,
port: int,
ip_addresses: list[str] = list(),
persistent: bool = False,
always_reconnect: bool = False,
cea_timeout: int = None,
cer_timeout: int = None,
dwa_timeout: int = None,
idle_timeout: int = None,
reconnect_wait: int = 30,
disconnect_reason: int | None = None,
last_connect: int = None,
last_disconnect: int = None,
counters: PeerCounters = PeerCounters(),
statistics: PeerStats = PeerStats(),
connection: PeerConnection = None,
)
Single configured or known peer.
Collects all settings and a few timers for a single peer. The node collects
one instance of Peer
for every configured peer, or every discovered
unknown peer. There exists one peer for each FQDN. An instance of Peer
exists whether the peer is currently connected or not. The state peer
connectivity is determined by the connection
attribute.
transport
instance-attribute
¶
transport: int
Transport method, either PEER_TRANSPORT_TCP
or
PEER_TRANSPORT_SCTP
.
port
instance-attribute
¶
port: int
Port number is always set, even if the peer has not been configured for outgoing connections. It defaults to 3868.
ip_addresses
class-attribute
instance-attribute
¶
ip_addresses: list[str] = field(default_factory=list)
A list of IP addresses configured for the peer.
persistent
class-attribute
instance-attribute
¶
persistent: bool = False
Indicates that the connection to the peer is automatically established,
at Node startup and at connection lost (see reconnect_wait
timer). A
connection is automatically established regardless of whether the node acts
as a server or a client.
always_reconnect
class-attribute
instance-attribute
¶
always_reconnect: bool = False
Indicates that the connection to the peer should always be attempted to be re-established, even if the peer has disconnected cleanly after a DPR/DPA procedure.
cea_timeout
class-attribute
instance-attribute
¶
cea_timeout: int = None
Timeout waiting for a CEA after sending a CER. If no CEA is received within this timeframe, the connection to the peer is closed.
cer_timeout
class-attribute
instance-attribute
¶
cer_timeout: int = None
Timeout waiting for a CER after receiving a connection attempt. If the peer does not send a CER within this timeframe, the connection is closed.
dwa_timeout
class-attribute
instance-attribute
¶
dwa_timeout: int = None
Timeout waiting for a DWA after sending a DWR. If no DWA is received within this timeframe, the connection to the peer is closed.
idle_timeout
class-attribute
instance-attribute
¶
idle_timeout: int = None
Time spent idle before a DWR is triggered.
reconnect_wait
class-attribute
instance-attribute
¶
reconnect_wait: int = 30
Time waited before a reconnect is attempted for a persistent peer.
The wait time is only applied in a scenario where the peer connection has
failed least once and has the persistent
attribute enabled. If the
peer has not yet (ever) been disconnected, a connection attempt is made
immediately.
disconnect_reason
class-attribute
instance-attribute
¶
disconnect_reason: int | None = None
Reason for the peer having been disconnected. One of the
PEER_DISCONNECT_REASON_*
constants, or None
if the peer has not yet
been disconnected. The value is reset back to None
after a peer has
been reconnected.
last_connect
class-attribute
instance-attribute
¶
last_connect: int = None
Unix timestamp of last successful connect.
last_disconnect
class-attribute
instance-attribute
¶
last_disconnect: int = None
Unix timestamp of last disconnect.
counters
class-attribute
instance-attribute
¶
counters: PeerCounters = field(default_factory=PeerCounters)
Peer message counters.
statistics
class-attribute
instance-attribute
¶
Peer connection statistics.
connection
class-attribute
instance-attribute
¶
connection: PeerConnection = None
The actual, current connection to the peer. If the peer is not
connected, the value will be None
. Note that even if the peer may be
connected, the actual connection readiness is determined by the
Peer.connection.state
attribute.
disconnected_since
property
¶
disconnected_since: int
Time since last disconnect, as seconds. If the peer has never been disconnected, the value -1 is returned.
PeerConnection ¶
PeerConnection(
peer_ip: list[str] | str,
peer_port: int,
peer_direction: int,
interrupt_fileno: int,
)
A connection with another diameter node.
Instances of this class are assigned as the value for the
Peer.connection
attribute.
Connections are created and closed by the parent governing diameter node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
peer_ip
|
list[str] | str
|
Either a list of possible IP addresses to connect to, or an individual IP address that the connection socket is already connected with. |
required |
peer_port
|
int
|
Peer connection port number |
required |
peer_direction
|
int
|
Indicates whether the connection is either a receiving or a sending instance |
required |
interrupt_fileno
|
int
|
A write socket/pipe file number that this
connection will write its connection ID every time it needs
attention. This occurs most often when the connection has
something to write and needs to wake up the parent node's
|
required |
auth_application_ids
instance-attribute
¶
auth_application_ids: list[int] = []
List of supported authentication application IDs for this peer. The list is populated when CER/CEA has been completed and will be used by the node to route messages to their proper applications.
acct_application_ids
instance-attribute
¶
acct_application_ids: list[int] = []
List of supported accounting application IDs for this peer. The list is populated when CER/CEA has been completed and will be used by the node to route messages to their proper applications.
hop_by_hop_seq
instance-attribute
¶
hop_by_hop_seq = SequenceGenerator()
A sequence generator that will produce unique hop-by-hop IDs. Use
PeerConnection.hop_by_hop_seq.next_sequence()
to retrieve the next
ID.
host_identity
instance-attribute
¶
host_identity: str = ''
Resolved peer host ID. Will be set after CER/CEA has taken place.
host_ip_address
instance-attribute
¶
host_ip_address: list[str] = []
Node's host IP addresses, resolved at the time of peer creation.
ident
instance-attribute
¶
ident: str = '00' * 6
A unique (for the lifetime of the parent node) connection identifier, a 6-byte long hexadecimal string.
ip
instance-attribute
¶
ip: list[str] | str = peer_ip
The actual peer IP address(es) that the connection socket is connected with.
message_handler
instance-attribute
¶
message_handler: Callable[[PeerConnection, _AnyMessageType], None] = (
lambda p, m: None
)
A callback function that will be called each time a diameter
message is received. This should always be Node._receive_message
.
node_name
instance-attribute
¶
node_name: str = ''
Configured node name. Is set for every known peer and should always
equal host_identity
. If connections from unknown peers are accepted,
this attribute remains always empty.
origin_host
instance-attribute
¶
origin_host: str = ''
The value that will be used in sent requests, in the Host-Origin AVP.
socket_fileno
instance-attribute
¶
socket_fileno: int = 0
The ID of the underlying socket. The peer does not hold the socket itself, only the ID. The sockets are tracked by the parent node.
socket_proto
instance-attribute
¶
socket_proto: int = 0
Connected socket protocol, either PEER_TRANSPORT_TCP or PEER_TRANSPORT_SCTP.
state
instance-attribute
¶
state: int = PEER_CLOSED
The current peer state, one of PEER_*
constants. The peer will
go through a transition of CONNECTING - CONNECTED - READY and will not
handle any messages until the READY state has been reached.
is_receiver
property
¶
is_receiver: bool
Indicates the direction of connectivity. A receiver is a connection that has been established by a foreign peer, towards us. A receiver can both send and receive diameter messages, this property affects mostly only the CER/CEA procedure.
is_sender
property
¶
is_sender: bool
Indicates the direction of connectivity. A sender is a connection that has been by our node, towards a foreign peer. A sender can both send and receive diameter messages, this property affects mostly only the CER/CEA procedure.
is_waiting_for_dwa
property
¶
is_waiting_for_dwa
Indicates that the connection is in a waiting-for-DWA state.
dwa_wait_time
property
¶
dwa_wait_time: int
Time spent waiting for DWA, in seconds. If no DWR has been sent, returns zero.
last_read_since
property
¶
last_read_since: int
Seconds since bytes were last receveid from the network.
add_in_bytes ¶
add_in_bytes(read_bytes: bytes)
Add network-received bytes to parse and handle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
read_bytes
|
bytes
|
A byte string. Does not have to contain a complete message; the connection will buffer received bytes internally, until at least one valid message has been received |
required |
add_out_msg ¶
add_out_msg(out_msg: _AnyMessageType)
Add an outgoing Diameter message to send to network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_msg
|
_AnyMessageType
|
A message to send back towards the network. The message is queued internally and sent out as soon as possible. Messages are processed in the order that they were added. |
required |
close ¶
close(signal_node: bool = True)
Close the peer connection.
Sets peer connection state as closed and signals parent Node to close the underlying socket. Also stops processing input and output bytes immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal_node
|
bool
|
Send a signal to parent node so that it knows that the underlying socket should be closed. Should always be set to True, unless the socket has already been closed, before shutting down the peer. |
True
|
demand_attention ¶
demand_attention()
Signal parent node that data can be sent or read for this peer.
remove_out_bytes ¶
remove_out_bytes(sent_bytes: int)
Remove a given amount of bytes from outgoing buffer.
reset_last_message ¶
reset_last_message()
Mark that a full diameter message has been received.
Resets the internal idle counter.
reset_last_read ¶
reset_last_read()
Mark that bytes have been received from the network.
Resets the internal idle counter.
reset_last_dwa ¶
reset_last_dwa()
Mark that a DWA has been received.
Resets the timer that starts counting from a sent DWR. If no DWA is received within the configured timeout period, the peer connection is closed.
reset_last_dwr ¶
reset_last_dwr()
Mark that a DWR has been sent.
Starts the DWA wait timer and changes connection state to PEER_READY_WAITING_DWA.