1212use Protobuf \AbstractMessage ;
1313use Pulsar \Exception \OptionsException ;
1414use Pulsar \Exception \RuntimeException ;
15+ use Pulsar \IO \ChannelManager ;
1516use Pulsar \Proto \CommandSendReceipt ;
1617use Pulsar \Proto \KeyValue ;
1718use Pulsar \Proto \MessageMetadata ;
2021use Pulsar \Traits \ProducerKeepAlive ;
2122use Pulsar \Util \Buffer ;
2223use Pulsar \Util \Helper ;
24+ use Swoole \Coroutine ;
2325
2426/**
2527 * Class Producer
@@ -53,9 +55,22 @@ class Producer extends Client
5355 * @param string $url
5456 * @param ProducerOptions $options
5557 * @throws Exception\OptionsException
58+ * @throws RuntimeException
5659 */
5760 public function __construct (string $ url , ProducerOptions $ options )
5861 {
62+ // validate keepalive condition
63+ if ($ options ->getKeepalive ()) {
64+
65+ if (!extension_loaded ('swoole ' )) {
66+ throw new RuntimeException ('Keepalive require swoole extension ' );
67+ }
68+
69+ if (Coroutine::getCid () === -1 ) {
70+ throw new RuntimeException ('Keepalive Must be in a coroutine environment ' );
71+ }
72+ }
73+
5974 parent ::__construct ($ url , $ options );
6075 }
6176
@@ -66,12 +81,41 @@ public function __construct(string $url, ProducerOptions $options)
6681 */
6782 public function connect ()
6883 {
84+ // Establish tcp connection And complete the pulsar server handshake
6985 parent ::initialization ();
7086
87+ // Enable Keepalive
88+ if ($ this ->options ->getKeepalive ()) {
89+ Coroutine::create (function () {
90+ while ($ this ->keepalive ) {
91+
92+ /**
93+ * @var $response Response
94+ */
95+ $ response = $ this ->eventloop ->wait (3 );
96+ if (is_null ($ response )) {
97+ continue ;
98+ }
99+
100+ $ fd = $ response ->fd ();
101+ if ($ fd <= 0 ) {
102+ continue ;
103+ }
104+
105+ // Push data to Channel
106+ ChannelManager::get ($ fd )->push ($ response );
107+ }
108+ });
109+ }
110+
71111 // Send CreateProducer Command
72112 foreach ($ this ->topicManage ->all () as $ id => $ topic ) {
73- $ io = $ this ->topicManage ->getConnection ($ topic );
74- $ this ->producers [] = new PartitionProducer ($ id , $ topic , $ io , $ this ->options );
113+ $ connection = $ this ->topicManage ->getConnection ($ topic );
114+ if ($ this ->options ->getKeepalive ()) {
115+ ChannelManager::init ($ connection ->fd ());
116+ }
117+
118+ $ this ->producers [] = new PartitionProducer ($ id , $ topic , $ connection , $ this ->options );
75119 }
76120 }
77121
@@ -123,6 +167,7 @@ public function send($payload, array $options = []): string
123167 * @return void
124168 * @throws RuntimeException|OptionsException
125169 * @throws \Exception
170+ * @deprecated 1.3.0
126171 */
127172 public function sendAsync (string $ payload , callable $ callable , array $ options = [])
128173 {
@@ -140,6 +185,7 @@ public function sendAsync(string $payload, callable $callable, array $options =
140185 * @return void
141186 * @throws Exception\IOException
142187 * @throws RuntimeException
188+ * @deprecated 1.3.0
143189 */
144190 public function wait ()
145191 {
0 commit comments