id generator for multi processes
parent
8104af5d27
commit
b16e56b9a3
2
HISTORY
2
HISTORY
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
Version 1.27 2016-04-09
|
||||
Version 1.27 2016-04-10
|
||||
* add function fd_set_cloexec
|
||||
* php-fastcommon.spec.in support PHP 7
|
||||
* add file lock and unlock functions
|
||||
|
|
|
|||
|
|
@ -69,10 +69,10 @@ int id_generator_init_ex(struct idg_context *context, const char *filename,
|
|||
return EINVAL;
|
||||
}
|
||||
|
||||
printf("ip_addr: %s, s_addr: %08X\n", local_ip, ip_addr.s_addr);
|
||||
printf("mask number: %08X\n", (1 << mid_bits));
|
||||
logDebug("ip_addr: %s, s_addr: %08X, mask number: %08X",
|
||||
local_ip, ip_addr.s_addr, (1 << mid_bits));
|
||||
|
||||
mid = (ip_addr.s_addr >> (32 - mid_bits)) & ((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)
|
||||
|
|
@ -91,7 +91,8 @@ int id_generator_init_ex(struct idg_context *context, const char *filename,
|
|||
context->masked_mid = ((int64_t)mid) << context->sn_bits;
|
||||
context->sn_mask = ((int64_t)1 << context->sn_bits) - 1;
|
||||
|
||||
printf("mid: %08X, masked_mid: %016llX, sn_mask: %08llX\n", mid, context->masked_mid, context->sn_mask);
|
||||
logDebug("mid: %08X, masked_mid: %016llX, sn_mask: %08llX\n",
|
||||
mid, context->masked_mid, context->sn_mask);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -171,7 +172,7 @@ int id_generator_next(struct idg_context *context, int64_t *id)
|
|||
|
||||
file_unlock(context->fd);
|
||||
|
||||
*id = (time(NULL) << 32) | context->masked_mid | (sn & context->sn_mask);
|
||||
*id = (((int64_t)time(NULL)) << 32) | context->masked_mid | (sn & context->sn_mask);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -714,7 +714,10 @@ static void* log_gzip_func(void *args)
|
|||
snprintf(full_filename, sizeof(full_filename), "%s%s",
|
||||
log_filepath, filename_array.filenames[i]);
|
||||
snprintf(cmd, sizeof(cmd), "%s %s", gzip, full_filename);
|
||||
system(cmd);
|
||||
if (system(cmd) == -1)
|
||||
{
|
||||
fprintf(stderr, "execute %s fail\n", cmd);
|
||||
}
|
||||
}
|
||||
|
||||
log_free_filename_array(&filename_array);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@ int main(int argc, char *argv[])
|
|||
int64_t id;
|
||||
const int machine_id = 0;
|
||||
const int mid_bits = 8;
|
||||
|
||||
log_init();
|
||||
//g_log_context.log_level = LOG_DEBUG;
|
||||
|
||||
result = id_generator_init_ex(&context, "/tmp/sn.txt",
|
||||
machine_id, mid_bits);
|
||||
|
|
@ -30,17 +33,17 @@ int main(int argc, char *argv[])
|
|||
return result;
|
||||
}
|
||||
|
||||
id_generator_next(&context, &id);
|
||||
printf("id: %"PRId64", %016llX\n", id, id);
|
||||
for (i=0; i<1000000; i++)
|
||||
//id_generator_next(&context, &id);
|
||||
//printf("id: %"PRId64", %016llX\n", id, id);
|
||||
for (i=0; i<100000; i++)
|
||||
{
|
||||
result = id_generator_next(&context, &id);
|
||||
if (result != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%"PRId64", %016llX\n", id, id);
|
||||
}
|
||||
printf("id: %"PRId64", %016llX\n", id, id);
|
||||
|
||||
id_generator_destroy(&context);
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in New Issue