Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
amerck committed Jan 15, 2020
1 parent bed7da0 commit 055ef46
Show file tree
Hide file tree
Showing 84 changed files with 7,416 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM alpine:3.7 AS builder

RUN apk --no-cache add python3
RUN python3 -m venv /app

COPY requirements.txt /requirements.txt
RUN /app/bin/pip install -r requirements.txt

COPY setup.py /src/setup.py
COPY hpfeeds /src/hpfeeds
RUN /app/bin/pip install /src


FROM alpine:3.7

RUN apk --no-cache add sqlite python3

COPY --from=builder /app /app

RUN mkdir /app/var
WORKDIR /app/var
VOLUME /app/var

EXPOSE 20000/tcp
EXPOSE 9431/tcp

CMD ["/app/bin/hpfeeds-broker", "--bind=0.0.0.0:10000"]
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
recursive-include hpfeeds *
include *
global-exclude *.pyc
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
ignore:
- setup.py
14 changes: 14 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys


collect_ignore = []
if sys.version_info[0] == 2:
collect_ignore.extend([
"hpfeeds/tests/test_asyncio_client.py",
"hpfeeds/tests/test_asyncio_protocol.py",
"hpfeeds/tests/test_broker_connection.py",
"hpfeeds/tests/test_broker_prometheus.py",
"hpfeeds/tests/test_client_integration.py",
"hpfeeds/tests/test_twisted_service.py",
])

20 changes: 20 additions & 0 deletions dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
aiomock==0.1.0; python_version > '3.5'
certifi==2018.1.18
codecov==2.0.15
coverage==4.5.1
flake8==3.5.0
flake8-isort==2.5
isort==4.3.4
mccabe==0.6.1
pluggy==0.6.0
py==1.5.2
pycodestyle==2.3.1
pyflakes==1.6.0
pytest==3.3.1
pytest-cov==2.5.1
requests==2.20.1
six==1.11.0
testfixtures==6.0.0
urllib3==1.22

Twisted==18.4.0
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SPHINXPROJ = hpfeeds3
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
114 changes: 114 additions & 0 deletions docs/asyncio_quickstart.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
.. _hpfeeds-asyncio-client-quickstart:


Client Quickstart
=================

.. module:: hpfeeds3.asyncio
.. currentmodule:: hpfeeds3.asyncio


Most common tasks you'll need to perform in asyncio against a hpfeeds broker
can be accomplished with an instance of :class:`ClientSession`.


Publishing an event
-------------------

Usage example::

import asyncio
from hpfeeds.asyncio import ClientSession


async def main():
async with ClientSession('localhost', 20000, 'ident', 'secret') as client:
client.publish('channel', b'{"data": "fefefefefefef"}')


loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Listening to events
-------------------

You can just async for over your client to read from the broker forever::

import asyncio
from hpfeeds.asyncio import ClientSession


async def main():
async with ClientSession('localhost', 20000, 'ident', 'secret') as client:
client.subscribe('channel')

async for ident, channel, payload in client:
print(payload)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Publishing events from an asynchronous iterator
-----------------------------------------------

You can now construct asynchronous generators in Python 3, and then have
hpfeeds3 publish directly from the iterator::

import asyncio

async def test_iterator():
while True:
wait asyncio.sleep(1)
yield b'payload'

async def main():
async with ClientSession('localhost', 20000, 'ident', 'secret') as client:
client.publish_async_iterable('channel', test_iterator())


loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Using hpfeeds with aiostream
----------------------------

aiostream is like an asynchronous version of `itertools`.

For example, you could merge together the output from multiple brokers, perform
transformations on it and send it into another::

import asyncio

from asyncio.stream import iterable, merge
from hpfeeds.asyncio import ClientSession

async def main():
brokers = []
for port in (10000, 10001, 10002):
session = ClientSession('localhost', 10000, 'ident', 'secret')
session.subscribe('in-channel')
brokers.append(session)

pipeline = (
# Merge feed from multiple brokers
merge(*brokers) |

# Decode JSON payload
map(lambda ident, channel, payload: json.loads(payload.decode('utf-8'))) |

# Only interested in events that have hashes associated with them
filter(lambda payload: len(payload['hashes']) > 0) |

# Reencode payload for transmission
map(lambda payload: json.dumps(payload).encode('utf-8'))
)

output = ClientSession('localhost', 10004, 'ident', 'secret')
await output.publish_async_iterable('out-channel', combined)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
57 changes: 57 additions & 0 deletions docs/asyncio_reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.. _hpfeeds-asyncio-client-reference:


Client Reference
================

.. module:: hpfeeds3.asyncio
.. currentmodule:: hpfeeds3.asyncio


Client Session
--------------

Client session is the recommended interface for subscribing and publish to a
hpfeeds broker with asyncio.


.. class:: ClientSession(host, port, ident, secret)

The class for creating client sessions and publish/subscribing.

Instances of this class will automatically maintain a connection to the
broker and try to reconnect if that connection fails.

:param str host: The broker to connect to

:param str port: The port to connect to

:param str ident: The identity to authenticate with

:param str secret: The secret to authenticate with


.. comethod:: read()

Returns a message received by the broker. It's future will not fire until
a message is available.

.. method:: publish(channel, payload)

:param str channel: The channel to post the payload to.

:param bytes payload: The data to publish to the broker.

Send the given payload to the given channel.

.. method:: subscribe(channel)

:param str channel: The channel to subscribe to.

Subscribe to the named channel.

.. method:: unsubscribe(channel)

:param str channel: The channel to subscribe to.

Unsubscribe from the named channel.
Loading

0 comments on commit 055ef46

Please sign in to comment.