GithubHelp home page GithubHelp logo

gabrielformiga / libpqnb Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 49 KB

Postgres non-blocking libpq connection pool

License: MIT License

Makefile 3.68% C 95.58% Shell 0.74%
postgres postgresql connection-pool non-blocking libpq c

libpqnb's Introduction

libpqnb

Postgres non-blocking libpq connection pool

How to build

make libpqnb.so

If you do not have pg_config installed:

make -DPG_INCLUDEDIR=/your/pg/include/dir libpqnb.so

Usage

All dependencies needed are in:

#include "pqnb.h"  

Initialize the connection pool:

struct PQNB_pool *pool;  
const char conninfo[] = "postgresql:///yourdbname?host=/var/run/postgresql";  
uint16_t num_connections = 32;  
pool = PQNB_pool_init(conninfo, num_connections);  

Get pool epoll file descriptor:

const union PQNB_pool_info *info;  
info = PQNB_pool_get_info(pool, PQNB_INFO_EPOLL_FD);  

Run pool:

/* This call doesn't block, you may select/epoll_wait the pool epoll_fd */  
/* you need to call this function any time the library has any data to proccess */  
/* we know when there's data ready when epoll_fd is ready to read POLLIN / EPOLLIN */  
/* see examples/test.c */  
PQNB_pool_run(pool);  

Run a query:

/* just a struct example */  
struct query_counter { uint64_t count; };  
  
/* callback */  
void  
query_callback(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))  
    {  
      assert(NULL != user_data);  
      queries_counter = user_data;  
      queries_counter->count++;  
    }  
  else  
    printf("query failed\n");  
}    
  
/* querying */
PQNB_pool_query(pool, "SELECT * FROM version()",  
                query_callback, &counter);  

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.