gabrielformiga / libpqnb Goto Github PK
View Code? Open in Web Editor NEWPostgres non-blocking libpq connection pool
License: MIT License
Postgres non-blocking libpq connection pool
License: MIT License
Hello, I can't seem to figure out how to make just one query. I only pool one query but it always ends up running the query more than once, and it is giving undefined behavior in that the amount of times that the query callback is called is random.
#include "poolInit.h"
#define CONNINFO "postgresql://info@localhost/dbname"
#define QUERY "SELECT * FROM users"
#define TEST_TIME_SEC 1
#define NUM_CONNECTIONS 32
struct query_counter { uint64_t count; PGresult *resRet;};
static void
test_print_res(PGresult *res)
{
int num_fields = PQnfields(res);
for (int i = 0; i < num_fields; i++) {
const int ftype = PQftype(res, i);
printf("fname %s ftype %d\n", PQfname(res, i), ftype);
}
/* next, print out the rows */
int num_rows = PQntuples(res);
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_fields; j++)
printf("%s\t", PQgetvalue(res, i, j));
printf("\n");
}
}
struct PQNB_pool * getPool(){
struct PQNB_pool *pool;
pool = PQNB_pool_init(CONNINFO, NUM_CONNECTIONS);
assert(NULL != pool);
return pool;
}
int main(){
struct PQNB_pool *pool = getPool();
const union PQNB_pool_info *info;
int epoll_fd, res;
struct epoll_event ev, evs[1];
struct query_counter counter;
time_t end;
info = PQNB_pool_get_info(pool, PQNB_INFO_EPOLL_FD);
assert(NULL != info);
assert(info->epoll_fd != -1);
epoll_fd = epoll_create1(EPOLL_CLOEXEC);
assert(epoll_fd != -1);
ev.events = EPOLLIN;
ev.data.ptr = NULL;
res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, info->epoll_fd, &ev);
assert(-1 != res);
counter.count = 0;
static bool flag = false;
void test_query_cb2(PGresult *res,
void *user_data,
char *error_msg,
bool timeout)
{
struct query_counter *queries_counter;
/*
* ignoring compiler warnings
*/
(void) res;
if (timeout)
{
printf("timeout\n");
return;
}
if (NULL != error_msg)
{
printf("%s", error_msg);
return;
}
if (PGRES_TUPLES_OK == PQresultStatus(res))
{
flag = true;
assert(NULL != user_data);
queries_counter = user_data;
queries_counter->count++;
queries_counter->resRet = PQcopyResult(res, PG_COPYRES_TUPLES);
}
else
printf("query failed\n");
}
// filling query buffer
while(-1 != PQNB_pool_query(pool, QUERY, test_query_cb2, &counter));
while(!flag){
res = epoll_wait(epoll_fd, evs, 1, 1000);
if (res == -1)
break;
if(counter.count ==0 && !flag){
int run = PQNB_pool_run(pool);
if (run == -1){
printf("run returned -1\n");
//break;
}
}
}
test_print_res(counter.resRet);
PQNB_pool_free(pool);
printf("total queries: %ld\n", counter.count);
return 0;
}
Any help is very much appreciated, this seems like a really nice library and I'd love to get it fully working.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.