trafficclass

package
v1.5.10 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package trafficclass sorts each edge request into one class so analytics can tell page views from API calls, assets and bots. It is a pure function of the request and response headers: no state, no allocation on the hot path.

Index

Constants

View Source
const Count = int(Ignored)

Count is the number of user-visible classes (Ignored is not one of them).

View Source
const DefaultRulesMax = 50

DefaultRulesMax caps path rules per app when APP_TRAFFIC_RULES_MAX is unset.

View Source
const UnknownBot = "unknown automated"

UnknownBot labels an automated client that matched no named token.

Variables

This section is empty.

Functions

func IsPageView

func IsPageView(method string, status int) bool

IsPageView reports whether a page-class request counts as a page view: a successful GET. Redirects and error documents are not views.

func RulesMaxFromEnv

func RulesMaxFromEnv() int

RulesMaxFromEnv reads APP_TRAFFIC_RULES_MAX.

Types

type Class

type Class uint8

Class is the kind of request the edge saw. The zero value is Other.

const (
	Other Class = iota
	Page
	API
	Asset
	Bot
	Prefetch
	// Ignored counts in RED metrics but never in analytics (health checks).
	Ignored
)

The six analytics classes, then Ignored for requests a path rule excludes.

func (Class) String

func (c Class) String() string

String is the wire name of the class.

type Response

type Response struct {
	Status      int
	ContentType string
	// Shielded is true when the abuse shield already turned the request away.
	Shielded bool
}

Response is what the classifier needs from the answer.

type Result

type Result struct {
	Class Class
	// Soft is a client-side navigation (a Next.js RSC fetch), counted as a page view.
	Soft bool
	// Bot names the matched crawler token, or UnknownBot.
	Bot string
	// Rule is the 1-based index of the path rule that decided, 0 when none did.
	Rule int
}

Result is the class plus the facts a caller may record next to it.

func Classify

func Classify(r *http.Request, resp Response, rules *Rules) Result

Classify decides the class of one finished request. The first matching step wins: path rules, bots, prefetch, client navigation, Sec-Fetch-Dest, fallback. A nil rules value means DefaultRules.

type Rule

type Rule struct {
	Pattern string
	Class   Class
}

Rule maps a path glob to a class. Only page, api, asset and ignored are valid targets. In Pattern, "*" matches any run of characters including "/"; a pattern with no "*" matches exactly.

type Rules

type Rules struct {
	// contains filtered or unexported fields
}

Rules is an ordered, immutable list of path rules; the first match wins.

func DefaultRules

func DefaultRules() *Rules

DefaultRules returns the built-in rules the spec ships; callers replace them per app.

func NewRules

func NewRules(rs []Rule) (*Rules, error)

NewRules validates rs against the per-app cap and returns them as a rule set.

func (*Rules) List

func (r *Rules) List() []Rule

List returns a copy of the rules in order.

Jump to

Keyboard shortcuts

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