cacher

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package cacher implements main logic for go-apt-cacher.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned by Storage.Lookup for non-existing items.
	ErrNotFound = errors.New("not found")

	// ErrBadPath is returned by Storage.Insert if path is bad
	ErrBadPath = errors.New("bad path")
)
View Source
var (

	// ErrInvalidPrefix returned for invalid prefix.
	ErrInvalidPrefix = errors.New("invalid prefix")
)

Functions

func NewServer added in v1.2.0

func NewServer(c *Cacher, config *Config) *well.HTTPServer

NewServer returns HTTPServer implements go-apt-cacher handlers.

Types

type Cacher

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

Cacher downloads and caches APT indices and deb files.

func NewCacher

func NewCacher(config *Config) (*Cacher, error)

NewCacher constructs Cacher.

func (*Cacher) Download

func (c *Cacher) Download(p string, valid *apt.FileInfo) <-chan struct{}

Download downloads an item and caches it.

If valid is not nil, the downloaded data is validated against it.

The caller receives a channel that will be closed when the item is downloaded and cached. If prefix of p is not registered in URLMap, nil is returned.

Note that download may fail, or just invalidated soon. Users of this method should retry if the item is not cached or invalidated.

func (*Cacher) Get

func (c *Cacher) Get(p string) (statusCode int, f *os.File, err error)

Get looks up a cached item, and if not found, downloads it from the upstream server.

The return values are cached HTTP status code of the response from an upstream server, a pointer to os.File for the cache file, and error.

type Config

type Config struct {
	// Addr is the listening address of HTTP server.
	//
	// Default is ":3142".
	Addr string `toml:"listen_address"`

	// CheckInterval specifies interval in seconds to check updates for
	// Release/InRelease files.
	//
	// Default is 600 seconds.
	CheckInterval int `toml:"check_interval"`

	// CachePeriod specifies the period to cache bad HTTP response statuses.
	//
	// Default is 3 seconds.
	CachePeriod int `toml:"cache_period"`

	// MetaDirectory specifies a directory to store APT meta data files.
	//
	// This must differ from CacheDirectory.
	MetaDirectory string `toml:"meta_dir"`

	// CacheDirectory specifies a directory to cache non-meta data files.
	//
	// This must differ from MetaDirectory.
	CacheDirectory string `toml:"cache_dir"`

	// CacheCapacity specifies how many bytes can be stored in CacheDirectory.
	//
	// Unit is GiB.  Default is 1 GiB.
	CacheCapacity int `toml:"cache_capacity"`

	// MaxConns specifies the maximum concurrent connections to an
	// upstream host.
	//
	// Zero disables limit on the number of connections.
	MaxConns int `toml:"max_conns"`

	// Log is well.LogConfig
	Log well.LogConfig `toml:"log"`

	// Mapping specifies mapping between prefixes and APT URLs.
	Mapping map[string]string `toml:"mapping"`
}

Config is a struct to read TOML configurations.

Use https://github.com/BurntSushi/toml as follows:

config := cacher.NewConfig()
md, err := toml.DecodeFile("/path/to/config.toml", config)
if err != nil {
    ...
}

func NewConfig added in v1.2.0

func NewConfig() *Config

NewConfig creates Config with default values.

type Storage

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

Storage stores cache items in local file system.

Cached items will be removed in LRU fashion when the total size of items exceeds the capacity.

func NewStorage

func NewStorage(dir string, capacity uint64) *Storage

NewStorage creates a Storage.

dir is the directory for cached items. capacity is the maximum total size (bytes) of items in the cache. If capacity is zero, items will not be evicted. Non-existing directories will be created (insufficient permission result in panic)

func (*Storage) Delete

func (cm *Storage) Delete(p string) error

Delete deletes an item from the cache.

func (*Storage) Insert

func (cm *Storage) Insert(filename string, fi *apt.FileInfo) error

Insert inserts or updates a cache item.

fi.Path() must be as clean as filepath.Clean() and must not be filepath.IsAbs().

func (*Storage) Len

func (cm *Storage) Len() int

Len implements heap.Interface.

func (*Storage) Less

func (cm *Storage) Less(i, j int) bool

Less implements heap.Interface.

func (*Storage) ListAll

func (cm *Storage) ListAll() []*apt.FileInfo

ListAll returns a list of *apt.FileInfo for all cached items.

func (*Storage) Load

func (cm *Storage) Load() error

Load loads existing items in filesystem.

func (*Storage) Lookup

func (cm *Storage) Lookup(fi *apt.FileInfo) (*os.File, error)

Lookup looks up an item in the cache. If no item matching fi is found, ErrNotFound is returned.

The caller is responsible to close the returned os.File.

func (*Storage) Pop

func (cm *Storage) Pop() interface{}

Pop implements heap.Interface.

func (*Storage) Push

func (cm *Storage) Push(x interface{})

Push implements heap.Interface.

func (*Storage) Swap

func (cm *Storage) Swap(i, j int)

Swap implements heap.Interface.

func (*Storage) TempFile added in v1.4.0

func (cm *Storage) TempFile() (*os.File, error)

TempFile creates a new temporary file in the directory specified in Storage, opens the file for reading and writing, and returns the resulting *os.File.

type URLMap

type URLMap map[string]*url.URL

URLMap is a mapping between prefix and debian repository URL.

To create an instance, use make(URLMap).

func (*URLMap) Register

func (um *URLMap) Register(prefix string, u *url.URL) error

Register registeres a prefix for a remote URL.

func (URLMap) URL

func (um URLMap) URL(p string) *url.URL

URL returns remote URL corresponding to a local path.

Preceding slashes in slash are ignored. For example, if p "/abc/def" is the same as "abc/def".

If p does not starts with a registered prefix, nil is returned.

Jump to

Keyboard shortcuts

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