/usr/lib/python2.7/dist-packages/fedmsg/utils.py is in python-fedmsg 0.9.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | # This file is part of fedmsg.
# Copyright (C) 2012 - 2014 Red Hat, Inc.
#
# fedmsg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# fedmsg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with fedmsg; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors: Ralph Bean <rbean@redhat.com>
#
import six
import zmq
import logging
import inspect
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
def set_high_water_mark(socket, config):
""" Set a high water mark on the zmq socket. Do so in a way that is
cross-compatible with zeromq2 and zeromq3.
"""
if config['high_water_mark']:
if hasattr(zmq, 'HWM'):
# zeromq2
socket.setsockopt(zmq.HWM, config['high_water_mark'])
else:
# zeromq3
socket.setsockopt(zmq.SNDHWM, config['high_water_mark'])
socket.setsockopt(zmq.RCVHWM, config['high_water_mark'])
# TODO -- this should be in kitchen, not fedmsg
def guess_calling_module(default=None):
# Iterate up the call-stack and return the first new top-level module
for frame in (f[0] for f in inspect.stack()):
modname = frame.f_globals['__name__'].split('.')[0]
if modname != "fedmsg":
return modname
# Otherwise, give up and just return the default.
return default
def set_tcp_keepalive(socket, config):
""" Set a series of TCP keepalive options on the socket if
and only if
1) they are specified explicitly in the config and
2) the version of pyzmq has been compiled with support
We ran into a problem in FedoraInfrastructure where long-standing
connections between some hosts would suddenly drop off the
map silently. Because PUB/SUB sockets don't communicate
regularly, nothing in the TCP stack would automatically try and
fix the connection. With TCP_KEEPALIVE options (introduced in
libzmq 3.2 and pyzmq 2.2.0.1) hopefully that will be fixed.
See the following
- http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
- http://api.zeromq.org/3-2:zmq-setsockopt
"""
keepalive_options = {
# Map fedmsg config keys to zeromq socket constants
'zmq_tcp_keepalive': 'TCP_KEEPALIVE',
'zmq_tcp_keepalive_cnt': 'TCP_KEEPALIVE_CNT',
'zmq_tcp_keepalive_idle': 'TCP_KEEPALIVE_IDLE',
'zmq_tcp_keepalive_intvl': 'TCP_KEEPALIVE_INTVL',
}
for key, const in keepalive_options.items():
if key in config:
attr = getattr(zmq, const, None)
if attr:
socket.setsockopt(attr, config[key])
def set_tcp_reconnect(socket, config):
""" Set a series of TCP reconnect options on the socket if
and only if
1) they are specified explicitly in the config and
2) the version of pyzmq has been compiled with support
Once our fedmsg bus grew to include many hundreds of endpoints, we started
notices a *lot* of SYN-ACKs in the logs. By default, if an endpoint is
unavailable, zeromq will attempt to reconnect every 100ms until it gets a
connection. With this code, you can reconfigure that to back off
exponentially to some max delay (like 1000ms) to reduce reconnect storm
spam.
See the following
- http://api.zeromq.org/3-2:zmq-setsockopt
"""
reconnect_options = {
# Map fedmsg config keys to zeromq socket constants
'zmq_reconnect_ivl': 'RECONNECT_IVL',
'zmq_reconnect_ivl_max': 'RECONNECT_IVL_MAX',
}
for key, const in reconnect_options.items():
if key in config:
attr = getattr(zmq, const, None)
if attr:
socket.setsockopt(attr, config[key])
def load_class(location):
""" Take a string of the form 'fedmsg.consumers.ircbot:IRCBotConsumer'
and return the IRCBotConsumer class.
"""
mod_name, cls_name = location = location.strip().split(':')
tokens = mod_name.split('.')
fromlist = '[]'
if len(tokens) > 1:
fromlist = '.'.join(tokens[:-1])
module = __import__(mod_name, fromlist=fromlist)
try:
return getattr(module, cls_name)
except AttributeError as e:
raise ImportError("%r not found in %r" % (cls_name, mod_name))
def dict_query(dic, query):
""" Query a dict with 'dotted notation'. Returns an OrderedDict.
A query of "foo.bar.baz" would retrieve 'wat' from this::
dic = {
'foo': {
'bar': {
'baz': 'wat',
}
}
}
Multiple queries can be specified if comma-separated. For instance, the
query "foo.bar.baz,foo.bar.something_else" would return this::
OrderedDict({
"foo.bar.baz": "wat",
"foo.bar.something_else": None,
})
"""
if not isinstance(query, six.string_types):
raise ValueError("query must be a string, not %r" % type(query))
def _browse(tokens, d):
""" Recurse through a dict to retrieve a value. """
current, rest = tokens[0], tokens[1:]
if not rest:
return d.get(current, None)
if current in d:
if isinstance(d[current], dict):
return _browse(rest, d[current])
elif rest:
return None
else:
return d[current]
keys = [key.strip().split('.') for key in query.split(',')]
return OrderedDict([
('.'.join(tokens), _browse(tokens, dic)) for tokens in keys
])
|