image_service

package module
v0.0.0-...-a2990ee Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2017 License: MIT Imports: 5 Imported by: 10

README

ImageService

Build Status

An gRPC based parallel image processing and storage microservice. Image Service takes an original image and a set of 'operations' to process and streams the results as they're complete back to the client. Optional no operations can be requested and the original is stored using the storage provided for later use with external services (imgix.com etc).

Processing is done by a set of 'workers' to limit concurrency and overloading.

By default 5 workers are run, meaning only 5 image resizing or uploading operations happen concurrently. New operations are queued and streamed back to the client when completed.

A "sync" method is there for convenience, it's worth nothing that this method still uploads and processes concurrently but returns all the images at once when they're all complete.

Image processing uses bimg which is backed by libvips.

service ImageService {
  rpc Store(ImageStoreRequest) returns (stream Image) {}
  rpc StoreSync(ImageStoreRequest) returns (ImageSyncResponse) {}
  rpc Delete(DeleteRequest) returns (DeleteResponse) {}
}

Docker

A pre build Docker container is available at:

docker pull lileio/image_service

Setup

Configuration of storage is done via ENV variables

Worker Pool Size

To increase or decrease the number of parallel workers, set the WORKER_POOL_SIZE var.

WORKER_POOL_SIZE=10
Cloud Storage

To point to a cloud storage service instance.

CLOUD_STORAGE_ADDR=10.0.0.1:8000
File Storage

File system storage is also available, note that you will be responsible for hosting/serving behind something like nginx or a CDN.

FILE_LOCATION=/path/to/webserver

Example


// Setup a client connection
client = image_service.NewImageServiceClient(conn)

// Create a request object
b, err := ioutil.ReadFile("pic.jpg")
if err != nil {
  panic(err) 
}

ctx := context.Background()
req := &image_service.ImageStoreRequest{
  Filename: "pic.jpg",
  Data:     b,
  Ops: []*image_service.ImageOperation{
    &image_service.ImageOperation{
      Crop:        true,
      Width:       200,
      Height:      200,
      VersionName: "small",
    },
    &image_service.ImageOperation{
      Crop:        true,
      Width:       600,
      Height:      600,
      VersionName: "medium",
    },
    &image_service.ImageOperation{
      Crop:        true,
      Width:       1000,
      Height:      1000,
      VersionName: "large",
    },
  },
}


stream, err := client.Store(ctx, req)

for {
  img, err := stream.Recv()
  if err == io.EOF {
    break
  }

  if err != nil {
    fmt.Printf("err = %+v\n", err)
  }

  fmt.Printf("img = %+v\n", img.URL)
}

Documentation

Overview

Package image_service is a generated protocol buffer package.

It is generated from these files:

image_service.proto

It has these top-level messages:

Image
ImageOperation
ImageStoreRequest
ImageSyncResponse
DeleteRequest
DeleteResponse

Index

Constants

This section is empty.

Variables

View Source
var DefaultOps = []*ImageOperation{
	{Quality: 90, Crop: true, Width: 200, Height: 200, VersionName: "small"},
	{Quality: 90, Crop: true, Width: 600, Height: 600, VersionName: "medium"},
	{Quality: 90, Crop: true, Width: 1200, Height: 1200, VersionName: "large"},
}
View Source
var Format_name = map[int32]string{
	0: "JPEG",
	1: "WEBP",
	2: "PNG",
}
View Source
var Format_value = map[string]int32{
	"JPEG": 0,
	"WEBP": 1,
	"PNG":  2,
}

Functions

func RegisterImageServiceServer

func RegisterImageServiceServer(s *grpc.Server, srv ImageServiceServer)

Types

type DeleteRequest

type DeleteRequest struct {
	Filename string `protobuf:"bytes,1,opt,name=filename" json:"filename,omitempty"`
}

func (*DeleteRequest) Descriptor

func (*DeleteRequest) Descriptor() ([]byte, []int)

func (*DeleteRequest) GetFilename

func (m *DeleteRequest) GetFilename() string

func (*DeleteRequest) ProtoMessage

func (*DeleteRequest) ProtoMessage()

func (*DeleteRequest) Reset

func (m *DeleteRequest) Reset()

func (*DeleteRequest) String

func (m *DeleteRequest) String() string

type DeleteResponse

type DeleteResponse struct {
	Filename string `protobuf:"bytes,1,opt,name=filename" json:"filename,omitempty"`
}

func (*DeleteResponse) Descriptor

func (*DeleteResponse) Descriptor() ([]byte, []int)

func (*DeleteResponse) GetFilename

func (m *DeleteResponse) GetFilename() string

func (*DeleteResponse) ProtoMessage

func (*DeleteResponse) ProtoMessage()

func (*DeleteResponse) Reset

func (m *DeleteResponse) Reset()

func (*DeleteResponse) String

func (m *DeleteResponse) String() string

type Format

type Format int32
const (
	Format_JPEG Format = 0
	Format_WEBP Format = 1
	Format_PNG  Format = 2
)

func (Format) EnumDescriptor

func (Format) EnumDescriptor() ([]byte, []int)

func (Format) String

func (x Format) String() string

type Image

type Image struct {
	VersionName string `protobuf:"bytes,1,opt,name=version_name,json=versionName" json:"version_name,omitempty"`
	Filename    string `protobuf:"bytes,2,opt,name=filename" json:"filename,omitempty"`
	Url         string `protobuf:"bytes,3,opt,name=url" json:"url,omitempty"`
}

func (*Image) Descriptor

func (*Image) Descriptor() ([]byte, []int)

func (*Image) GetFilename

func (m *Image) GetFilename() string

func (*Image) GetUrl

func (m *Image) GetUrl() string

func (*Image) GetVersionName

func (m *Image) GetVersionName() string

func (*Image) ProtoMessage

func (*Image) ProtoMessage()

func (*Image) Reset

func (m *Image) Reset()

func (*Image) String

func (m *Image) String() string

type ImageOperation

type ImageOperation struct {
	VersionName string `protobuf:"bytes,1,opt,name=version_name,json=versionName" json:"version_name,omitempty"`
	Height      int32  `protobuf:"varint,2,opt,name=height" json:"height,omitempty"`
	Width       int32  `protobuf:"varint,3,opt,name=width" json:"width,omitempty"`
	Quality     int32  `protobuf:"varint,4,opt,name=quality" json:"quality,omitempty"`
	Compression int32  `protobuf:"varint,5,opt,name=compression" json:"compression,omitempty"`
	Crop        bool   `protobuf:"varint,6,opt,name=crop" json:"crop,omitempty"`
	Enlarge     bool   `protobuf:"varint,7,opt,name=enlarge" json:"enlarge,omitempty"`
	Flip        bool   `protobuf:"varint,8,opt,name=flip" json:"flip,omitempty"`
	Interlace   bool   `protobuf:"varint,9,opt,name=interlace" json:"interlace,omitempty"`
	Format      Format `protobuf:"varint,10,opt,name=format,enum=image_service.Format" json:"format,omitempty"`
}

func (*ImageOperation) Descriptor

func (*ImageOperation) Descriptor() ([]byte, []int)

func (*ImageOperation) GetCompression

func (m *ImageOperation) GetCompression() int32

func (*ImageOperation) GetCrop

func (m *ImageOperation) GetCrop() bool

func (*ImageOperation) GetEnlarge

func (m *ImageOperation) GetEnlarge() bool

func (*ImageOperation) GetFlip

func (m *ImageOperation) GetFlip() bool

func (*ImageOperation) GetFormat

func (m *ImageOperation) GetFormat() Format

func (*ImageOperation) GetHeight

func (m *ImageOperation) GetHeight() int32

func (*ImageOperation) GetInterlace

func (m *ImageOperation) GetInterlace() bool

func (*ImageOperation) GetQuality

func (m *ImageOperation) GetQuality() int32

func (*ImageOperation) GetVersionName

func (m *ImageOperation) GetVersionName() string

func (*ImageOperation) GetWidth

func (m *ImageOperation) GetWidth() int32

func (*ImageOperation) ProtoMessage

func (*ImageOperation) ProtoMessage()

func (*ImageOperation) Reset

func (m *ImageOperation) Reset()

func (*ImageOperation) String

func (m *ImageOperation) String() string

type ImageServiceClient

type ImageServiceClient interface {
	Store(ctx context.Context, in *ImageStoreRequest, opts ...grpc.CallOption) (ImageService_StoreClient, error)
	StoreSync(ctx context.Context, in *ImageStoreRequest, opts ...grpc.CallOption) (*ImageSyncResponse, error)
	Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*DeleteResponse, error)
}

func NewImageServiceClient

func NewImageServiceClient(cc *grpc.ClientConn) ImageServiceClient

type ImageService_StoreClient

type ImageService_StoreClient interface {
	Recv() (*Image, error)
	grpc.ClientStream
}

type ImageService_StoreServer

type ImageService_StoreServer interface {
	Send(*Image) error
	grpc.ServerStream
}

type ImageStoreRequest

type ImageStoreRequest struct {
	Filename string            `protobuf:"bytes,1,opt,name=filename" json:"filename,omitempty"`
	Data     []byte            `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
	Ops      []*ImageOperation `protobuf:"bytes,3,rep,name=ops" json:"ops,omitempty"`
}

func (*ImageStoreRequest) Descriptor

func (*ImageStoreRequest) Descriptor() ([]byte, []int)

func (*ImageStoreRequest) GetData

func (m *ImageStoreRequest) GetData() []byte

func (*ImageStoreRequest) GetFilename

func (m *ImageStoreRequest) GetFilename() string

func (*ImageStoreRequest) GetOps

func (m *ImageStoreRequest) GetOps() []*ImageOperation

func (*ImageStoreRequest) ProtoMessage

func (*ImageStoreRequest) ProtoMessage()

func (*ImageStoreRequest) Reset

func (m *ImageStoreRequest) Reset()

func (*ImageStoreRequest) String

func (m *ImageStoreRequest) String() string

type ImageSyncResponse

type ImageSyncResponse struct {
	Images []*Image `protobuf:"bytes,1,rep,name=images" json:"images,omitempty"`
}

func (*ImageSyncResponse) Descriptor

func (*ImageSyncResponse) Descriptor() ([]byte, []int)

func (*ImageSyncResponse) GetImages

func (m *ImageSyncResponse) GetImages() []*Image

func (*ImageSyncResponse) ProtoMessage

func (*ImageSyncResponse) ProtoMessage()

func (*ImageSyncResponse) Reset

func (m *ImageSyncResponse) Reset()

func (*ImageSyncResponse) String

func (m *ImageSyncResponse) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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