<<

NAME

Koha::Middleware::ComplexityScore - Plack middleware to send a per-endpoint complexity score header

SYNOPSIS

  builder {
      ...
      enable "+Koha::Middleware::ComplexityScore";
      ...
  }

DESCRIPTION

This middleware adds a response header weighting each request by how expensive the requested endpoint is. A reverse proxy or WAF (for example Cloudflare Rate Limiting rules) can read the header and make heavy endpoints consume more of a client's rate limit budget, so scrapers walking the catalog get throttled on the expensive pages without blanket-limiting normal users.

The feature is disabled unless enabled in koha-conf.xml:

  <complexity_score_header>1</complexity_score_header>
  <complexity_score_header_name>x-complexity-score</complexity_score_header_name>

complexity_score_header_name is optional and defaults to 'x-complexity-score'.

The weights are a map keyed on the request's script name. They are based on the number of searches, database calls, etc per script.

An instance can override or extend them in koha-conf.xml without editing this module:

  <complexity_score_weights>
    <opac-search.pl>30</opac-search.pl>
    <opac-detail.pl>25</opac-detail.pl>
  </complexity_score_weights>

Any script listed there replaces its default weight; scripts that are not listed keep their default.

METHODS

prepare_app

Reads the koha-conf.xml configuration once at Plack startup so it doesn't have to be re-read on every request.

call

When the feature is enabled, sets the complexity score response header based on the requested endpoint.

score_for_path

  my $score = $self->score_for_path($path);

Returns the complexity weight for a request path. Anything not in the configured weights gets the default weight of 1.

AUTHORS

Kyle M Hall <kyle@bywatersolutions.com>

<<