phpfastcgiconnlimit

package module
v0.0.0-...-39d253d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 10 Imported by: 0

README

caddy-php-fastcgi-connlimit CI

This Caddy module limits the number of concurrent connections made by a php_fastcgi handler. Requests wait for a free slot instead of being rejected. If a request is canceled while waiting, it is removed from the queue.

The limit applies only to requests that reach the generated FastCGI reverse proxy. Static files and other handlers are not limited.

Caddyfile

Use php_fastcgi_connlimit in place of php_fastcgi. The first argument after an optional matcher is the connection limit. The remaining syntax is the same as Caddy's php_fastcgi directive.

wiki.gslin.org {
	root * /srv/wiki
	php_fastcgi_connlimit 1 unix//run/php/php8.4-fpm.sock
	file_server
}

All standard php_fastcgi subdirectives are passed to Caddy's own parser:

wiki.gslin.org {
	root * /srv/wiki
	php_fastcgi_connlimit 2 unix//run/php/php8.4-fpm.sock {
		capture_stderr
		env APP_ENV production
	}
	file_server
}

The standard directive can also be nested when its original layout is useful:

wiki.gslin.org {
	php_fastcgi_connlimit 1 {
		php_fastcgi unix//run/php/php8.4-fpm.sock
	}
}

Syntax:

php_fastcgi_connlimit [<matcher>] <max_connections> <upstreams...> {
	<php_fastcgi subdirectives...>
}

The limit is shared by all upstreams declared in one directive. Separate directive instances have separate limits.

Why two adjacent directives are not supported

This form cannot precisely limit only FastCGI connections through Caddy's public module API:

php_fastcgi unix//run/php/php8.4-fpm.sock
php_fastcgi_connlimit 1

php_fastcgi expands into a self-contained subroute. A sibling middleware can run before or after that subroute, but it cannot be inserted into it. Running before it would also limit static requests, while running after it would not see PHP requests. This module instead asks Caddy's standard php_fastcgi parser to build the subroute and inserts the limiter immediately before its FastCGI reverse proxy.

Build

For local development:

go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
xcaddy build v2.11.4 \
	--with github.com/gslin/caddy-php-fastcgi-connlimit=.

After the module is published, omit the local replacement:

xcaddy build v2.11.4 \
	--with github.com/gslin/caddy-php-fastcgi-connlimit

Test

go test -race ./...

The current code and tests target Caddy v2.11.4.

Development disclosure

This project was developed with assistance from OpenAI Codex, a large language model (LLM). All assisted changes were reviewed and tested by the maintainer.

License

MIT

Documentation

Overview

Package phpfastcgiconnlimit limits concurrent PHP FastCGI connections.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	MaxConnections int `json:"max_connections"`
	// contains filtered or unexported fields
}

Handler limits the number of concurrent requests passed to the next handler. The Caddyfile adapter inserts it immediately before the FastCGI reverse proxy.

func (Handler) CaddyModule

func (Handler) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Handler) Provision

func (h *Handler) Provision(caddy.Context) error

Provision initializes the connection semaphore.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

ServeHTTP waits for a connection slot, then calls the FastCGI reverse proxy.

func (Handler) Validate

func (h Handler) Validate() error

Validate validates the configured connection limit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL