/* The inbox (docs/design/grapevine.md): another process's command for a * conversation, one Pi RPC command line in a pic_inbox row. A sender inserts * it through the narrow open (pic_open_narrow) with no lease and rings the * conversation's bell; the process running a request in that conversation * delivers it through the command table (commands.c), in one transaction * with what it made. Delivered once, or settled as refused or expired; * never twice. Stage 1: steer, follow_up and abort, same workspace. */ #ifndef PIC_INBOX_H #define PIC_INBOX_H #include #include "pic.h" #define PIC_INBOX_STEER_SECONDS 600L /* default expiry of a steer or abort */ #define PIC_INBOX_FOLLOW_UP_SECONDS 86400L /* of a follow-up */ #define PIC_INBOX_MAX_SECONDS (7L * 86400L) /* the ceiling on any */ #define PIC_INBOX_LINE_CAP (16u << 20) /* a command line, as on stdin */ #define PIC_INBOX_PENDING_ROWS 100 /* per conversation */ #define PIC_INBOX_PENDING_BYTES (64LL << 20) /* per conversation */ #define PIC_INBOX_KEEP_MS (7LL * 86400000LL) /* a settled row's command and response */ typedef struct { const char *session; /* the conversation's id, resolved by the caller */ const char *key; /* the idempotency key; NULL: a random one */ const char *line; /* one Pi RPC command line: steer, follow_up or abort */ const char *sender; /* "cli", "attach" */ long expires_s; /* 0: the default for its type */ } pic_inbox_msg; /* Accepts M in one transaction and rings the bell. A known key with the same * command changes nothing. 0: committed (or already was); its key in key * and its state in state. 1: refused, nothing written (s->err): a key with * another command, a command the inbox does not take, a full inbox, no * such conversation. -1: an error (s->err). */ int pic_inbox_accept(pic_store *s, const pic_inbox_msg *m, char *key, size_t key_cap, char *state, size_t state_cap); /* KEY's state, and its response line (malloc'd, NULL while pending or * pruned) when RESPONSE is not NULL. 0; 1 when no row has the key; -1. */ int pic_inbox_status(pic_store *s, const char *key, char *state, size_t cap, char **response); /* The worker's side, called by pic_run at each step while it holds the * running request's lease: watches that conversation's bell (the watch * descriptor in net.h) and delivers its pending messages in order. */ void pic_inbox_poll(pic_store *s); /* Stops the watch; pic_run calls it as it returns. */ void pic_inbox_stop(void); /* Retention, run where pic prunes: pending rows past their expiry become * expired; settled rows older than PIC_INBOX_KEEP_MS lose their command * and response, keeping key, hash and state. 0 or -1. */ int pic_inbox_retain(pic_store *s, long long now_ms); #endif