Skip to content

Commit

Permalink
support hessian2 for triple protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Dec 17, 2023
1 parent 3187f8e commit e93578e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions protocol/triple/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func newClientManager(url *common.URL) (*clientManager, error) {
case constant.ProtobufSerialization:
case constant.JSONSerialization:
cliOpts = append(cliOpts, tri.WithProtoJSON())
case constant.Hessian2Serialization:
cliOpts = append(cliOpts, tri.WithHessian2())
default:
panic(fmt.Sprintf("Unsupported serialization: %s", serialization))
}
Expand Down
1 change: 1 addition & 0 deletions protocol/triple/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *Server) Start(invoker protocol.Invoker, info *server.ServiceInfo) {
switch serialization {
case constant.ProtobufSerialization:
case constant.JSONSerialization:
case constant.Hessian2Serialization:
default:
panic(fmt.Sprintf("Unsupported serialization: %s", serialization))
}
Expand Down
27 changes: 27 additions & 0 deletions protocol/triple/triple_protocol/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"encoding/json"
"errors"
"fmt"
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/dubbogo/grpc-go/encoding/tools"
)

import (
Expand All @@ -32,6 +34,7 @@ import (
const (
codecNameProto = "proto"
codecNameJSON = "json"
codecNameHessian2 = "hessian2"
codecNameJSONCharsetUTF8 = codecNameJSON + "; charset=utf-8"
)

Expand Down Expand Up @@ -173,6 +176,30 @@ func (c *protoJSONCodec) IsBinary() bool {
return false
}

// todo(DMwangnima): add unit tests
type hessian2Codec struct{}

func (h *hessian2Codec) Name() string {
return codecNameHessian2
}

func (h *hessian2Codec) Marshal(message interface{}) ([]byte, error) {
encoder := hessian.NewEncoder()
if err := encoder.Encode(message); err != nil {
return nil, err
}
return encoder.Buffer(), nil
}

func (h *hessian2Codec) Unmarshal(binary []byte, message interface{}) error {
decoder := hessian.NewDecoder(binary)
rawMessage, err := decoder.Decode()
if err != nil {
return err
}
return tools.ReflectResponse(rawMessage, message)
}

// readOnlyCodecs is a read-only interface to a map of named codecs.
type readOnlyCodecs interface {
// Get gets the Codec with the given name.
Expand Down
1 change: 1 addition & 0 deletions protocol/triple/triple_protocol/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func newHandlerConfig(procedure string, options []HandlerOption) *handlerConfig
}
withProtoBinaryCodec().applyToHandler(&config)
withProtoJSONCodecs().applyToHandler(&config)
withHessian2Codec().applyToHandler(&config)
withGzip().applyToHandler(&config)
for _, opt := range options {
opt.applyToHandler(&config)
Expand Down
9 changes: 9 additions & 0 deletions protocol/triple/triple_protocol/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func WithProtoJSON() ClientOption {
return WithCodec(&protoJSONCodec{codecNameJSON})
}

// todo(DMwangnima): add comment
func WithHessian2() ClientOption {
return WithCodec(&hessian2Codec{})
}

// WithSendCompression configures the client to use the specified algorithm to
// compress request messages. If the algorithm has not been registered using
// [WithAcceptCompression], the client will return errors at runtime.
Expand Down Expand Up @@ -545,3 +550,7 @@ func withProtoJSONCodecs() HandlerOption {
WithCodec(&protoJSONCodec{codecNameJSONCharsetUTF8}),
)
}

func withHessian2Codec() Option {
return WithCodec(&hessian2Codec{})
}

0 comments on commit e93578e

Please sign in to comment.