|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | + |
| 20 | +""" |
| 21 | +The Pulsar Python client APIs that work with the asyncio module. |
| 22 | +""" |
| 23 | + |
| 24 | +import asyncio |
| 25 | +import functools |
| 26 | +from typing import Any |
| 27 | + |
| 28 | +import _pulsar |
| 29 | +import pulsar |
| 30 | + |
| 31 | +class PulsarException(BaseException): |
| 32 | + """ |
| 33 | + The exception that wraps the Pulsar error code |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self, result: pulsar.Result) -> None: |
| 37 | + """ |
| 38 | + Create the Pulsar exception. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + result: pulsar.Result |
| 43 | + The error code of the underlying Pulsar APIs. |
| 44 | + """ |
| 45 | + self._result = result |
| 46 | + |
| 47 | + def error(self) -> pulsar.Result: |
| 48 | + """ |
| 49 | + Returns the Pulsar error code. |
| 50 | + """ |
| 51 | + return self._result |
| 52 | + |
| 53 | + def __str__(self): |
| 54 | + """ |
| 55 | + Convert the exception to string. |
| 56 | + """ |
| 57 | + return f'{self._result.value} {self._result.name}' |
| 58 | + |
| 59 | +class Producer: |
| 60 | + """ |
| 61 | + The Pulsar message producer, used to publish messages on a topic. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, producer: _pulsar.Producer) -> None: |
| 65 | + """ |
| 66 | + Create the producer. |
| 67 | + Users should not call this constructor directly. Instead, create the |
| 68 | + producer via `Client.create_producer`. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + producer: _pulsar.Producer |
| 73 | + The underlying Producer object from the C extension. |
| 74 | + """ |
| 75 | + self._producer: _pulsar.Producer = producer |
| 76 | + |
| 77 | + async def send(self, content: bytes) -> pulsar.MessageId: |
| 78 | + """ |
| 79 | + Send a message asynchronously. |
| 80 | +
|
| 81 | + parameters |
| 82 | + ---------- |
| 83 | + content: bytes |
| 84 | + The message payload |
| 85 | +
|
| 86 | + Returns |
| 87 | + ------- |
| 88 | + pulsar.MessageId |
| 89 | + The message id that represents the persisted position of the message. |
| 90 | +
|
| 91 | + Raises |
| 92 | + ------ |
| 93 | + PulsarException |
| 94 | + """ |
| 95 | + builder = _pulsar.MessageBuilder() |
| 96 | + builder.content(content) |
| 97 | + future = asyncio.get_running_loop().create_future() |
| 98 | + self._producer.send_async(builder.build(), functools.partial(_set_future, future)) |
| 99 | + msg_id = await future |
| 100 | + return pulsar.MessageId( |
| 101 | + msg_id.partition(), |
| 102 | + msg_id.ledger_id(), |
| 103 | + msg_id.entry_id(), |
| 104 | + msg_id.batch_index(), |
| 105 | + ) |
| 106 | + |
| 107 | + async def close(self) -> None: |
| 108 | + """ |
| 109 | + Close the producer. |
| 110 | +
|
| 111 | + Raises |
| 112 | + ------ |
| 113 | + PulsarException |
| 114 | + """ |
| 115 | + future = asyncio.get_running_loop().create_future() |
| 116 | + self._producer.close_async(functools.partial(_set_future, future, value=None)) |
| 117 | + await future |
| 118 | + |
| 119 | +class Client: |
| 120 | + """ |
| 121 | + The asynchronous version of `pulsar.Client`. |
| 122 | + """ |
| 123 | + |
| 124 | + def __init__(self, service_url, **kwargs) -> None: |
| 125 | + """ |
| 126 | + See `pulsar.Client.__init__` |
| 127 | + """ |
| 128 | + self._client: _pulsar.Client = pulsar.Client(service_url, **kwargs)._client |
| 129 | + |
| 130 | + async def create_producer(self, topic: str) -> Producer: |
| 131 | + """ |
| 132 | + Create a new producer on a given topic |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + topic: str |
| 137 | + The topic name |
| 138 | +
|
| 139 | + Returns |
| 140 | + ------- |
| 141 | + Producer |
| 142 | + The producer created |
| 143 | +
|
| 144 | + Raises |
| 145 | + ------ |
| 146 | + PulsarException |
| 147 | + """ |
| 148 | + future = asyncio.get_running_loop().create_future() |
| 149 | + conf = _pulsar.ProducerConfiguration() |
| 150 | + # TODO: add more configs |
| 151 | + self._client.create_producer_async(topic, conf, functools.partial(_set_future, future)) |
| 152 | + return Producer(await future) |
| 153 | + |
| 154 | + async def close(self) -> None: |
| 155 | + """ |
| 156 | + Close the client and all the associated producers and consumers |
| 157 | +
|
| 158 | + Raises |
| 159 | + ------ |
| 160 | + PulsarException |
| 161 | + """ |
| 162 | + future = asyncio.get_running_loop().create_future() |
| 163 | + self._client.close_async(functools.partial(_set_future, future, value=None)) |
| 164 | + await future |
| 165 | + |
| 166 | +def _set_future(future: asyncio.Future, result: _pulsar.Result, value: Any): |
| 167 | + def complete(): |
| 168 | + if result == _pulsar.Result.Ok: |
| 169 | + future.set_result(value) |
| 170 | + else: |
| 171 | + future.set_exception(PulsarException(result)) |
| 172 | + future.get_loop().call_soon_threadsafe(complete) |
0 commit comments