Giter Club home page Giter Club logo

nginx-poller-module's Introduction

nginx-poller-module - Flexible detached polling requests for nginx workers

This is nginx-poller-module, an nginx module that lets you configure your worker process such that they each make detached requests to endpoints of your choosing at a frequency you can control.

Background and motivation

This work was motivated by my desire to enable nginx worker processes to do either of the following things:

  • Communicate status information to some central location at regular, configurable intervals.

  • Pull configuration information from some central location at regular, configurable intervals.

In a single-worker configuration, the poller module is not as useful, because you can always set up handlers in your nginx installation that respond with status information, or accept configuration information via POST. However, in a multiple-worker configuration, the workers share the listening sockets, and it is not possible to pull from or push to an individual worker.

A simple example

The following is an example nginx configuration snippet that one might use with this module:

# upstreams for status and config pollers

upstream status { server 10.20.30.40:2000; }
upstream config { server 10.20.30.40:3000; }

# poller block for the status poller

poller status {
  endpoint http://status;
  method   POST;
  header   Host status;
  header   User-Agent nginx;
  uri      /status;
  body     $my_worker_status;
  interval 100ms;
}

# poller block for the config poller

poller config {
  endpoint http://config;
  method   GET;
  header   Host config;
  header   User-Agent nginx;
  uri      $my_config_uri;
  interval $my_config_interval;
}

Pollers are configured in blocks within the main HTTP block, at the same level as upstream directives. Within a poller block, one must define an endpoint, a method, a uri, and an interval. One may also include any number of header directives, specifying the header name and value as arguments. Finally, for POST and PUT requests, one must define a body.

It is permissible to use variables in the header value, the uri, the body, and the interval. The interval value must evaluate to a time value; e.g. "1s" or "100ms", etc. In a typical usage, the URI and/or body might be furnished from some other module, which might also provide the request interval. So, for example, if you set up a poller to request configuration updates, and your request comes back with updates that need to be applied, you may set the interval such that your next request is made immediately, so as to check right away for further updates. Once your configuration is caught up, you can then restore the default interval.

For application of configuration updates, it is important to receive the response body and do something with it. To this end, you can register a set of handlers that will process the response to your poller requests. Here is an example:

static ngx_http_poller_handler_t my_config_handler = {
  my_config_handle_status,
  my_config_handle_header,
  my_config_handle_body,
  my_config_finalize
};

static ngx_int_t
my_config_postconf(ngx_conf_t *cf)
{
  static ngx_str_t         config = ngx_string("config");
  ngx_http_poller_conf_t  *conf;

  conf = ngx_http_conf_get_module_main_conf(cf, ngx_http_poller_module);
  ngx_http_poller_set_handler(conf, &config, &my_config_handler);

  return NGX_OK;
}

The poller handler is a set of functions with the following signatures:

typedef ngx_int_t (*ngx_http_poller_status_pt)(ngx_http_request_t *r,
					       ngx_http_status_t *status);
typedef ngx_int_t (*ngx_http_poller_header_pt)(ngx_http_request_t *r,
					       ngx_str_t *name,
					       ngx_str_t *value);
typedef ngx_int_t (*ngx_http_poller_body_pt)(ngx_http_request_t *r,
					     ngx_str_t *data);
typedef void (*ngx_http_poller_finalize_pt)(ngx_http_request_t *r,
					    ngx_int_t rc);

In this way, you can inject your module's processing into the poller's handling of the HTTP response from the endpoint.

How to use this module

Like all other third party modules, this module needs to be compiled into your nginx binary. You can add the module to your nginx build by configuring nginx like so:

./configure --add-module=/your/path/to/nginx-poller-module

If you wish to call ngx_http_poller_set_handler, you will also need to add the same directory to your includes:

./configure \
  --with-cc-opt="-I/your/path/to/nginx-poller-module" \
  --add-module=/your/path/to/nginx-poller-module \
  --add-module=/your/path/to/your-other-module

26 April 2013

Dave Bailey [email protected]

nginx-poller-module's People

Contributors

dbcode avatar

Watchers

 avatar  avatar

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.