mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-10-19 03:41:46 +00:00
add grpc-proxy (github.com/mwitkow/grpc-proxy)
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
69
proxy/codec.go
Normal file
69
proxy/codec.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// Codec returns a proxying grpc.Codec with the default protobuf codec as parent.
|
||||
//
|
||||
// See CodecWithParent.
|
||||
//
|
||||
// Deprecated: No longer necessary.
|
||||
func Codec() grpc.Codec {
|
||||
return CodecWithParent(&protoCodec{})
|
||||
}
|
||||
|
||||
// CodecWithParent returns a proxying grpc.Codec with a user provided codec as parent.
|
||||
//
|
||||
// Deprecated: No longer necessary.
|
||||
func CodecWithParent(fallback grpc.Codec) grpc.Codec {
|
||||
return &rawCodec{fallback}
|
||||
}
|
||||
|
||||
type rawCodec struct {
|
||||
parentCodec grpc.Codec
|
||||
}
|
||||
|
||||
type frame struct {
|
||||
payload []byte
|
||||
}
|
||||
|
||||
func (c *rawCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
out, ok := v.(*frame)
|
||||
if !ok {
|
||||
return c.parentCodec.Marshal(v)
|
||||
}
|
||||
return out.payload, nil
|
||||
|
||||
}
|
||||
|
||||
func (c *rawCodec) Unmarshal(data []byte, v interface{}) error {
|
||||
dst, ok := v.(*frame)
|
||||
if !ok {
|
||||
return c.parentCodec.Unmarshal(data, v)
|
||||
}
|
||||
dst.payload = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *rawCodec) String() string {
|
||||
return fmt.Sprintf("proxy>%s", c.parentCodec.String())
|
||||
}
|
||||
|
||||
// protoCodec is a Codec implementation with protobuf. It is the default rawCodec for gRPC.
|
||||
type protoCodec struct{}
|
||||
|
||||
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
return proto.Marshal(v.(proto.Message))
|
||||
}
|
||||
|
||||
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
||||
return proto.Unmarshal(data, v.(proto.Message))
|
||||
}
|
||||
|
||||
func (protoCodec) String() string {
|
||||
return "proto"
|
||||
}
|
Reference in New Issue
Block a user