codec.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package grpc
  34. import (
  35. "math"
  36. "sync"
  37. "github.com/golang/protobuf/proto"
  38. )
  39. // Codec defines the interface gRPC uses to encode and decode messages.
  40. // Note that implementations of this interface must be thread safe;
  41. // a Codec's methods can be called from concurrent goroutines.
  42. type Codec interface {
  43. // Marshal returns the wire format of v.
  44. Marshal(v interface{}) ([]byte, error)
  45. // Unmarshal parses the wire format into v.
  46. Unmarshal(data []byte, v interface{}) error
  47. // String returns the name of the Codec implementation. The returned
  48. // string will be used as part of content type in transmission.
  49. String() string
  50. }
  51. // protoCodec is a Codec implementation with protobuf. It is the default codec for gRPC.
  52. type protoCodec struct {
  53. }
  54. type cachedProtoBuffer struct {
  55. lastMarshaledSize uint32
  56. proto.Buffer
  57. }
  58. func capToMaxInt32(val int) uint32 {
  59. if val > math.MaxInt32 {
  60. return uint32(math.MaxInt32)
  61. }
  62. return uint32(val)
  63. }
  64. func (p protoCodec) marshal(v interface{}, cb *cachedProtoBuffer) ([]byte, error) {
  65. protoMsg := v.(proto.Message)
  66. newSlice := make([]byte, 0, cb.lastMarshaledSize)
  67. cb.SetBuf(newSlice)
  68. cb.Reset()
  69. if err := cb.Marshal(protoMsg); err != nil {
  70. return nil, err
  71. }
  72. out := cb.Bytes()
  73. cb.lastMarshaledSize = capToMaxInt32(len(out))
  74. return out, nil
  75. }
  76. func (p protoCodec) Marshal(v interface{}) ([]byte, error) {
  77. cb := protoBufferPool.Get().(*cachedProtoBuffer)
  78. out, err := p.marshal(v, cb)
  79. // put back buffer and lose the ref to the slice
  80. cb.SetBuf(nil)
  81. protoBufferPool.Put(cb)
  82. return out, err
  83. }
  84. func (p protoCodec) Unmarshal(data []byte, v interface{}) error {
  85. cb := protoBufferPool.Get().(*cachedProtoBuffer)
  86. cb.SetBuf(data)
  87. v.(proto.Message).Reset()
  88. err := cb.Unmarshal(v.(proto.Message))
  89. cb.SetBuf(nil)
  90. protoBufferPool.Put(cb)
  91. return err
  92. }
  93. func (protoCodec) String() string {
  94. return "proto"
  95. }
  96. var (
  97. protoBufferPool = &sync.Pool{
  98. New: func() interface{} {
  99. return &cachedProtoBuffer{
  100. Buffer: proto.Buffer{},
  101. lastMarshaledSize: 16,
  102. }
  103. },
  104. }
  105. )