Skip to content

Commit

Permalink
fix(codec): support transinfo in attachment codec
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderPoet committed Mar 17, 2024
1 parent e45a62c commit 575b15f
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 1,191 deletions.
14 changes: 14 additions & 0 deletions pkg/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (m *DubboCodec) encodeRequestPayload(ctx context.Context, message remote.Me
Method: message.RPCInfo().Invocation().MethodName(),
Timeout: message.RPCInfo().Config().RPCTimeout(),
Group: message.RPCInfo().To().DefaultTag(registries.DubboServiceGroupKey, ""),
TransInfo: message.TransInfo(),
}
methodAnno := m.getMethodAnnotation(message)

Expand Down Expand Up @@ -272,6 +273,7 @@ func (m *DubboCodec) messageAttachment(ctx context.Context, service *dubbo_spec.
service.Path,
service.Version,
service.Timeout,
service.TransInfo,
)
return e.Encode(attachment)
}
Expand Down Expand Up @@ -450,11 +452,23 @@ func processAttachments(decoder iface.Decoder, message remote.Message) error {
}

if attachments, ok := attachmentsRaw.(map[interface{}]interface{}); ok {
transStrMap := map[string]string{}
transIntMap := map[uint16]string{}
for keyRaw, val := range attachments {
if key, ok := keyRaw.(string); ok {
message.Tags()[key] = val
if v, ok := val.(string); ok {
transStrMap[key] = v
}
}
if uint16Key, ok := keyRaw.(uint16); ok {
if v, ok := val.(string); ok {
transIntMap[uint16Key] = v
}
}
}
message.TransInfo().PutTransStrInfo(transStrMap)
message.TransInfo().PutTransIntInfo(transIntMap)
return nil
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/dubbo_spec/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package dubbo_spec
import (
"strconv"
"time"

"github.com/cloudwego/kitex/pkg/remote"
)

const (
Expand All @@ -32,9 +34,9 @@ const (
TIMEOUT_KEY = "timeout"
)

type Attachment = map[string]interface{}
type Attachment = map[interface{}]interface{}

func NewAttachment(path, group, iface, version string, timeout time.Duration) Attachment {
func NewAttachment(path, group, iface, version string, timeout time.Duration, transInfo remote.TransInfo) Attachment {
result := Attachment{}
if len(path) > 0 {
result[PATH_KEY] = path
Expand All @@ -51,5 +53,11 @@ func NewAttachment(path, group, iface, version string, timeout time.Duration) At
if timeout > 0 {
result[TIMEOUT_KEY] = strconv.Itoa(int(timeout.Milliseconds()))
}
for k, v := range transInfo.TransIntInfo() {
result[k] = v
}
for k, v := range transInfo.TransStrInfo() {
result[k] = v
}
return result
}
3 changes: 2 additions & 1 deletion pkg/dubbo_spec/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"time"

"github.com/cloudwego/kitex/pkg/remote"
"github.com/kitex-contrib/codec-dubbo/pkg/iface"
)

Expand All @@ -35,6 +36,7 @@ type Service struct {
Method string
Timeout time.Duration
Group string
TransInfo remote.TransInfo
}

func (svc *Service) Decode(decoder iface.Decoder) error {
Expand All @@ -50,7 +52,6 @@ func (svc *Service) Decode(decoder iface.Decoder) error {
if err := decodeString(decoder, &svc.Method, "Method"); err != nil {
return err
}

return nil
}

Expand Down
13 changes: 12 additions & 1 deletion samples/helloworld/kitex/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ import (
dubbo "github.com/kitex-contrib/codec-dubbo/pkg"
"github.com/kitex-contrib/codec-dubbo/samples/helloworld/kitex/kitex_gen/hello"
"github.com/kitex-contrib/codec-dubbo/samples/helloworld/kitex/kitex_gen/hello/greetservice"
"github.com/kitex-contrib/obs-opentelemetry/provider"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
)

func main() {
serviceName := "helloworld-client"
p := provider.NewOpenTelemetryProvider(
provider.WithServiceName(serviceName),
provider.WithExportEndpoint("localhost:4317"),
provider.WithInsecure(),
)
defer p.Shutdown(context.Background())

cli, err := greetservice.NewClient("helloworld",
client.WithHostPorts("127.0.0.1:21001"),
client.WithHostPorts("127.0.0.1:21000"),
client.WithCodec(
dubbo.NewDubboCodec(
dubbo.WithJavaClassName("org.cloudwego.kitex.samples.api.GreetProvider"),
),
),
client.WithSuite(tracing.NewClientSuite()),
)
if err != nil {
panic(err)
Expand Down
60 changes: 42 additions & 18 deletions samples/helloworld/kitex/go.mod
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
module github.com/kitex-contrib/codec-dubbo/samples/helloworld/kitex

go 1.20
go 1.21

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

replace github.com/kitex-contrib/codec-dubbo => ../../../

require (
github.com/apache/thrift v0.13.0
github.com/cloudwego/kitex v0.7.2
github.com/apache/thrift v0.16.0
github.com/cloudwego/kitex v0.9.0
github.com/kitex-contrib/codec-dubbo v0.0.0-20231009160704-aad6a2705290
github.com/kitex-contrib/obs-opentelemetry v0.2.6
github.com/pkg/errors v0.9.1
)

require (
github.com/apache/dubbo-go-hessian2 v1.12.2 // indirect
github.com/apache/dubbo-go-hessian2 v1.12.4 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/choleraehyq/pid v0.0.17 // indirect
github.com/bytedance/sonic v1.11.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/choleraehyq/pid v0.0.18 // indirect
github.com/cloudwego/configmanager v0.2.0 // indirect
github.com/cloudwego/dynamicgo v0.1.3 // indirect
github.com/cloudwego/dynamicgo v0.2.0 // indirect
github.com/cloudwego/fastpb v0.0.4 // indirect
github.com/cloudwego/frugal v0.1.8 // indirect
github.com/cloudwego/frugal v0.1.14 // indirect
github.com/cloudwego/localsession v0.0.2 // indirect
github.com/cloudwego/netpoll v0.5.0 // indirect
github.com/cloudwego/thriftgo v0.3.0 // indirect
github.com/cloudwego/netpoll v0.6.0 // indirect
github.com/cloudwego/thriftgo v0.3.6 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dubbogo/gost v1.14.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20230509042627-b1315fad0c5a // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -39,18 +46,35 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oleiade/lane v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tidwall/gjson v1.9.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.45.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.20.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.20.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a // indirect

Check warning on line 74 in samples/helloworld/kitex/go.mod

View workflow job for this annotation

GitHub Actions / compliant

"ba" should be "by" or "be".
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 575b15f

Please sign in to comment.