forked from walijoy/php_snowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_snowflake.c
289 lines (248 loc) · 6.94 KB
/
php_snowflake.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
+----------------------------------------------------------------------+
| php_snowflake https://github.com/Sxdd/php_snowflake |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Towers ([email protected]) |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define MAX_SEQUENCE 999
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <stdio.h>
#include <sys/syscall.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_snowflake.h"
/* If you declare any globals in php_snowflake.h uncomment this:*/
ZEND_DECLARE_MODULE_GLOBALS(php_snowflake)
zend_class_entry *php_snowflake_ce;
static zend_long time_re_gen(zend_long last) {
zend_long new_time;
struct timeval tv;
do {
gettimeofday(&tv, 0);
new_time = (zend_long)tv.tv_sec * 1000 + (zend_long)tv.tv_usec / 1000;
} while (new_time <= last);
return new_time;
}
static zend_long get_workid() {
return syscall(SYS_gettid);
}
static zend_long time_gen() {
struct timeval tv;
gettimeofday(&tv, 0);
return (zend_long)tv.tv_sec * 1000 + (zend_long)tv.tv_usec / 1000;
}
static void next_id(char *id) {
#if PHP_API_VERSION < 20151012
TSRMLS_FETCH();
#endif
zend_long ts;
ts = time_gen();
if (ts == PHP_SNOWFLAKE_G(last_time_stamp)) {
PHP_SNOWFLAKE_G(sequence) = (PHP_SNOWFLAKE_G(sequence)+1) & MAX_SEQUENCE;
if (PHP_SNOWFLAKE_G(sequence) == 0) {
ts = time_re_gen(ts);
}
} else {
PHP_SNOWFLAKE_G(last_time_stamp) = ts;
PHP_SNOWFLAKE_G(sequence) = 1;
}
if (ts < PHP_SNOWFLAKE_G(last_time_stamp)) {
strcpy(id, NULL);
} else {
sprintf(id, "%ld%d%08ld%03d", ts, PHP_SNOWFLAKE_G(service_no), PHP_SNOWFLAKE_G(worker_id), PHP_SNOWFLAKE_G(sequence));
}
}
PHP_METHOD(PhpSnowFlake, nextId) {
char id[27];
if (zend_parse_parameters(ZEND_NUM_ARGS()
#if PHP_API_VERSION < 20151012
TSRMLS_CC
#endif
, "l", &PHP_SNOWFLAKE_G(service_no)) == FAILURE) {
RETURN_FALSE;
}
if (PHP_SNOWFLAKE_G(service_no)>99 | PHP_SNOWFLAKE_G(service_no)<10) {
zend_error(E_ERROR, "service_no in the range of 10,99");
}
next_id(id);
if (id == NULL) {
RETURN_FALSE;
} else {
#if PHP_API_VERSION < 20151012
RETURN_STRINGL(id,strlen(id), 1);
#else
RETURN_STRINGL(id,strlen(id));
#endif
}
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* {{{ ARG_INFO
*/
ZEND_BEGIN_ARG_INFO_EX(php_snowflake_next_id, 0, 0, 1)
ZEND_ARG_INFO(0, service_no)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ php_php_snowflake_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_php_snowflake_init_globals(zend_php_snowflake_globals *php_snowflake_globals)
{
php_snowflake_globals->global_value = 0;
php_snowflake_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ php_snowflake_functions[]
*
* Every user visible function must have an entry in php_snowflake_methods[].
*/
const zend_function_entry php_snowflake_methods[] = {
PHP_ME(PhpSnowFlake, nextId, php_snowflake_next_id, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
#if PHP_API_VERSION < 20151012
zend_function_entry php_snowflake_functions[] = {
PHP_FE_END
};
#else
#define php_snowflake_functions NULL
#endif
/* }}} */
static void php_snowflake_ctor(
#if PHP_API_VERSION < 20151012
zend_php_snowflake_globals *iw TSRMLS_DC
#else
zend_php_snowflake_globals *iw
#endif
)
{
iw->sequence = 0;
#ifdef ZTS
iw->worker_id = get_workid();
#endif
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(php_snowflake)
{
#ifdef ZTS
ts_allocate_id(&php_snowflake_globals_id, sizeof(zend_php_snowflake_globals), (ts_allocate_ctor)php_snowflake_ctor, NULL);
#else
# if PHP_API_VERSION < 20151012
php_snowflake_ctor(&php_snowflake_globals TSRMLS_CC);
# else
php_snowflake_ctor(&php_snowflake_globals);
# endif
#endif
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "PhpSnowFlake", php_snowflake_methods);
#if PHP_API_VERSION < 20151012
php_snowflake_ce = zend_register_internal_class(&ce TSRMLS_CC);
#else
php_snowflake_ce = zend_register_internal_class_ex(&ce, NULL);
#endif
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(php_snowflake)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(php_snowflake)
{
#ifndef ZTS
if (!PHP_SNOWFLAKE_G(worker_id)) {
PHP_SNOWFLAKE_G(worker_id) = get_workid();
}
#endif
#if defined(COMPILE_DL_PHP_SNOWFLAKE) && defined(ZTS) && PHP_API_VERSION >= 20151012
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(php_snowflake)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(php_snowflake)
{
php_info_print_table_start();
php_info_print_table_header(2, "php_snowflake", "enabled");
php_info_print_table_row(2, "version", PHP_SNOWFLAKE_VERSION);
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* {{{ php_snowflake_module_entry
*/
zend_module_entry php_snowflake_module_entry = {
STANDARD_MODULE_HEADER,
"php_snowflake",
php_snowflake_functions,
PHP_MINIT(php_snowflake),
PHP_MSHUTDOWN(php_snowflake),
PHP_RINIT(php_snowflake), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(php_snowflake), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(php_snowflake),
PHP_SNOWFLAKE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_PHP_SNOWFLAKE
#if defined(ZTS) && PHP_API_VERSION >= 20151012
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(php_snowflake)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/