Repository navigation
Expand file tree
/
Copy pathConsumerOptions.php
More file actions
159 lines (131 loc) · 3.36 KB
/
Copy pathConsumerOptions.php
File metadata and controls
159 lines (131 loc) · 3.36 KB
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
<?php
/**
* Created by PhpStorm.
* User: Sunny
* Date: 2022/6/26
* Time: 3:28 PM
*/
namespace Pulsar;
use Pulsar\Exception\OptionsException;
/**
* Class ConsumerOptions
*
* @package Pulsar
*/
class ConsumerOptions extends Options
{
/**
* @var string consumer name
*/
const NAME = 'name';
/**
* @var string subscription
*/
const SUBSCRIPTION = 'subscription';
/**
* @var int sub_type
*/
const SUBSCRIPTION_TYPE = 'sub_type';
/**
* @var int Nack Latency delivery default 60 seconds Unit second
*/
const NACK_REDELIVERY_DELAY = 'nack_redelivery_delay';
/**
* @var int Provides message throughput using local memory, default is 1000
*/
const RECEIVE_QUEUE_SIZE = 'receive_queue_size';
/**
* @param string $name
* @return void
*/
public function setConsumerName(string $name)
{
$this->data[ self::NAME ] = $name;
}
/**
* @param string $subscription
* @return void
*/
public function setSubscription(string $subscription)
{
$this->data[ self::SUBSCRIPTION ] = $subscription;
}
/**
* @param int $subType
* @return void
*/
public function setSubscriptionType(int $subType)
{
$this->data[ self::SUBSCRIPTION_TYPE ] = $subType;
}
/**
* @param int $seconds
* @return void
*/
public function setNackRedeliveryDelay(int $seconds)
{
$this->data[ self::NACK_REDELIVERY_DELAY ] = $seconds;
}
/**
* @param int $size
* @return void
*/
public function setReceiveQueueSize(int $size)
{
if ($size <= 0) {
$size = 1000;
}
$this->data[ self::RECEIVE_QUEUE_SIZE ] = $size;
}
/**
* @return int|mixed
*/
public function getReceiveQueueSize()
{
return $this->data[ self::RECEIVE_QUEUE_SIZE ] ?? 1000;
}
/**
* @return string
* @throws OptionsException
*/
public function getSubscriptionName(): string
{
$subscription = trim($this->data[ ConsumerOptions::SUBSCRIPTION ] ?? '');
if (empty($subscription)) {
throw new OptionsException('subscription name is required for consumer');
}
return $subscription;
}
/**
* @return string
* @throws OptionsException
*/
public function getSubscriptionType(): string
{
$subscriptionType = trim($this->data[ ConsumerOptions::SUBSCRIPTION_TYPE ] ?? SubscriptionType::Exclusive);
if ($subscriptionType < SubscriptionType::Exclusive || $subscriptionType > SubscriptionType::Key_Shared) {
throw new OptionsException('subscription type Out of range');
}
$this->offsetSet(ConsumerOptions::SUBSCRIPTION_TYPE, $subscriptionType);
return $subscriptionType;
}
/**
* @return int|mixed
*/
public function getNackRedeliveryDelay()
{
$delay = $this->data[ self::NACK_REDELIVERY_DELAY ] ?? 60;
return $delay <= 0 ? 60 : $delay;
}
/**
* @return mixed|string
* @throws \Exception
*/
public function getConsumerName()
{
if (!isset($this->data[ ConsumerOptions::NAME ])) {
$this->data[ ConsumerOptions::NAME ] = base64_encode(random_bytes(6));
}
return $this->data[ ConsumerOptions::NAME ];
}
}