id generator support file mode
parent
8ccf3503ca
commit
3c77ef047d
3
HISTORY
3
HISTORY
|
|
@ -1,10 +1,11 @@
|
||||||
|
|
||||||
Version 1.30 2016-09-08
|
Version 1.30 2016-10-31
|
||||||
* modify php-fastcommon/test.php
|
* modify php-fastcommon/test.php
|
||||||
* php7_ext_wrapper.h: fix memory leak in php 7
|
* php7_ext_wrapper.h: fix memory leak in php 7
|
||||||
* setsockopt SO_NOSIGPIPE when nessary
|
* setsockopt SO_NOSIGPIPE when nessary
|
||||||
* add function iovent_add_to_deleted_list
|
* add function iovent_add_to_deleted_list
|
||||||
* fixbug for select with microseconds
|
* fixbug for select with microseconds
|
||||||
|
* id generator support file mode
|
||||||
|
|
||||||
Version 1.29 2016-06-17
|
Version 1.29 2016-06-17
|
||||||
* ini_file_reader support #@if
|
* ini_file_reader support #@if
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
%define LibFastcommonDevel libfastcommon-devel
|
%define LibFastcommonDevel libfastcommon-devel
|
||||||
|
%define LibFastcommonDebuginfo libfastcommon-debuginfo
|
||||||
|
|
||||||
Name: libfastcommon
|
Name: libfastcommon
|
||||||
Version: 1.0.30
|
Version: 1.0.30
|
||||||
|
|
|
||||||
|
|
@ -409,7 +409,7 @@ ZEND_FUNCTION(fastcommon_is_private_ip)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn",
|
resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn",
|
||||||
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16])
|
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16, int mode = 0644])
|
||||||
return resource handle for success, false for fail
|
return resource handle for success, false for fail
|
||||||
*/
|
*/
|
||||||
ZEND_FUNCTION(fastcommon_id_generator_init)
|
ZEND_FUNCTION(fastcommon_id_generator_init)
|
||||||
|
|
@ -420,11 +420,12 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
|
||||||
long mid_bits;
|
long mid_bits;
|
||||||
long extra_bits;
|
long extra_bits;
|
||||||
long sn_bits;
|
long sn_bits;
|
||||||
|
long mode;
|
||||||
char *filename;
|
char *filename;
|
||||||
PHPIDGContext *php_idg_context;
|
PHPIDGContext *php_idg_context;
|
||||||
|
|
||||||
argc = ZEND_NUM_ARGS();
|
argc = ZEND_NUM_ARGS();
|
||||||
if (argc > 5) {
|
if (argc > 6) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"fastcommon_id_generator_init parameters count: %d is invalid",
|
"fastcommon_id_generator_init parameters count: %d is invalid",
|
||||||
__LINE__, argc);
|
__LINE__, argc);
|
||||||
|
|
@ -437,9 +438,10 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
|
||||||
mid_bits = 16;
|
mid_bits = 16;
|
||||||
extra_bits = 0;
|
extra_bits = 0;
|
||||||
sn_bits = 16;
|
sn_bits = 16;
|
||||||
if (zend_parse_parameters(argc TSRMLS_CC, "|sllll", &filename,
|
mode = ID_GENERATOR_DEFAULT_FILE_MODE;
|
||||||
|
if (zend_parse_parameters(argc TSRMLS_CC, "|slllll", &filename,
|
||||||
&filename_len, &machine_id, &mid_bits, &extra_bits,
|
&filename_len, &machine_id, &mid_bits, &extra_bits,
|
||||||
&sn_bits) == FAILURE)
|
&sn_bits, &mode) == FAILURE)
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"zend_parse_parameters fail!", __LINE__);
|
"zend_parse_parameters fail!", __LINE__);
|
||||||
|
|
@ -454,8 +456,8 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id_generator_init_extra(&php_idg_context->idg_context, filename,
|
if (id_generator_init_extra_ex(&php_idg_context->idg_context, filename,
|
||||||
machine_id, mid_bits, extra_bits, sn_bits) != 0)
|
machine_id, mid_bits, extra_bits, sn_bits, mode) != 0)
|
||||||
{
|
{
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
%define php_inidir %(php --ini | head -n 1 | awk -F ':' '{print $2;}' | sed 's/ //g')
|
%define php_inidir %(php --ini | head -n 1 | awk -F ':' '{print $2;}' | sed 's/ //g')
|
||||||
%define php_extdir %(php-config --extension-dir 2>/dev/null)
|
%define php_extdir %(php-config --extension-dir 2>/dev/null)
|
||||||
Name: php-fastcommon
|
Name: php-fastcommon
|
||||||
Version: 1.0.8
|
Version: 1.0.9
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: The php extension for libfastcommon
|
Summary: The php extension for libfastcommon
|
||||||
License: GPL
|
License: GPL
|
||||||
|
|
@ -11,8 +11,8 @@ Source: http://perso.orange.fr/sebastien.godard/%{name}-%{version}.tar.gz
|
||||||
|
|
||||||
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
|
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
|
||||||
|
|
||||||
BuildRequires: libfastcommon-devel >= 1.0.29
|
BuildRequires: libfastcommon-devel >= 1.0.30
|
||||||
Requires: libfastcommon >= 1.0.29
|
Requires: libfastcommon >= 1.0.30
|
||||||
|
|
||||||
%description
|
%description
|
||||||
This package provides the php extension for libfastcommon
|
This package provides the php extension for libfastcommon
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_gen
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$id = 6301319781687017475;
|
$id = 6301319781687017475;
|
||||||
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14);
|
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14, 0666);
|
||||||
echo 'extra no: ' . fastcommon_id_generator_get_extra($id, $handle1) . "\n";
|
echo 'extra no: ' . fastcommon_id_generator_get_extra($id, $handle1) . "\n";
|
||||||
|
|
||||||
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ struct fast_allocator_array
|
||||||
int reclaim_interval; //<= 0 for never reclaim
|
int reclaim_interval; //<= 0 for never reclaim
|
||||||
int last_reclaim_time;
|
int last_reclaim_time;
|
||||||
volatile int64_t malloc_bytes; //total alloc bytes
|
volatile int64_t malloc_bytes; //total alloc bytes
|
||||||
int64_t malloc_bytes_limit; //mater mark bytes for malloc
|
int64_t malloc_bytes_limit; //water mark bytes for malloc
|
||||||
double expect_usage_ratio;
|
double expect_usage_ratio;
|
||||||
struct fast_allocator_info **allocators;
|
struct fast_allocator_info **allocators;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ mpool init
|
||||||
parameters:
|
parameters:
|
||||||
mpool: the mpool pointer
|
mpool: the mpool pointer
|
||||||
alloc_size_once: malloc elements once, 0 for malloc 1MB memory once
|
alloc_size_once: malloc elements once, 0 for malloc 1MB memory once
|
||||||
discard_size: discard when remain size <= discard_size, 0 for 8 bytes
|
discard_size: discard when remain size <= discard_size, 0 for 64 bytes
|
||||||
return error no, 0 for success, != 0 fail
|
return error no, 0 for success, != 0 fail
|
||||||
*/
|
*/
|
||||||
int fast_mpool_init(struct fast_mpool_man *mpool,
|
int fast_mpool_init(struct fast_mpool_man *mpool,
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,9 @@
|
||||||
#include "local_ip_func.h"
|
#include "local_ip_func.h"
|
||||||
#include "id_generator.h"
|
#include "id_generator.h"
|
||||||
|
|
||||||
int id_generator_init_extra(struct idg_context *context, const char *filename,
|
int id_generator_init_extra_ex(struct idg_context *context, const char *filename,
|
||||||
const int machine_id, const int mid_bits, const int extra_bits,
|
const int machine_id, const int mid_bits, const int extra_bits,
|
||||||
const int sn_bits)
|
const int sn_bits, const mode_t mode)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
int mid;
|
int mid;
|
||||||
|
|
@ -116,14 +116,35 @@ int id_generator_init_extra(struct idg_context *context, const char *filename,
|
||||||
mid = ntohl(ip_addr.s_addr) & ((1 << mid_bits) - 1);
|
mid = ntohl(ip_addr.s_addr) & ((1 << mid_bits) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((context->fd = open(filename, O_RDWR | O_CREAT, 0644)) < 0)
|
if ((context->fd = open(filename, O_RDWR)) < 0)
|
||||||
{
|
{
|
||||||
result = errno != 0 ? errno : EACCES;
|
if (errno == ENOENT)
|
||||||
logError("file: "__FILE__", line: %d, "
|
{
|
||||||
"open file \"%s\" fail, "
|
mode_t old_mode;
|
||||||
"errno: %d, error info: %s", __LINE__,
|
old_mode = umask(0);
|
||||||
filename, result, STRERROR(result));
|
if ((context->fd=open(filename, O_RDWR | O_CREAT, mode)) < 0)
|
||||||
return result;
|
{
|
||||||
|
result = errno != 0 ? errno : EACCES;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
umask(old_mode);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = errno != 0 ? errno : EACCES;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"open file \"%s\" fail, "
|
||||||
|
"errno: %d, error info: %s", __LINE__,
|
||||||
|
filename, result, STRERROR(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context->machine_id = mid;
|
context->machine_id = mid;
|
||||||
|
|
@ -143,6 +164,15 @@ int id_generator_init_extra(struct idg_context *context, const char *filename,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int id_generator_init_extra(struct idg_context *context, const char *filename,
|
||||||
|
const int machine_id, const int mid_bits, const int extra_bits,
|
||||||
|
const int sn_bits)
|
||||||
|
{
|
||||||
|
return id_generator_init_extra_ex(context, filename,
|
||||||
|
machine_id, mid_bits, extra_bits, sn_bits,
|
||||||
|
ID_GENERATOR_DEFAULT_FILE_MODE);
|
||||||
|
}
|
||||||
|
|
||||||
void id_generator_destroy(struct idg_context *context)
|
void id_generator_destroy(struct idg_context *context)
|
||||||
{
|
{
|
||||||
if (context->fd >= 0)
|
if (context->fd >= 0)
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,13 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/time.h>
|
#include <sys/stat.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include "common_define.h"
|
#include "common_define.h"
|
||||||
|
|
||||||
|
#define ID_GENERATOR_DEFAULT_FILE_MODE 0666
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -39,6 +42,22 @@ struct idg_context {
|
||||||
int64_t sn_mask;
|
int64_t sn_mask;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* init function
|
||||||
|
* parameter:
|
||||||
|
* context: the id generator context
|
||||||
|
* filename: the filename to store id
|
||||||
|
* machine_id: the machine id, 0 for auto generate by local ip address
|
||||||
|
* mid_bits: the bits of machine id, such as 16
|
||||||
|
* extra_bits: the extra bits, such as 0
|
||||||
|
* sn_bits: the bits of serial no, such as 16, mid_bits + sn_bits must <= 32
|
||||||
|
* mode: the mode for file open
|
||||||
|
* return error no, 0 for success, none zero for fail
|
||||||
|
*/
|
||||||
|
int id_generator_init_extra_ex(struct idg_context *context, const char *filename,
|
||||||
|
const int machine_id, const int mid_bits, const int extra_bits,
|
||||||
|
const int sn_bits, const mode_t mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* init function
|
* init function
|
||||||
* parameter:
|
* parameter:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue