keyindex

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 1 Imported by: ? 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 keyindex is the lookup table of API keys: a lookup costs a bounded amount whatever the key a client presents.

  • a key longer than the longest configured key is refused without being hashed: it can not be a configured key, whatever it holds, so the cap never refuses a valid key and it is no fixed limit;
  • the key is hashed once with a seeded hash (a random seed per index, so a client can not choose keys that collide), and the table is keyed by the hash: O(1) among any number of keys;
  • a key is compared with a configured one only when the hashes are equal, against the full configured key, and the comparison reads every byte (no early exit), so the time of a lookup does not depend on how much of a key is right. A hash is a bucket, not an identity: every key of a hash is kept, none is replaced.

An index is immutable once built: any number of goroutines may read it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Index

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

Index is the lookup table of a list of keys: Find returns the position of a key in the list.

func New

func New(keys []string) *Index

New indexes the keys with a random seeded hash. A key that is repeated keeps its first position.

func NewWithHash

func NewWithHash(keys []string, hash func(string) uint64) *Index

NewWithHash is New with the hash function given: it exists so that a test can force collisions.

func (*Index) Find

func (idx *Index) Find(key string) (int, bool)

Find returns the position of the key in the list the index was built from. It does not allocate.

Jump to

Keyboard shortcuts

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