domain

package
v0.8.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockCache

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

BlockCache provides a means of caching domain blocks in memory to reduce load on an underlying storage mechanism, e.g. a database.

It consists of a TTL primary cache that stores calculated domain string to block results, that on cache miss is filled by calculating block status by iterating over a list of all of the domain blocks stored in memory. This reduces CPU usage required by not need needing to iterate through a possible 100-1000s long block list, while saving memory by having a primary cache of limited size that evicts stale entries. The raw list of all domain blocks should in most cases be negligible when it comes to memory usage.

The in-memory block list is kept up-to-date by means of a passed loader function during every call to .IsBlocked(). In the case of a nil internal block list, the loader function is called to hydrate the cache with the latest list of domain blocks. The .Clear() function can be used to invalidate the cache, e.g. when a domain block is added / deleted from the database. It will drop the current list of domain blocks and clear all entries from the primary cache.

func New

func New(pcap int, pttl time.Duration) *BlockCache

New returns a new initialized BlockCache instance with given primary cache capacity and TTL.

func (*BlockCache) Clear

func (b *BlockCache) Clear()

Clear will drop the currently loaded domain list, and clear the primary cache. This will trigger a reload on next call to .IsBlocked().

func (*BlockCache) IsBlocked

func (b *BlockCache) IsBlocked(domain string, load func() ([]string, error)) (bool, error)

IsBlocked checks whether domain is blocked. If the cache is not currently loaded, then the provided load function is used to hydrate it. NOTE: be VERY careful using any kind of locking mechanism within the load function, as this itself is ran within the cache mutex lock.

func (*BlockCache) Start

func (b *BlockCache) Start(pfreq time.Duration) bool

Start will start the cache background eviction routine with given sweep frequency. If already running or a freq <= 0 provided, this is a no-op. This will block until the eviction routine has started.

func (*BlockCache) Stop

func (b *BlockCache) Stop() bool

Stop will stop cache background eviction routine. If not running this is a no-op. This will block until the eviction routine has stopped.

Jump to

Keyboard shortcuts

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