1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: jan@swi-prolog.org 5 WWW: https://www.swi-prolog.org 6 Copyright (C): 2026, SWI-Prolog Solutions b.v. 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 As a special exception, if you link this library with other files, 23 compiled with a Free Software compiler, to produce an executable, this 24 library does not by itself cause the resulting executable to be covered 25 by the GNU General Public License. This exception does not however 26 invalidate any other reasons why the executable file might be covered by 27 the GNU General Public License. 28*/ 29 30:- module(rate_limit, 31 [ rate_limit/4 % +Action, +Who, +Max, +Period 32 ]). 33:- use_module(library(aggregate)). 34:- use_module(library(debug)). 35 36/** <module> Limit the rate of user actions 37 38Actions that add data to the site's databases or send mail are limited 39per user, so a single (compromised or malicious) account cannot flood 40the databases or the mailboxes of watchers. The administration is kept 41in memory, so it is reset when the server restarts. 42*/ 43 44:- dynamic 45 rate_event/4, % Action, Who, Time, Expires 46 last_gc/1. 47 48%! rate_limit(+Action, +Who, +Max, +Period) is det. 49% 50% Record that Who performs Action. If Who already performed Action 51% Max times during the last Period seconds, raise an HTTP 503 52% (Service Unavailable) reply instead. 53 54rate_limit(Action, Who, Max, Period) :- 55 get_time(Now), 56 with_mutex(rate_limit, 57 rate_limit_(Action, Who, Max, Period, Now, Allowed)), 58 ( Allowed == true 59 -> true 60 ; debug(rate_limit, 'Rate limit exceeded: ~q by ~q', [Action, Who]), 61 throw(http_reply(unavailable( 62 p(['Too many requests. ', 63 'Please try again later.'])))) 64 ). 65 66rate_limit_(Action, Who, Max, Period, Now, Allowed) :- 67 gc_events(Now), 68 Since is Now-Period, 69 forall(( rate_event(Action, Who, Time, Expires), 70 Time < Since 71 ), 72 retractall(rate_event(Action, Who, Time, Expires))), 73 aggregate_all(count, rate_event(Action, Who, _, _), Count), 74 ( Count >= Max 75 -> Allowed = false 76 ; Expires is Now+Period, 77 assertz(rate_event(Action, Who, Now, Expires)), 78 Allowed = true 79 ). 80 81% gc_events(+Now) 82% 83% Every 10 minutes, remove all expired events, so users that do 84% not come back do not leave records behind. 85 86gc_events(Now) :- 87 last_gc(Last), 88 Now-Last < 600, 89 !. 90gc_events(Now) :- 91 retractall(last_gc(_)), 92 assertz(last_gc(Now)), 93 forall(( rate_event(Action, Who, Time, Expires), 94 Expires < Now 95 ), 96 retractall(rate_event(Action, Who, Time, Expires)))