/* Part of SWI-Prolog Author: Jan Wielemaker E-mail: jan@swi-prolog.org WWW: https://www.swi-prolog.org Copyright (C): 2026, SWI-Prolog Solutions b.v. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ :- module(rate_limit, [ rate_limit/4 % +Action, +Who, +Max, +Period ]). :- use_module(library(aggregate)). :- use_module(library(debug)). /** Limit the rate of user actions Actions that add data to the site's databases or send mail are limited per user, so a single (compromised or malicious) account cannot flood the databases or the mailboxes of watchers. The administration is kept in memory, so it is reset when the server restarts. */ :- dynamic rate_event/4, % Action, Who, Time, Expires last_gc/1. %! rate_limit(+Action, +Who, +Max, +Period) is det. % % Record that Who performs Action. If Who already performed Action % Max times during the last Period seconds, raise an HTTP 503 % (Service Unavailable) reply instead. rate_limit(Action, Who, Max, Period) :- get_time(Now), with_mutex(rate_limit, rate_limit_(Action, Who, Max, Period, Now, Allowed)), ( Allowed == true -> true ; debug(rate_limit, 'Rate limit exceeded: ~q by ~q', [Action, Who]), throw(http_reply(unavailable( p(['Too many requests. ', 'Please try again later.'])))) ). rate_limit_(Action, Who, Max, Period, Now, Allowed) :- gc_events(Now), Since is Now-Period, forall(( rate_event(Action, Who, Time, Expires), Time < Since ), retractall(rate_event(Action, Who, Time, Expires))), aggregate_all(count, rate_event(Action, Who, _, _), Count), ( Count >= Max -> Allowed = false ; Expires is Now+Period, assertz(rate_event(Action, Who, Now, Expires)), Allowed = true ). % gc_events(+Now) % % Every 10 minutes, remove all expired events, so users that do % not come back do not leave records behind. gc_events(Now) :- last_gc(Last), Now-Last < 600, !. gc_events(Now) :- retractall(last_gc(_)), assertz(last_gc(Now)), forall(( rate_event(Action, Who, Time, Expires), Expires < Now ), retractall(rate_event(Action, Who, Time, Expires))).