Documentation
¶
Overview ¶
Package slimfast is a library that allows you to create a service which will resize, serve, and cache images.
For a full guide, visit https://www.github.com/ericflo/slimgfast
Index ¶
Constants ¶
const DEFAULT_CACHE_SIZE_MB = int64(128)
const DEFAULT_IMAGE_SOURCE_NAME = "slimgfast_image_source"
const RESIZED_IMAGE_SOURCE_NAME string = "slimgfast_resized_image_source"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
App ties together the fetcher, the transformers, handles the lifecycle of an image request, and does the actual HTTP serving.
func NewApp ¶
func NewApp( fetcher Fetcher, transformers []Transformer, counterFilename string, numWorkers int, cacheMegabytes int64, maxWidth int, maxHeight int, ) (*App, error)
NewApp returns an App that is initialized and ready to be started.
func (*App) Close ¶
func (app *App) Close()
Close signals to the worker group and size counter goroutines to exit.
type Fetcher ¶
type Fetcher interface {
Fetch(urlPath string, dest groupcache.Sink) error
}
Fetcher is the interface that is used to fetch images from some source, which could be the filesystem, a remote URL, or S3 -- but it could be from anywhere.
type ImageRequest ¶
ImageRequest captures information about which file the user wants to have transformed and served, and what transformations the user would like to make with it.
func ImageRequestFromCacheKey ¶
func ImageRequestFromCacheKey(cacheKey string) (*ImageRequest, error)
ImageRequestFromCacheKey parses a cache key generated by CacheKey and constructs an ImageRequest out of it.
func ImageRequestFromURLString ¶
func ImageRequestFromURLString(rawUrl string) (*ImageRequest, error)
ImageRequestFromURLString parses a URL string and constructs an ImageRequest out of it.
func (*ImageRequest) CacheKey ¶
func (req *ImageRequest) CacheKey() (string, error)
CacheKey generates a cache key that encodes all of the information about this ImageRequest.
func (*ImageRequest) Size ¶
func (req *ImageRequest) Size() (*Size, error)
Size is a convenience function for getting a *Size out of the parsed width and height ImageRequest members.
type ImageSource ¶
type ImageSource struct {
// contains filtered or unexported fields
}
ImageSource is an abstraction over a fetcher which caches intelligently, and serves as the primary internal interface to fetchers.
func NewImageSource ¶
func NewImageSource(fetcher Fetcher) *ImageSource
NewImageSource initializes and returns an *ImageSource with sane default values.
func NewImageSourceCustomCache ¶
func NewImageSourceCustomCache(fetcher Fetcher, cacheName string, cacheMegabytes int64) *ImageSource
NewImageSourceCustomCache initializes and returns an *ImageSource with a custom groupcache name and a custom cache size.
func (*ImageSource) GetImageData ¶
func (src *ImageSource) GetImageData(req *ImageRequest) ([]byte, error)
GetImageData gets the image data the request asked for, either from cache or from the associated Fetcher.
type Job ¶
type Job struct { ImageSource ImageSource ImageRequest ImageRequest Result chan []byte Error chan error }
Job is a single image resize job that can be sent over a channel to a worker.
type Size ¶
Size is a Width and a Height
func SizeFromKey ¶
SizeFromKey takes a string of the form WIDTHxHEIGHT and parses it into a Size struct.
type SizeCounter ¶
type SizeCounter struct {
// contains filtered or unexported fields
}
SizeCounter keeps track of how many times a certain size was requested, and persists this information to disk on a periodic basis.
func NewSizeCounter ¶
func NewSizeCounter(filename string) (*SizeCounter, error)
NewSizeCounter initializes a *SizeCounter struct, loads in, and parses the persisted sizes.
func (*SizeCounter) Close ¶
func (counter *SizeCounter) Close()
Close stops the SizeCounter from doing any more persistence.
func (*SizeCounter) CountSize ¶
func (counter *SizeCounter) CountSize(size *Size)
CountSize notes the size of one request.
func (*SizeCounter) GetAllSizes ¶
func (counter *SizeCounter) GetAllSizes() ([]Size, error)
GetAllSizes gets a list of all the sizes that we've seen.
func (*SizeCounter) GetTopSizesByCount ¶
func (counter *SizeCounter) GetTopSizesByCount(count uint) ([]Size, error)
GetTopSizesByCount gets a list of all the sizes who have been requested at least `count` times.
func (*SizeCounter) Start ¶
func (counter *SizeCounter) Start(every time.Duration)
Start starts the counter persisting its aggregated size stats to disk periodically.
type SizeFile ¶
SizeFile is a struct that is used to serialize the aggregated Size counts to a JSON file.
type Transformer ¶
Transformer is the interface that any image transformation operations must adhere to. By implementing one Transform method which takes the ImageRequest and an image object and returns a new image object, we can achieve any effect we want.
type TransformerResize ¶
type TransformerResize struct{}
TransformerResize is the primary Transformer that will resize images to the proper size.
func (*TransformerResize) Transform ¶
func (t *TransformerResize) Transform(req *ImageRequest, image image.Image) (image.Image, error)
Transform resizes the image as requested.
type WorkerGroup ¶
type WorkerGroup struct { Transformers []Transformer NumWorkers int // contains filtered or unexported fields }
WorkerGroup is a pool of workers, a channel, and a list of Transformers that this pool supports.
func (*WorkerGroup) Resize ¶
func (wg *WorkerGroup) Resize(imageSource *ImageSource, imageRequest *ImageRequest) ([]byte, error)
Resize enqueues one request to be run on a worker, and waits for it to respond.
func (*WorkerGroup) Start ¶
func (wg *WorkerGroup) Start()
Start spawns workers for the worker pool and starts them up.