Documentation
¶
Overview ¶
Package hsmatch is a thin, build-tag gated wrapper around the gohs (Hyperscan / Vectorscan) library.
It exposes a small block-mode multi-pattern matcher used by netcap to pre-filter input buffers before running heavier RE2 / regexp2 matchers for capture-group extraction.
Build behaviour
- Default builds (no tag): a stub implementation is compiled. All exported functions return ErrDisabled and callers fall back to their existing regex engine. Zero CGO / C library dependencies.
- With `-tags hyperscan`: the real implementation backed by github.com/flier/gohs is compiled. Requires libhs (Intel Hyperscan on x86_64 or VectorCamp Vectorscan on ARM/macOS) discoverable via pkg-config (libhs.pc).
Hyperscan does not return submatch offsets – only the (id, from, to) of the overall match. The intended usage pattern is therefore:
- Compile a DB from many [Pattern]s once at startup.
- For each input buffer call DB.Match; collect the IDs that hit.
- For each hit, run the original capture-aware regex (RE2 / regexp2) to extract groups – but only for the small set of patterns that actually matched, not the full pattern list.
Patterns that Hyperscan refuses to compile (e.g. because they use backreferences or other PCRE-only features) are reported via the `rejections` slice returned from Compile – including the original pattern index, expression and the libhs/gohs error message – so the caller can keep these on the existing regex engine and surface useful diagnostics in logs and the web UI.
Runtime counters (scans, matches, scan errors) are exposed through DB.Stats; the optional integration in `decoder/stream/service` aggregates them into a JSON-friendly HyperscanStatus that the web UI exposes at GET /api/hyperscan.
Index ¶
- Constants
- Variables
- func Compile(patterns []Pattern) (*DB, []Rejection, error)
- func RegisterSubsystem(s Subsystem)
- func SnapshotMap() map[string]any
- func UnregisterSubsystem(name string)
- func Version() string
- type DB
- type Flag
- type Hit
- type NamedSnapshot
- type Pattern
- type Rejection
- type Stats
- type Subsystem
- type SubsystemFunc
Constants ¶
const Enabled = false
Enabled reports whether this build was compiled with Hyperscan support.
Variables ¶
var ErrClosed = errors.New("hsmatch: database closed")
ErrClosed mirrors the tagged-build sentinel; never produced here.
var ErrDisabled = errors.New("hsmatch: hyperscan support not compiled in (rebuild with -tags hyperscan)")
ErrDisabled is returned by every entry point in this stub build.
Functions ¶
func Compile ¶
Compile reports every supplied pattern as rejected and returns ErrDisabled.
Callers are expected to check `Enabled` (or the returned error) and fall back to their pre-existing regex engine.
func RegisterSubsystem ¶
func RegisterSubsystem(s Subsystem)
RegisterSubsystem adds (or replaces) a subsystem-stats producer keyed by name. Safe to call from package init or from runtime build hooks.
Both build configurations expose this function so registration sites remain unconditional. In stub builds, registered snapshots still flow through Snapshot()/SnapshotMap() — useful for "Enabled: false" surfaces.
func SnapshotMap ¶
SnapshotMap is a convenience for callers (the web UI) that prefer a map keyed by subsystem name over the ordered slice from Snapshot.
func UnregisterSubsystem ¶
func UnregisterSubsystem(name string)
UnregisterSubsystem removes a previously registered producer. Used by tests; production code typically registers once at init.
Types ¶
type DB ¶
type DB struct{}
DB is an opaque placeholder; all methods report ErrDisabled.
type NamedSnapshot ¶
NamedSnapshot pairs a subsystem name with its current snapshot.
func Snapshot ¶
func Snapshot() []NamedSnapshot
Snapshot returns the registered subsystem snapshots in deterministic (name-sorted) order so HTTP responses and logs are reproducible.
type Subsystem ¶
Subsystem returns a free-form, JSON-friendly snapshot of one Hyperscan consumer (service probes, CMS detection, rule engine, …). The exact shape is owned by the consumer — the registry just routes it.
type SubsystemFunc ¶
SubsystemFunc adapts a closure into a Subsystem.
func (SubsystemFunc) Snapshot ¶
func (s SubsystemFunc) Snapshot() any
Snapshot implements Subsystem.