Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow per-location directives push_stream_max_messages_stored and push_stream_message_ttl #298

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ngx_http_push_stream_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ typedef struct {
} ngx_http_push_stream_main_conf_t;

typedef struct {
time_t message_ttl;
ngx_uint_t max_messages_stored;
ngx_http_complex_value_t *channels_path;
ngx_uint_t authorized_channels_only;
ngx_flag_t store_messages;
Expand Down
2 changes: 1 addition & 1 deletion include/ngx_http_push_stream_module_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static void ngx_http_push_stream_unescape_uri(ngx_str_t *value);
static void ngx_http_push_stream_complex_value(ngx_http_request_t *r, ngx_http_complex_value_t *val, ngx_str_t *value);


ngx_int_t ngx_http_push_stream_add_msg_to_channel(ngx_http_push_stream_main_conf_t *mcf, ngx_log_t *log, ngx_http_push_stream_channel_t *channel, u_char *text, size_t len, ngx_str_t *event_id, ngx_str_t *event_type, ngx_flag_t store_messages, ngx_pool_t *temp_pool);
ngx_int_t ngx_http_push_stream_add_msg_to_channel(ngx_http_push_stream_main_conf_t *mcf, ngx_log_t *log, ngx_http_push_stream_channel_t *channel, u_char *text, size_t len, ngx_str_t *event_id, ngx_str_t *event_type, ngx_flag_t store_messages, ngx_pool_t *temp_pool, time_t message_ttl, ngx_uint_t max_messages_stored);
ngx_int_t ngx_http_push_stream_send_event(ngx_http_push_stream_main_conf_t *mcf, ngx_log_t *log, ngx_http_push_stream_channel_t *channel, ngx_str_t *event_id, ngx_pool_t *temp_pool);

static void ngx_http_push_stream_ping_timer_wake_handler(ngx_event_t *ev);
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_push_stream_module_publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ ngx_http_push_stream_publisher_body_handler(ngx_http_request_t *r)
for (q = ngx_queue_head(&ctx->requested_channels->queue); q != ngx_queue_sentinel(&ctx->requested_channels->queue); q = ngx_queue_next(q)) {
requested_channel = ngx_queue_data(q, ngx_http_push_stream_requested_channel_t, queue);

if (ngx_http_push_stream_add_msg_to_channel(mcf, r->connection->log, requested_channel->channel, buf->pos, ngx_buf_size(buf), event_id, event_type, cf->store_messages, r->pool) != NGX_OK) {
if (ngx_http_push_stream_add_msg_to_channel(mcf, r->connection->log, requested_channel->channel, buf->pos, ngx_buf_size(buf), event_id, event_type, cf->store_messages, r->pool, cf->message_ttl, cf->max_messages_stored) != NGX_OK) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
Expand Down
35 changes: 35 additions & 0 deletions src/ngx_http_push_stream_module_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ static ngx_command_t ngx_http_push_stream_commands[] = {
NULL },

/* Location directives */
{ ngx_string("push_stream_message_ttl"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_sec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_push_stream_loc_conf_t, message_ttl),
NULL },
{ ngx_string("push_stream_max_messages_stored"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_push_stream_loc_conf_t, max_messages_stored),
NULL },
{ ngx_string("push_stream_channels_path"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_http_set_complex_value_slot,
Expand Down Expand Up @@ -566,6 +578,8 @@ ngx_http_push_stream_create_loc_conf(ngx_conf_t *cf)
return NGX_CONF_ERROR;
}

lcf->message_ttl = NGX_CONF_UNSET;
lcf->max_messages_stored = NGX_CONF_UNSET_UINT;
lcf->channels_path = NULL;
lcf->authorized_channels_only = NGX_CONF_UNSET_UINT;
lcf->store_messages = NGX_CONF_UNSET_UINT;
Expand Down Expand Up @@ -600,7 +614,9 @@ ngx_http_push_stream_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_push_stream_loc_conf_t *prev = parent, *conf = child;

ngx_conf_merge_uint_value(conf->authorized_channels_only, prev->authorized_channels_only, 0);
ngx_conf_merge_value(conf->message_ttl, prev->message_ttl, NGX_HTTP_PUSH_STREAM_DEFAULT_MESSAGE_TTL);
ngx_conf_merge_value(conf->store_messages, prev->store_messages, 0);
ngx_conf_merge_uint_value(conf->max_messages_stored, prev->max_messages_stored, mcf->max_messages_stored_per_channel);
ngx_conf_merge_str_value(conf->header_template, prev->header_template, NGX_HTTP_PUSH_STREAM_DEFAULT_HEADER_TEMPLATE);
ngx_conf_merge_str_value(conf->message_template, prev->message_template, NGX_HTTP_PUSH_STREAM_DEFAULT_MESSAGE_TEMPLATE);
ngx_conf_merge_str_value(conf->footer_template, prev->footer_template, NGX_HTTP_PUSH_STREAM_DEFAULT_FOOTER_TEMPLATE);
Expand All @@ -618,6 +634,14 @@ ngx_http_push_stream_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
conf->channels_path = prev->channels_path;
}

if (conf->message_ttl == NGX_CONF_UNSET) {
conf->message_ttl = prev->message_ttl;
}

if (conf->max_messages_stored == NGX_CONF_UNSET_UINT) {
conf->max_messages_stored = prev->max_messages_stored;
}

if (conf->last_received_message_time == NULL) {
conf->last_received_message_time = prev->last_received_message_time;
}
Expand All @@ -642,6 +666,17 @@ ngx_http_push_stream_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
return NGX_CONF_OK;
}

// message ttl cannot be zero
if ((conf->message_ttl != NGX_CONF_UNSET) && (conf->message_ttl == 0)) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: push_stream_message_ttl cannot be zero.");
return NGX_CONF_ERROR;
}

if ((conf->max_messages_stored != NGX_CONF_UNSET_UINT) && (conf->max_messages_stored == 0)) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: push_stream_max_messages_stored cannot be zero.");
return NGX_CONF_ERROR;
}

if (conf->channels_path == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: push_stream_channels_path must be set.");
return NGX_CONF_ERROR;
Expand Down
8 changes: 4 additions & 4 deletions src/ngx_http_push_stream_module_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ ngx_http_push_stream_convert_char_to_msg_on_shared(ngx_http_push_stream_main_con


ngx_int_t
ngx_http_push_stream_add_msg_to_channel(ngx_http_push_stream_main_conf_t *mcf, ngx_log_t *log, ngx_http_push_stream_channel_t *channel, u_char *text, size_t len, ngx_str_t *event_id, ngx_str_t *event_type, ngx_flag_t store_messages, ngx_pool_t *temp_pool)
ngx_http_push_stream_add_msg_to_channel(ngx_http_push_stream_main_conf_t *mcf, ngx_log_t *log, ngx_http_push_stream_channel_t *channel, u_char *text, size_t len, ngx_str_t *event_id, ngx_str_t *event_type, ngx_flag_t store_messages, ngx_pool_t *temp_pool, time_t message_ttl, ngx_uint_t max_messages_stored)
{
ngx_http_push_stream_shm_data_t *data = mcf->shm_data;
ngx_http_push_stream_msg_t *msg;
Expand Down Expand Up @@ -415,7 +415,7 @@ ngx_http_push_stream_add_msg_to_channel(ngx_http_push_stream_main_conf_t *mcf, n
channel->last_message_time = msg->time;
channel->last_message_tag = msg->tag;
// set message expiration time
msg->expires = msg->time + mcf->message_ttl;
msg->expires = msg->time + message_ttl;
channel->expires = ngx_time() + mcf->channel_inactivity_time;

// put messages on the queue
Expand All @@ -426,7 +426,7 @@ ngx_http_push_stream_add_msg_to_channel(ngx_http_push_stream_main_conf_t *mcf, n
ngx_shmtx_unlock(channel->mutex);

// now see if the queue is too big
qtd_removed = ngx_http_push_stream_ensure_qtd_of_messages(data, channel, mcf->max_messages_stored_per_channel, 0);
qtd_removed = ngx_http_push_stream_ensure_qtd_of_messages(data, channel, max_messages_stored, 0);

if (!channel->for_events) {
ngx_shmtx_lock(&data->channels_queue_mutex);
Expand Down Expand Up @@ -465,7 +465,7 @@ ngx_http_push_stream_send_event(ngx_http_push_stream_main_conf_t *mcf, ngx_log_t
ngx_str_t *event = ngx_http_push_stream_create_str(temp_pool, len);
if (event != NULL) {
ngx_sprintf(event->data, NGX_HTTP_PUSH_STREAM_EVENT_TEMPLATE, event_type, &channel->id);
ngx_http_push_stream_add_msg_to_channel(mcf, log, data->events_channel, event->data, ngx_strlen(event->data), NULL, event_type, 1, temp_pool);
ngx_http_push_stream_add_msg_to_channel(mcf, log, data->events_channel, event->data, ngx_strlen(event->data), NULL, event_type, 1, temp_pool, mcf->message_ttl, mcf->max_messages_stored_per_channel);
}

if ((received_temp_pool == NULL) && (temp_pool != NULL)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_push_stream_module_websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ ngx_http_push_stream_websocket_reading(ngx_http_request_t *r)
continue;
}

if (ngx_http_push_stream_add_msg_to_channel(mcf, r->connection->log, subscription->channel, ctx->frame->payload, ctx->frame->payload_len, NULL, NULL, cf->store_messages, ctx->temp_pool) != NGX_OK) {
if (ngx_http_push_stream_add_msg_to_channel(mcf, r->connection->log, subscription->channel, ctx->frame->payload, ctx->frame->payload_len, NULL, NULL, cf->store_messages, ctx->temp_pool, cf->message_ttl, cf->max_messages_stored) != NGX_OK) {
goto finalize;
}
}
Expand Down