codec.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "math"
  21. "sync"
  22. "github.com/golang/protobuf/proto"
  23. )
  24. // Codec defines the interface gRPC uses to encode and decode messages.
  25. // Note that implementations of this interface must be thread safe;
  26. // a Codec's methods can be called from concurrent goroutines.
  27. type Codec interface {
  28. // Marshal returns the wire format of v.
  29. Marshal(v interface{}) ([]byte, error)
  30. // Unmarshal parses the wire format into v.
  31. Unmarshal(data []byte, v interface{}) error
  32. // String returns the name of the Codec implementation. The returned
  33. // string will be used as part of content type in transmission.
  34. String() string
  35. }
  36. // protoCodec is a Codec implementation with protobuf. It is the default codec for gRPC.
  37. type protoCodec struct {
  38. }
  39. type cachedProtoBuffer struct {
  40. lastMarshaledSize uint32
  41. proto.Buffer
  42. }
  43. func capToMaxInt32(val int) uint32 {
  44. if val > math.MaxInt32 {
  45. return uint32(math.MaxInt32)
  46. }
  47. return uint32(val)
  48. }
  49. func (p protoCodec) marshal(v interface{}, cb *cachedProtoBuffer) ([]byte, error) {
  50. protoMsg := v.(proto.Message)
  51. newSlice := make([]byte, 0, cb.lastMarshaledSize)
  52. cb.SetBuf(newSlice)
  53. cb.Reset()
  54. if err := cb.Marshal(protoMsg); err != nil {
  55. return nil, err
  56. }
  57. out := cb.Bytes()
  58. cb.lastMarshaledSize = capToMaxInt32(len(out))
  59. return out, nil
  60. }
  61. func (p protoCodec) Marshal(v interface{}) ([]byte, error) {
  62. cb := protoBufferPool.Get().(*cachedProtoBuffer)
  63. out, err := p.marshal(v, cb)
  64. // put back buffer and lose the ref to the slice
  65. cb.SetBuf(nil)
  66. protoBufferPool.Put(cb)
  67. return out, err
  68. }
  69. func (p protoCodec) Unmarshal(data []byte, v interface{}) error {
  70. cb := protoBufferPool.Get().(*cachedProtoBuffer)
  71. cb.SetBuf(data)
  72. v.(proto.Message).Reset()
  73. err := cb.Unmarshal(v.(proto.Message))
  74. cb.SetBuf(nil)
  75. protoBufferPool.Put(cb)
  76. return err
  77. }
  78. func (protoCodec) String() string {
  79. return "proto"
  80. }
  81. var (
  82. protoBufferPool = &sync.Pool{
  83. New: func() interface{} {
  84. return &cachedProtoBuffer{
  85. Buffer: proto.Buffer{},
  86. lastMarshaledSize: 16,
  87. }
  88. },
  89. }
  90. )