Browse Source

jsonrpc: Simple demonstration on how jcon can be locked for streams

This is a bit of overkill now that we simply accumulate the entire
JSON response in the buffer before flushing, but when we move to
streamed responses it allows us to have a single command that has
exclusive access to the out direction of the JSON-RPC connection.
fee-tracking2
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
b86edf3cf1
  1. 1
      lightningd/Makefile
  2. 15
      lightningd/jsonrpc.c
  3. 2
      lightningd/jsonrpc.h

1
lightningd/Makefile

@ -32,6 +32,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/htlc_state.o \
common/htlc_wire.o \
common/key_derive.o \
common/io_lock.o \
common/json.o \
common/json_escaped.o \
common/memleak.o \

15
lightningd/jsonrpc.c

@ -510,6 +510,9 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])
assert(c->pending);
}
static struct io_plan *locked_write_json(struct io_conn *conn,
struct json_connection *jcon);
static struct io_plan *write_json(struct io_conn *conn,
struct json_connection *jcon)
{
@ -524,8 +527,9 @@ static struct io_plan *write_json(struct io_conn *conn,
return io_close(conn);
}
io_lock_release(jcon->lock);
/* Wait for more output. */
return io_out_wait(conn, jcon, write_json, jcon);
return io_out_wait(conn, jcon, locked_write_json, jcon);
}
jcon->outbuf = tal_steal(jcon, out->json);
@ -536,6 +540,12 @@ static struct io_plan *write_json(struct io_conn *conn,
jcon->outbuf, strlen(jcon->outbuf), write_json, jcon);
}
static struct io_plan *locked_write_json(struct io_conn *conn,
struct json_connection *jcon)
{
return io_lock_acquire_out(conn, jcon->lock, write_json, jcon);
}
static struct io_plan *read_json(struct io_conn *conn,
struct json_connection *jcon)
{
@ -600,6 +610,7 @@ static struct io_plan *jcon_connected(struct io_conn *conn,
jcon->used = 0;
jcon->buffer = tal_arr(jcon, char, 64);
jcon->stop = false;
jcon->lock = io_lock_new(jcon);
list_head_init(&jcon->commands);
/* We want to log on destruction, so we free this in destructor. */
@ -613,7 +624,7 @@ static struct io_plan *jcon_connected(struct io_conn *conn,
io_read_partial(conn, jcon->buffer,
tal_count(jcon->buffer),
&jcon->len_read, read_json, jcon),
write_json(conn, jcon));
locked_write_json(conn, jcon));
}
static struct io_plan *incoming_jcon_connected(struct io_conn *conn,

2
lightningd/jsonrpc.h

@ -4,6 +4,7 @@
#include <bitcoin/chainparams.h>
#include <ccan/autodata/autodata.h>
#include <ccan/list/list.h>
#include <common/io_lock.h>
#include <common/json.h>
struct bitcoin_txid;
@ -64,6 +65,7 @@ struct json_connection {
struct list_head output;
const char *outbuf;
struct io_lock *lock;
};
struct json_command {

Loading…
Cancel
Save