yarpcgzip

package
v1.73.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package yarpcgzip provides a YARPC binding for GZIP compression.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compressor

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

Compressor represents the gzip compression strategy.

func New

func New(opts ...Option) *Compressor

New returns a GZIP compression strategy, suitable for configuring an outbound dialer.

The compressor needs to be adapted and registered to be compatible with the gRPC compressor system. Since gRPC requires global registration of compressors, you must arrange for the compressor to be registered in your application initialization. The adapter converts an io.Reader into an io.ReadCloser so that reading EOF will implicitly trigger Close, a behavior gRPC-go relies upon to reuse readers.

import (
    "compress/gzip"

    "google.golang.org/grpc/encoding"
    "go.uber.org/yarpc/compressor/grpc"
    "go.uber.org/yarpc/compressor/gzip"
)

var GZIPCompressor = yarpcgzip.New(yarpcgzip.Level(gzip.BestCompression))

func init()
    gz := yarpcgrpccompressor.New(GZIPCompressor)
    encoding.RegisterCompressor(gz)
}

If you are constructing your YARPC clients directly through the API, create a gRPC dialer with the Compressor option.

trans := grpc.NewTransport()
dialer := trans.NewDialer(GZIPCompressor)
peers := roundrobin.New(dialer)
outbound := trans.NewOutbound(peers)

If you are using the YARPC configurator to create YARPC objects using config files, you will also need to register the compressor with your configurator.

configurator := yarpcconfig.New()
configurator.MustRegisterCompressor(GZIPCompressor)

Then, using the compression strategy for outbound requests on a particular client, just set the compressor to gzip.

outbounds:
  theirsecureservice:
    grpc:
      address: ":443"
      tls:
        enabled: true
      compressor: gzip

func (*Compressor) Compress

func (c *Compressor) Compress(w io.Writer) (io.WriteCloser, error)

Compress creates a gzip compressor.

func (*Compressor) Decompress

func (c *Compressor) Decompress(r io.Reader) (io.ReadCloser, error)

Decompress obtains a gzip decompressor.

func (*Compressor) DecompressedSize

func (c *Compressor) DecompressedSize(buf []byte) int

DecompressedSize returns the decompressed size of the given GZIP compressed bytes.

gRPC specifically casts the compressor to a DecompressedSizer to pre-check message length.

Per gRPC-go, on which this is based: https://github.com/grpc/grpc-go/blob/master/encoding/gzip/gzip.go

RFC1952 specifies that the last four bytes "contains the size of the original (uncompressed) input data modulo 2^32." gRPC has a max message size of 2GB so we don't need to worry about wraparound for that transport protocol.

func (*Compressor) Name

func (*Compressor) Name() string

Name is gzip.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is an option argument for the Gzip compressor constructor, New.

func Level

func Level(level int) Option

Level sets the compression level for the compressor.

Jump to

Keyboard shortcuts

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