diff options
Diffstat (limited to 'vendor/google.golang.org/grpc/encoding')
-rw-r--r-- | vendor/google.golang.org/grpc/encoding/encoding.go | 131 | ||||
-rw-r--r-- | vendor/google.golang.org/grpc/encoding/encoding_v2.go | 81 | ||||
-rw-r--r-- | vendor/google.golang.org/grpc/encoding/gzip/gzip.go | 132 | ||||
-rw-r--r-- | vendor/google.golang.org/grpc/encoding/proto/proto.go | 96 |
4 files changed, 0 insertions, 440 deletions
diff --git a/vendor/google.golang.org/grpc/encoding/encoding.go b/vendor/google.golang.org/grpc/encoding/encoding.go deleted file mode 100644 index 11d0ae142..000000000 --- a/vendor/google.golang.org/grpc/encoding/encoding.go +++ /dev/null @@ -1,131 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -// Package encoding defines the interface for the compressor and codec, and -// functions to register and retrieve compressors and codecs. -// -// # Experimental -// -// Notice: This package is EXPERIMENTAL and may be changed or removed in a -// later release. -package encoding - -import ( - "io" - "strings" - - "google.golang.org/grpc/internal/grpcutil" -) - -// Identity specifies the optional encoding for uncompressed streams. -// It is intended for grpc internal use only. -const Identity = "identity" - -// Compressor is used for compressing and decompressing when sending or -// receiving messages. -// -// If a Compressor implements `DecompressedSize(compressedBytes []byte) int`, -// gRPC will invoke it to determine the size of the buffer allocated for the -// result of decompression. A return value of -1 indicates unknown size. -type Compressor interface { - // Compress writes the data written to wc to w after compressing it. If an - // error occurs while initializing the compressor, that error is returned - // instead. - Compress(w io.Writer) (io.WriteCloser, error) - // Decompress reads data from r, decompresses it, and provides the - // uncompressed data via the returned io.Reader. If an error occurs while - // initializing the decompressor, that error is returned instead. - Decompress(r io.Reader) (io.Reader, error) - // Name is the name of the compression codec and is used to set the content - // coding header. The result must be static; the result cannot change - // between calls. - Name() string -} - -var registeredCompressor = make(map[string]Compressor) - -// RegisterCompressor registers the compressor with gRPC by its name. It can -// be activated when sending an RPC via grpc.UseCompressor(). It will be -// automatically accessed when receiving a message based on the content coding -// header. Servers also use it to send a response with the same encoding as -// the request. -// -// NOTE: this function must only be called during initialization time (i.e. in -// an init() function), and is not thread-safe. If multiple Compressors are -// registered with the same name, the one registered last will take effect. -func RegisterCompressor(c Compressor) { - registeredCompressor[c.Name()] = c - if !grpcutil.IsCompressorNameRegistered(c.Name()) { - grpcutil.RegisteredCompressorNames = append(grpcutil.RegisteredCompressorNames, c.Name()) - } -} - -// GetCompressor returns Compressor for the given compressor name. -func GetCompressor(name string) Compressor { - return registeredCompressor[name] -} - -// Codec defines the interface gRPC uses to encode and decode messages. Note -// that implementations of this interface must be thread safe; a Codec's -// methods can be called from concurrent goroutines. -type Codec interface { - // Marshal returns the wire format of v. - Marshal(v any) ([]byte, error) - // Unmarshal parses the wire format into v. - Unmarshal(data []byte, v any) error - // Name returns the name of the Codec implementation. The returned string - // will be used as part of content type in transmission. The result must be - // static; the result cannot change between calls. - Name() string -} - -var registeredCodecs = make(map[string]any) - -// RegisterCodec registers the provided Codec for use with all gRPC clients and -// servers. -// -// The Codec will be stored and looked up by result of its Name() method, which -// should match the content-subtype of the encoding handled by the Codec. This -// is case-insensitive, and is stored and looked up as lowercase. If the -// result of calling Name() is an empty string, RegisterCodec will panic. See -// Content-Type on -// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for -// more details. -// -// NOTE: this function must only be called during initialization time (i.e. in -// an init() function), and is not thread-safe. If multiple Codecs are -// registered with the same name, the one registered last will take effect. -func RegisterCodec(codec Codec) { - if codec == nil { - panic("cannot register a nil Codec") - } - if codec.Name() == "" { - panic("cannot register Codec with empty string result for Name()") - } - contentSubtype := strings.ToLower(codec.Name()) - registeredCodecs[contentSubtype] = codec -} - -// GetCodec gets a registered Codec by content-subtype, or nil if no Codec is -// registered for the content-subtype. -// -// The content-subtype is expected to be lowercase. -func GetCodec(contentSubtype string) Codec { - c, _ := registeredCodecs[contentSubtype].(Codec) - return c -} diff --git a/vendor/google.golang.org/grpc/encoding/encoding_v2.go b/vendor/google.golang.org/grpc/encoding/encoding_v2.go deleted file mode 100644 index 074c5e234..000000000 --- a/vendor/google.golang.org/grpc/encoding/encoding_v2.go +++ /dev/null @@ -1,81 +0,0 @@ -/* - * - * Copyright 2024 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package encoding - -import ( - "strings" - - "google.golang.org/grpc/mem" -) - -// CodecV2 defines the interface gRPC uses to encode and decode messages. Note -// that implementations of this interface must be thread safe; a CodecV2's -// methods can be called from concurrent goroutines. -type CodecV2 interface { - // Marshal returns the wire format of v. The buffers in the returned - // [mem.BufferSlice] must have at least one reference each, which will be freed - // by gRPC when they are no longer needed. - Marshal(v any) (out mem.BufferSlice, err error) - // Unmarshal parses the wire format into v. Note that data will be freed as soon - // as this function returns. If the codec wishes to guarantee access to the data - // after this function, it must take its own reference that it frees when it is - // no longer needed. - Unmarshal(data mem.BufferSlice, v any) error - // Name returns the name of the Codec implementation. The returned string - // will be used as part of content type in transmission. The result must be - // static; the result cannot change between calls. - Name() string -} - -// RegisterCodecV2 registers the provided CodecV2 for use with all gRPC clients and -// servers. -// -// The CodecV2 will be stored and looked up by result of its Name() method, which -// should match the content-subtype of the encoding handled by the CodecV2. This -// is case-insensitive, and is stored and looked up as lowercase. If the -// result of calling Name() is an empty string, RegisterCodecV2 will panic. See -// Content-Type on -// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for -// more details. -// -// If both a Codec and CodecV2 are registered with the same name, the CodecV2 -// will be used. -// -// NOTE: this function must only be called during initialization time (i.e. in -// an init() function), and is not thread-safe. If multiple Codecs are -// registered with the same name, the one registered last will take effect. -func RegisterCodecV2(codec CodecV2) { - if codec == nil { - panic("cannot register a nil CodecV2") - } - if codec.Name() == "" { - panic("cannot register CodecV2 with empty string result for Name()") - } - contentSubtype := strings.ToLower(codec.Name()) - registeredCodecs[contentSubtype] = codec -} - -// GetCodecV2 gets a registered CodecV2 by content-subtype, or nil if no CodecV2 is -// registered for the content-subtype. -// -// The content-subtype is expected to be lowercase. -func GetCodecV2(contentSubtype string) CodecV2 { - c, _ := registeredCodecs[contentSubtype].(CodecV2) - return c -} diff --git a/vendor/google.golang.org/grpc/encoding/gzip/gzip.go b/vendor/google.golang.org/grpc/encoding/gzip/gzip.go deleted file mode 100644 index 6306e8bb0..000000000 --- a/vendor/google.golang.org/grpc/encoding/gzip/gzip.go +++ /dev/null @@ -1,132 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -// Package gzip implements and registers the gzip compressor -// during the initialization. -// -// # Experimental -// -// Notice: This package is EXPERIMENTAL and may be changed or removed in a -// later release. -package gzip - -import ( - "compress/gzip" - "encoding/binary" - "fmt" - "io" - "sync" - - "google.golang.org/grpc/encoding" -) - -// Name is the name registered for the gzip compressor. -const Name = "gzip" - -func init() { - c := &compressor{} - c.poolCompressor.New = func() any { - return &writer{Writer: gzip.NewWriter(io.Discard), pool: &c.poolCompressor} - } - encoding.RegisterCompressor(c) -} - -type writer struct { - *gzip.Writer - pool *sync.Pool -} - -// SetLevel updates the registered gzip compressor to use the compression level specified (gzip.HuffmanOnly is not supported). -// NOTE: this function must only be called during initialization time (i.e. in an init() function), -// and is not thread-safe. -// -// The error returned will be nil if the specified level is valid. -func SetLevel(level int) error { - if level < gzip.DefaultCompression || level > gzip.BestCompression { - return fmt.Errorf("grpc: invalid gzip compression level: %d", level) - } - c := encoding.GetCompressor(Name).(*compressor) - c.poolCompressor.New = func() any { - w, err := gzip.NewWriterLevel(io.Discard, level) - if err != nil { - panic(err) - } - return &writer{Writer: w, pool: &c.poolCompressor} - } - return nil -} - -func (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) { - z := c.poolCompressor.Get().(*writer) - z.Writer.Reset(w) - return z, nil -} - -func (z *writer) Close() error { - defer z.pool.Put(z) - return z.Writer.Close() -} - -type reader struct { - *gzip.Reader - pool *sync.Pool -} - -func (c *compressor) Decompress(r io.Reader) (io.Reader, error) { - z, inPool := c.poolDecompressor.Get().(*reader) - if !inPool { - newZ, err := gzip.NewReader(r) - if err != nil { - return nil, err - } - return &reader{Reader: newZ, pool: &c.poolDecompressor}, nil - } - if err := z.Reset(r); err != nil { - c.poolDecompressor.Put(z) - return nil, err - } - return z, nil -} - -func (z *reader) Read(p []byte) (n int, err error) { - n, err = z.Reader.Read(p) - if err == io.EOF { - z.pool.Put(z) - } - return n, err -} - -// 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. -func (c *compressor) DecompressedSize(buf []byte) int { - last := len(buf) - if last < 4 { - return -1 - } - return int(binary.LittleEndian.Uint32(buf[last-4 : last])) -} - -func (c *compressor) Name() string { - return Name -} - -type compressor struct { - poolCompressor sync.Pool - poolDecompressor sync.Pool -} diff --git a/vendor/google.golang.org/grpc/encoding/proto/proto.go b/vendor/google.golang.org/grpc/encoding/proto/proto.go deleted file mode 100644 index ceec319dd..000000000 --- a/vendor/google.golang.org/grpc/encoding/proto/proto.go +++ /dev/null @@ -1,96 +0,0 @@ -/* - * - * Copyright 2024 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -// Package proto defines the protobuf codec. Importing this package will -// register the codec. -package proto - -import ( - "fmt" - - "google.golang.org/grpc/encoding" - "google.golang.org/grpc/mem" - "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/protoadapt" -) - -// Name is the name registered for the proto compressor. -const Name = "proto" - -func init() { - encoding.RegisterCodecV2(&codecV2{}) -} - -// codec is a CodecV2 implementation with protobuf. It is the default codec for -// gRPC. -type codecV2 struct{} - -func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) { - vv := messageV2Of(v) - if vv == nil { - return nil, fmt.Errorf("proto: failed to marshal, message is %T, want proto.Message", v) - } - - size := proto.Size(vv) - if mem.IsBelowBufferPoolingThreshold(size) { - buf, err := proto.Marshal(vv) - if err != nil { - return nil, err - } - data = append(data, mem.SliceBuffer(buf)) - } else { - pool := mem.DefaultBufferPool() - buf := pool.Get(size) - if _, err := (proto.MarshalOptions{}).MarshalAppend((*buf)[:0], vv); err != nil { - pool.Put(buf) - return nil, err - } - data = append(data, mem.NewBuffer(buf, pool)) - } - - return data, nil -} - -func (c *codecV2) Unmarshal(data mem.BufferSlice, v any) (err error) { - vv := messageV2Of(v) - if vv == nil { - return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v) - } - - buf := data.MaterializeToBuffer(mem.DefaultBufferPool()) - defer buf.Free() - // TODO: Upgrade proto.Unmarshal to support mem.BufferSlice. Right now, it's not - // really possible without a major overhaul of the proto package, but the - // vtprotobuf library may be able to support this. - return proto.Unmarshal(buf.ReadOnlyData(), vv) -} - -func messageV2Of(v any) proto.Message { - switch v := v.(type) { - case protoadapt.MessageV1: - return protoadapt.MessageV2Of(v) - case protoadapt.MessageV2: - return v - } - - return nil -} - -func (c *codecV2) Name() string { - return Name -} |