From a8df1abc2bc967324c9a8130b17158823e319b79 Mon Sep 17 00:00:00 2001 From: Doug Hoyte Date: Thu, 17 Aug 2023 16:29:06 -0400 Subject: [PATCH] remove lookbehind feature from plugins --- docs/plugins.md | 10 ++-------- src/PluginWritePolicy.h | 42 +++------------------------------------ src/apps/relay/golpe.yaml | 3 --- strfry.conf | 3 --- 4 files changed, 5 insertions(+), 53 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index cebd829..6b480bf 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -10,17 +10,15 @@ A plugin can be implemented in any programming language that supports reading li Whenever the script's modification-time changes, or the plugin settings in `strfry.conf` change, the plugin will be reloaded upon the next write attempt. -If configured, When a plugin is loaded some number of recently stored events will be sent to it as a "lookback". This is useful for populating the initial rate-limiting state. Plugins should print nothing in response to a lookback message. - ## Input messages Input messages contain the following keys: -* `type`: Either `new` or `lookback` +* `type`: Currently always `new` * `event`: The event posted by the client, with all the required fields such as `id`, `pubkey`, etc * `receivedAt`: Unix timestamp of when this event was received by the relay -* `sourceType`: Where this event came from. Typically will be `IP4` or `IP6`, but in lookback can also be `Import`, `Stream`, or `Sync`. +* `sourceType`: The channel where this event came from: `IP4`, `IP6`, `Import`, `Stream`, or `Sync`. * `sourceInfo`: Specifics of the event's source. Either an IP address or a relay URL (for stream/sync) @@ -52,10 +50,6 @@ Here is a simple example `whitelist.js` plugin that will reject all events excep rl.on('line', (line) => { let req = JSON.parse(line); - if (req.type === 'lookback') { - return; // do nothing - } - if (req.type !== 'new') { console.error("unexpected request type"); // will appear in strfry logs return; diff --git a/src/PluginWritePolicy.h b/src/PluginWritePolicy.h index 452c4a1..8696d2d 100644 --- a/src/PluginWritePolicy.h +++ b/src/PluginWritePolicy.h @@ -27,12 +27,11 @@ struct PluginWritePolicy { struct RunningPlugin { pid_t pid; std::string currPluginPath; - uint64_t lookbackSeconds; struct timespec lastModTime; FILE *r; FILE *w; - RunningPlugin(pid_t pid, int rfd, int wfd, std::string currPluginPath, uint64_t lookbackSeconds) : pid(pid), currPluginPath(currPluginPath), lookbackSeconds(lookbackSeconds) { + RunningPlugin(pid_t pid, int rfd, int wfd, std::string currPluginPath) : pid(pid), currPluginPath(currPluginPath) { r = fdopen(rfd, "r"); w = fdopen(wfd, "w"); setlinebuf(w); @@ -63,7 +62,7 @@ struct PluginWritePolicy { try { if (running) { - if (pluginPath != running->currPluginPath || cfg().relay__writePolicy__lookbackSeconds != running->lookbackSeconds) { + if (pluginPath != running->currPluginPath) { running.reset(); } else { struct stat statbuf; @@ -76,7 +75,6 @@ struct PluginWritePolicy { if (!running) { setupPlugin(); - sendLookbackEvents(); } auto request = tao::json::value({ @@ -176,40 +174,6 @@ struct PluginWritePolicy { auto ret = posix_spawn(&pid, path.c_str(), &file_actions, nullptr, argv, nullptr); if (ret) throw herr("posix_spawn failed to invoke '", path, "': ", strerror(errno)); - running = make_unique(pid, inPipe.saveFd(0), outPipe.saveFd(1), path, cfg().relay__writePolicy__lookbackSeconds); - } - - void sendLookbackEvents() { - if (running->lookbackSeconds == 0) return; - - Decompressor decomp; - auto now = hoytech::curr_time_us(); - - uint64_t start = now - (running->lookbackSeconds * 1'000'000); - - auto txn = env.txn_ro(); - - env.generic_foreachFull(txn, env.dbi_Event__receivedAt, lmdb::to_sv(start), lmdb::to_sv(0), [&](auto k, auto v) { - if (lmdb::from_sv(k) > now) return false; - - auto ev = lookupEventByLevId(txn, lmdb::from_sv(v)); - auto sourceType = (EventSourceType)ev.sourceType(); - std::string_view sourceInfo = ev.sourceInfo(); - - auto request = tao::json::value({ - { "type", "lookback" }, - { "event", tao::json::from_string(getEventJson(txn, decomp, ev.primaryKeyId)) }, - { "receivedAt", ev.receivedAt() / 1000000 }, - { "sourceType", eventSourceTypeToStr(sourceType) }, - { "sourceInfo", sourceType == EventSourceType::IP4 || sourceType == EventSourceType::IP6 ? renderIP(sourceInfo) : sourceInfo }, - }); - - std::string output = tao::json::to_string(request); - output += "\n"; - - if (::fwrite(output.data(), 1, output.size(), running->w) != output.size()) throw herr("error writing to plugin"); - - return true; - }); + running = make_unique(pid, inPipe.saveFd(0), outPipe.saveFd(1), path); } }; diff --git a/src/apps/relay/golpe.yaml b/src/apps/relay/golpe.yaml index 351fe5c..aecc62b 100644 --- a/src/apps/relay/golpe.yaml +++ b/src/apps/relay/golpe.yaml @@ -52,9 +52,6 @@ config: - name: relay__writePolicy__plugin desc: "If non-empty, path to an executable script that implements the writePolicy plugin logic" default: "" - - name: relay__writePolicy__lookbackSeconds - desc: "Number of seconds to search backwards for lookback events when starting the writePolicy plugin (0 for no lookback)" - default: 0 - name: relay__compression__enabled desc: "Use permessage-deflate compression if supported by client. Reduces bandwidth, but slight increase in CPU" diff --git a/strfry.conf b/strfry.conf index a78b44f..6f9242a 100644 --- a/strfry.conf +++ b/strfry.conf @@ -87,9 +87,6 @@ relay { writePolicy { # If non-empty, path to an executable script that implements the writePolicy plugin logic plugin = "" - - # Number of seconds to search backwards for lookback events when starting the writePolicy plugin (0 for no lookback) - lookbackSeconds = 0 } compression {