-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathQueueConsumer.php
More file actions
149 lines (123 loc) · 3.73 KB
/
QueueConsumer.php
File metadata and controls
149 lines (123 loc) · 3.73 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
<?php
namespace PostHog;
abstract class QueueConsumer extends Consumer
{
protected const MAX_BATCH_PAYLOAD_SIZE = 1024 * 1024; // 1MB
protected const MAX_BATCH_PAYLOAD_SIZE_HUMAN = (self::MAX_BATCH_PAYLOAD_SIZE / 1024) . 'KB';
protected $type = "QueueConsumer";
protected $queue;
protected $max_queue_size = 1000;
protected $batch_size = 100;
protected $maximum_backoff_duration = 10000; // Set maximum waiting limit to 10s
protected $host = "us.i.posthog.com";
protected $compress_request = false;
/**
* Store our api key and options as part of this consumer
* @param string $apiKey
* @param array $options
*/
public function __construct($apiKey, $options = array())
{
parent::__construct($apiKey, $options);
if (isset($options["max_queue_size"])) {
$this->max_queue_size = $options["max_queue_size"];
}
if (isset($options["batch_size"])) {
$this->batch_size = $options["batch_size"];
}
if (isset($options["maximum_backoff_duration"])) {
$this->maximum_backoff_duration = (int) $options["maximum_backoff_duration"];
}
if (isset($options["host"])) {
$this->host = $options["host"];
if ($this->host && preg_match("/^https?:\\/\\//i", $this->host)) {
$this->options['ssl'] = substr($this->host, 0, 5) == 'https';
$this->host = preg_replace("/^https?:\\/\\//i", "", $this->host);
}
}
if (isset($options["compress_request"])) {
$this->compress_request = json_decode($options["compress_request"]);
}
$this->queue = array();
}
public function __destruct()
{
// Flush our queue on destruction
$this->flush();
}
/**
* Captures a user action
*
* @param array $message
* @return boolean whether the capture call succeeded
*/
public function capture(array $message)
{
return $this->enqueue($message);
}
/**
* Tags properties about the user.
*
* @param array $message
* @return boolean whether the identify call succeeded
*/
public function identify(array $message)
{
return $this->enqueue($message);
}
/**
* Aliases from one user id to another
*
* @param array $message
* @return boolean whether the alias call succeeded
*/
public function alias(array $message)
{
return $this->enqueue($message);
}
/**
* Flushes our queue of messages by batching them to the server
*/
public function flush()
{
$count = count($this->queue);
$success = true;
while ($count > 0 && $success) {
$batch = array_splice($this->queue, 0, min($this->batch_size, $count));
$success = $this->flushBatch($batch);
$count = count($this->queue);
}
return $success;
}
/**
* Adds an item to our queue.
* @param mixed $item
* @return boolean whether call has succeeded
*/
public function enqueue($item)
{
$count = count($this->queue);
if ($count > $this->max_queue_size) {
return false;
}
$count = array_push($this->queue, $item);
if ($count >= $this->batch_size) {
return $this->flush(); // return ->flush() result: true on success
}
return true;
}
/**
* Given a batch of messages the method returns
* a valid payload.
*
* @param {Array} $batch
* @return {Array}
*/
protected function payload($batch)
{
return array(
"batch" => $batch,
"api_key" => $this->apiKey,
);
}
}