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. 3
      lightningd/Makefile
  2. 15
      lightningd/jsonrpc.c
  3. 2
      lightningd/jsonrpc.h

3
lightningd/Makefile

@ -32,6 +32,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/htlc_state.o \ common/htlc_state.o \
common/htlc_wire.o \ common/htlc_wire.o \
common/key_derive.o \ common/key_derive.o \
common/io_lock.o \
common/json.o \ common/json.o \
common/json_escaped.o \ common/json_escaped.o \
common/memleak.o \ common/memleak.o \
@ -121,7 +122,7 @@ check-makefile: check-lightningd-makefile
check-lightningd-makefile: check-lightningd-makefile:
@for f in lightningd/*.h lightningd/*/*.h; do if ! echo $(LIGHTNINGD_HEADERS_NOGEN) $(LIGHTNINGD_HEADERS_GEN) "" | grep -q "$$f "; then echo $$f not mentioned in LIGHTNINGD_HEADERS_NOGEN or LIGHTNINGD_HEADERS_GEN >&2; exit 1; fi; done @for f in lightningd/*.h lightningd/*/*.h; do if ! echo $(LIGHTNINGD_HEADERS_NOGEN) $(LIGHTNINGD_HEADERS_GEN) "" | grep -q "$$f "; then echo $$f not mentioned in LIGHTNINGD_HEADERS_NOGEN or LIGHTNINGD_HEADERS_GEN >&2; exit 1; fi; done
lightningd/lightningd: $(LIGHTNINGD_OBJS) $(LIGHTNINGD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(WIRE_ONION_OBJS) $(LIGHTNINGD_HANDSHAKE_CONTROL_OBJS) $(LIGHTNINGD_GOSSIP_CONTROL_OBJS) $(LIGHTNINGD_OPENING_CONTROL_OBJS) $(LIGHTNINGD_CHANNEL_CONTROL_OBJS) $(LIGHTNINGD_CLOSING_CONTROL_OBJS) $(LIGHTNINGD_ONCHAIN_CONTROL_OBJS) $(WALLET_LIB_OBJS) $(LIGHTNINGD_CONNECT_CONTROL_OBJS) lightningd/lightningd: $(LIGHTNINGD_OBJS) $(LIGHTNINGD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(WIRE_ONION_OBJS) $(LIGHTNINGD_HANDSHAKE_CONTROL_OBJS) $(LIGHTNINGD_GOSSIP_CONTROL_OBJS) $(LIGHTNINGD_OPENING_CONTROL_OBJS) $(LIGHTNINGD_CHANNEL_CONTROL_OBJS) $(LIGHTNINGD_CLOSING_CONTROL_OBJS) $(LIGHTNINGD_ONCHAIN_CONTROL_OBJS) $(WALLET_LIB_OBJS) $(LIGHTNINGD_CONNECT_CONTROL_OBJS)
clean: lightningd-clean clean: lightningd-clean

15
lightningd/jsonrpc.c

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

2
lightningd/jsonrpc.h

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

Loading…
Cancel
Save