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,
| 663 | * go waits ~4 seconds, not just 1. But always at least 1 second, as |
| 664 | * a flat penalty even for a marginal overage. */ |
| 665 | static 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 */ |
| 698 | static const u8 *maybe_gossip_msg(const tal_t *ctx, struct peer *peer) |
no test coverage detected