make Codec configurable when creating grpc.ClientConn and grpc.Server
This commit is contained in:
14
server.go
14
server.go
@ -85,12 +85,19 @@ type Server struct {
|
||||
}
|
||||
|
||||
type options struct {
|
||||
codec Codec
|
||||
maxConcurrentStreams uint32
|
||||
}
|
||||
|
||||
// A ServerOption sets options.
|
||||
type ServerOption func(*options)
|
||||
|
||||
func CustomCodec(codec Codec) ServerOption {
|
||||
return func(o *options) {
|
||||
o.codec = codec
|
||||
}
|
||||
}
|
||||
|
||||
// MaxConcurrentStreams returns an Option that will apply a limit on the number
|
||||
// of concurrent streams to each ServerTransport.
|
||||
func MaxConcurrentStreams(n uint32) ServerOption {
|
||||
@ -106,6 +113,10 @@ func NewServer(opt ...ServerOption) *Server {
|
||||
for _, o := range opt {
|
||||
o(&opts)
|
||||
}
|
||||
if opts.codec == nil {
|
||||
// Set the default codec.
|
||||
opts.codec = &protoCodec{}
|
||||
}
|
||||
return &Server{
|
||||
lis: make(map[net.Listener]bool),
|
||||
opts: opts,
|
||||
@ -203,7 +214,7 @@ func (s *Server) Serve(lis net.Listener) error {
|
||||
}
|
||||
|
||||
func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Stream, msg interface{}, pf payloadFormat, opts *transport.Options) error {
|
||||
p, err := encode(protoCodec{}, msg, pf)
|
||||
p, err := encode(s.opts.codec, msg, pf)
|
||||
if err != nil {
|
||||
// This typically indicates a fatal issue (e.g., memory
|
||||
// corruption or hardware faults) the application program
|
||||
@ -286,6 +297,7 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp
|
||||
t: t,
|
||||
s: stream,
|
||||
p: &parser{s: stream},
|
||||
codec: s.opts.codec,
|
||||
}
|
||||
if appErr := sd.Handler(srv.server, ss); appErr != nil {
|
||||
if err, ok := appErr.(rpcError); ok {
|
||||
|
Reference in New Issue
Block a user