* * Copyright 2018 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 (
	
	

	
	
)
Name is the name registered for the proto compressor.
const Name = "proto"

func () {
	encoding.RegisterCodec(codec{})
}
codec is a Codec implementation with protobuf. It is the default codec for gRPC.
type codec struct{}

type cachedProtoBuffer struct {
	lastMarshaledSize uint32
	proto.Buffer
}

func ( int) uint32 {
	if  > math.MaxInt32 {
		return uint32(math.MaxInt32)
	}
	return uint32()
}

func ( interface{},  *cachedProtoBuffer) ([]byte, error) {
	 := .(proto.Message)
	 := make([]byte, 0, .lastMarshaledSize)

	.SetBuf()
	.Reset()
	if  := .Marshal();  != nil {
		return nil, 
	}
	 := .Bytes()
	.lastMarshaledSize = capToMaxInt32(len())
	return , nil
}

func (codec) ( interface{}) ([]byte, error) {
object can marshal itself, no need for buffer
		return .Marshal()
	}

	 := protoBufferPool.Get().(*cachedProtoBuffer)
	,  := marshal(, )
put back buffer and lose the ref to the slice
	.SetBuf(nil)
	protoBufferPool.Put()
	return , 
}

func (codec) ( []byte,  interface{}) error {
	 := .(proto.Message)
	.Reset()

object can unmarshal itself, no need for buffer
		return .Unmarshal()
	}

	 := protoBufferPool.Get().(*cachedProtoBuffer)
	.SetBuf()
	 := .Unmarshal()
	.SetBuf(nil)
	protoBufferPool.Put()
	return 
}

func (codec) () string {
	return Name
}

var protoBufferPool = &sync.Pool{
	New: func() interface{} {
		return &cachedProtoBuffer{
			Buffer:            proto.Buffer{},
			lastMarshaledSize: 16,
		}
	},