MCPcopy Create free account
hub / github.com/ElementsProject/lightning / maybe_throttle_usec

Function maybe_throttle_usec

connectd/multiplex.c:665–695  ·  view source on GitHub ↗

Checks two (usage, limit) pairs (e.g. bytes and CPU usec): if * neither is over, returns 0. Otherwise logs it (once) and returns * how many usec to wait before retrying: how long, averaging the * burn in over the time since window_start, until we're back under * budget. No cap: a peer who burns 4 seconds worth of quota in one * go waits ~4 seconds, not just 1. But always at least 1 second,

Source from the content-addressed store, hash-verified

663 * go waits ~4 seconds, not just 1. But always at least 1 second, as
664 * a flat penalty even for a marginal overage. */
665static u64 maybe_throttle_usec(struct peer *peer, bool *warned, const char *direction,
666 u64 usage1, u64 limit1,
667 u64 usage2, u64 limit2)
668{
669 u64 need, need2, elapsed, wait;
670
671 if (usage1 <= limit1 && usage2 <= limit2)
672 return 0;
673
674 /* A peer hitting its budget is expected (that's what it's for), so
675 * don't make noise about it. */
676 if (!*warned) {
677 status_debug("Throttling %s peer %s: too much %s",
678 direction,
679 fmt_node_id(tmpctx, &peer->id),
680 usage1 > limit1 ? "traffic" : "CPU");
681 *warned = true;
682 }
683
684 need = usec_over_budget(usage1, limit1);
685 need2 = usec_over_budget(usage2, limit2);
686 if (need2 > need)
687 need = need2;
688
689 elapsed = time_to_usec(timemono_between(time_mono(), peer->gs.window_start));
690 wait = (need > elapsed) ? need - elapsed : 0;
691 if (wait < 1000000)
692 wait = 1000000;
693
694 return wait;
695}
696
697/* Gossip response or something from gossip store */
698static const u8 *maybe_gossip_msg(const tal_t *ctx, struct peer *peer)

Callers 2

maybe_gossip_msgFunction · 0.85
read_hdr_from_peerFunction · 0.85

Calls 5

fmt_node_idFunction · 0.85
usec_over_budgetFunction · 0.85
time_to_usecFunction · 0.85
timemono_betweenFunction · 0.85
time_monoFunction · 0.85

Tested by

no test coverage detected