Permalink
Browse files

bug 1742 - libconflate handles single-JSON REST polling better

A user was trying moxi/libconflate against their own REST/HTTP server,
which was not following the tested, advanced streaming REST pattern
that ns_server/menelaus provides (which streams a little more JSON
on a long-running connection whenever the cluster config changes).

Instead, the user's REST/HTTP server was just providing a single JSON
response and ending the connection, like most of the world will
probably attempt and expect in their first interaction with moxi.

With this change, libconflate now informs callbacks during this single
JSON response case, and the 1-second sleep-looping now happens for
more code pathways.

Also, null terminated the assembled JSON response correctly.

Change-Id: Iec5585bdb100da28e36d2373ed6ce1fe13510fc1
Reviewed-on: http://review.northscale.com/1149
Tested-by: Steve Yen <steve.yen@gmail.com>
Reviewed-by: Eric Lambert <eric.d.lambert@gmail.com>
  • Loading branch information...
1 parent 420faf4 commit de295bcf997d46a2e36be8e1e64fc8f8db6c7d0d @steveyen steveyen committed Jul 14, 2010
Showing with 19 additions and 4 deletions.
  1. +19 −4 rest.c
View
23 rest.c
@@ -66,6 +66,10 @@ static struct response_buffer *write_data_to_buffer(struct response_buffer *buff
}
static char *assemble_complete_response(struct response_buffer *response_head) {
+ if (response_head == NULL) {
+ return NULL;
+ }
+
//figure out how big the message is
struct response_buffer * cur_buffer = response_head;
size_t response_size = 0;
@@ -76,7 +80,7 @@ static char *assemble_complete_response(struct response_buffer *response_head) {
}
//create buffer
- response = malloc(response_size);
+ response = malloc(response_size + 1);
assert(response);
//populate buffer
@@ -88,6 +92,8 @@ static char *assemble_complete_response(struct response_buffer *response_head) {
cur_buffer = cur_buffer->next;
}
+ response[response_size] = '\0';
+
return response;
}
@@ -108,6 +114,11 @@ static void process_new_config(conflate_handle_t *conf_handle) {
char *values[2];
values[0] = assemble_complete_response(response_buffer_head);
values[1] = NULL;
+
+ if (values[0] == NULL) {
+ return;
+ }
+
kvpair_t *kv = mk_kvpair(CONFIG_KEY, values);
//execute the provided call back
@@ -183,16 +194,20 @@ void* run_rest_conflate(void *arg) {
while (true) {
if (curl_easy_perform(curl_handle) == 0) {
first = false;
+
+ /* if the REST server didn't provide a streaming JSON response,
+ we'll reach here, and need to process the just-one-JSON response */
+ process_new_config(handle);
} else {
if (first) {
/* the first time, exit rather than retry */
printf("ERROR: could not contact REST server: %s\n", handle->conf->host);
exit(1);
}
-
- /* otherwise, don't kill the server with tons of retries */
- sleep(1);
}
+
+ /* otherwise, don't kill the server with tons of retries */
+ sleep(1);
}
free_response(response_buffer_head);

0 comments on commit de295bc

Please sign in to comment.