model

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPayloadIsEmpty   = fmt.Errorf("payload is empty")
	ErrMalformedPayload = errors.New("malformed payload")
)

Functions

func MatchRule added in v0.9.3

func MatchRule(cfg config.Config, path []byte) *config.Rule

func WetherAnErrIsTheRuleNotFound added in v1.2.0

func WetherAnErrIsTheRuleNotFound(err error) bool

Types

type CacheItem added in v1.2.0

type CacheItem interface {
	Rule() *config.Rule
	MapKey() uint64
	ShardKey() uint64
	SwapPayloads(another *Entry)
	IsSamePayload(another *Entry) bool
	SetPayload(req *fasthttp.Request, resp *fasthttp.Response) *Entry
	Payload() (req *fasthttp.Request, resp *fasthttp.Response, releaser Releaser, err error)
	ShouldBeRefreshed(cfg config.Config) bool
	ToBytes() (data []byte, releaseFn func())
	TouchRefreshedAt()
	RefreshedAt() int64
	TouchedAt() int64
	Touch()
	Weight() int64
}

type Entry added in v0.9.3

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

func EntryFromBytes added in v0.9.3

func EntryFromBytes(data []byte, cfg config.Config) (*Entry, error)

EntryFromBytes decodes Entry from the wire format described in ToBytes. Strict bounds checks are applied to prevent panics on malformed input.

Zero-copy semantics:

The returned Entry will reference the provided `data` slice for payload
(and rulePath is used only for rule lookup). Ensure `data` remains valid
for as long as the Entry may access the payload (or copy the payload in
your constructor, if that is your policy).

func NewEntry added in v1.2.0

func NewEntry(cfg config.Config, r *fasthttp.RequestCtx) (*Entry, error)

func NewEntryFromField added in v0.9.3

func NewEntryFromField(
	key uint64,
	shard uint64,
	payload []byte,
	rule *config.Rule,
	updatedAt int64,
) *Entry

func NewEntryWithPayload added in v1.2.0

func NewEntryWithPayload(cfg config.Config, req *fasthttp.Request, resp *fasthttp.Response) (*Entry, error)

func (*Entry) DumpBuffer added in v1.0.1

func (e *Entry) DumpBuffer(r *fasthttp.RequestCtx)

func (*Entry) Init added in v0.9.3

func (e *Entry) Init() *Entry

func (*Entry) IsSamePayload added in v1.0.1

func (e *Entry) IsSamePayload(another *Entry) bool

func (*Entry) MapKey added in v0.9.3

func (e *Entry) MapKey() uint64

func (*Entry) Payload added in v0.9.3

func (e *Entry) Payload() (req *fasthttp.Request, resp *fasthttp.Response, releaser Releaser, err error)

Payload decodes e.payload into new fasthttp.Request/Response instances. On error, releases any acquired objects, logs the stage and offset, and returns an error.

func (*Entry) PayloadBytes added in v0.9.3

func (e *Entry) PayloadBytes() []byte

func (*Entry) RefreshedAt added in v1.2.0

func (e *Entry) RefreshedAt() int64

func (*Entry) Release added in v0.9.3

func (e *Entry) Release()

func (*Entry) ReleasePayload added in v1.4.0

func (e *Entry) ReleasePayload()

func (*Entry) Rule added in v0.9.3

func (e *Entry) Rule() *config.Rule

func (*Entry) SetMapKeyForTests added in v1.2.0

func (e *Entry) SetMapKeyForTests(key uint64) *Entry

SetMapKeyForTests is really dangerous - must be used exclusively in tests.

func (*Entry) SetPayload added in v0.9.3

func (e *Entry) SetPayload(req *fasthttp.Request, resp *fasthttp.Response) *Entry

SetPayload packs and gzip-compresses the entire payload: Path, Query, QueryHeaders, StatusCode, ResponseHeaders, Body. SetPayload encodes (req, resp) into Entry storage using the protocol above. It precomputes the total size to minimize reallocations and writes lengths in LE.

Preconditions:

  • e.payload must own the backing *[]byte for the lifetime of any decoded response (i.e., do NOT return this buffer to a pool while response is in use).

func (*Entry) ShardKey added in v0.9.3

func (e *Entry) ShardKey() uint64

func (*Entry) ShouldBeRefreshed added in v0.9.3

func (e *Entry) ShouldBeRefreshed(cfg config.Config) bool

ShouldBeRefreshed implements probabilistic refresh logic ("beta" algorithm). Returns true if the entry is stale and, with a probability proportional to its staleness, should be refreshed now.

func (*Entry) SwapPayloads added in v1.0.1

func (e *Entry) SwapPayloads(another *Entry)

func (*Entry) ToBytes added in v0.9.3

func (e *Entry) ToBytes() (data []byte, releaseFn func())

ToBytes encodes Entry into a compact little-endian binary format. Fingerprint is intentionally excluded from the wire format.

Layout (Little-Endian):

uint32  rulePathLen
[]byte  rulePath
uint64  key
uint64  shard
uint64  refreshedAtUnix // seconds or nanos — whatever RefreshedAt() returns
uint32  payloadLen
[]byte  payload

Notes:

  • Uses a pooled bytes.Buffer to minimize allocations.
  • No errors are returned; bytes.Buffer.Write never fails.
  • If Rule() is nil, this will panic (by design: Entry is assumed well-formed).

func (*Entry) Touch added in v1.4.4

func (e *Entry) Touch()

func (*Entry) TouchRefreshedAt added in v1.4.4

func (e *Entry) TouchRefreshedAt()

func (*Entry) TouchedAt added in v1.4.4

func (e *Entry) TouchedAt() int64

func (*Entry) Weight added in v0.9.3

func (e *Entry) Weight() int64

type Releaser added in v0.9.3

type Releaser func(request *fasthttp.Request, response *fasthttp.Response)

Jump to

Keyboard shortcuts

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