Skip to content

Commit

Permalink
feat: frugal usage example (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix021 authored Oct 6, 2023
1 parent 912eb54 commit 22ba921
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 9 deletions.
37 changes: 37 additions & 0 deletions frugal/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"context"

"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/remote/codec/thrift"
"github.com/cloudwego/kitex/transport"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
)

func frugalClient() {
codec := thrift.NewThriftCodecWithConfig(thrift.FrugalRead | thrift.FrugalWrite)
framed := client.WithTransportProtocol(transport.Framed)
server := client.WithHostPorts("127.0.0.1:8888")
cli := echo.MustNewClient("a.b.c", server, client.WithPayloadCodec(codec), framed)
rsp, err := cli.Echo(context.Background(), &api.Request{Message: "Hello"})
klog.Infof("resp: %v, err: %v", rsp, err)
}
97 changes: 97 additions & 0 deletions frugal/codec/frugal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2023 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"fmt"
"reflect"

"github.com/apache/thrift/lib/go/thrift"
"github.com/cloudwego/frugal"
"github.com/cloudwego/kitex/pkg/protocol/bthrift"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
)

type Meta struct {
MethodName string
MessageType thrift.TMessageType
SeqID int32
}

func HasFrugalTag(data interface{}) bool {
dt := reflect.TypeOf(data).Elem()
if dt.NumField() > 0 && dt.Field(0).Tag.Get("frugal") == "" {
return false
}
return true
}

func FrugalEncode(data interface{}, meta Meta) ([]byte, error) {
if !HasFrugalTag(data) {
return nil, fmt.Errorf("frugal tag missing")
}
// calculate and malloc message buffer
msgBeginLen := bthrift.Binary.MessageBeginLength(meta.MethodName, meta.MessageType, meta.SeqID)
msgEndLen := bthrift.Binary.MessageEndLength()
objectLen := frugal.EncodedSize(data)
buf := make([]byte, msgBeginLen+objectLen+msgEndLen)

// encode message
offset := bthrift.Binary.WriteMessageBegin(buf, meta.MethodName, meta.MessageType, meta.SeqID)
writeLen, err := frugal.EncodeObject(buf[offset:], nil, data)
if err != nil {
return buf, fmt.Errorf("thrift marshal, frugal.EncodeObject failed: %s", err.Error())
}
offset += writeLen
bthrift.Binary.WriteMessageEnd(buf[offset:])
return buf, nil
}

func FrugalDecode(buf []byte, data interface{}) (Meta, error) {
if !HasFrugalTag(data) {
return Meta{}, fmt.Errorf("frugal tag missing")
}
methodName, messageType, seqID, length, err := bthrift.Binary.ReadMessageBegin(buf)
if err != nil {
return Meta{}, err
}
_, err = frugal.DecodeObject(buf[length:], data)
return Meta{
MethodName: methodName,
MessageType: messageType,
SeqID: seqID,
}, err
}

func main() {
meta := Meta{
MethodName: "echo",
MessageType: thrift.CALL,
SeqID: 0x01020304,
}
req := &api.EchoEchoArgs{
Req: &api.Request{
Message: "Hello",
},
}
buf, err := FrugalEncode(req, meta)
fmt.Printf("Encode: buf = %v, err = %v\n", buf, err)

decodedReq := &api.EchoEchoArgs{}
meta, err = FrugalDecode(buf, decodedReq)
fmt.Printf("Decode: req = %v, meta = %v err = %v\n", decodedReq, meta, err)
}
25 changes: 25 additions & 0 deletions frugal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"github.com/cloudwego/kitex/server"
)

func main() {
server.RegisterStartHook(frugalClient)
frugalServer()
}
44 changes: 44 additions & 0 deletions frugal/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"context"

"github.com/cloudwego/kitex/pkg/remote/codec/thrift"
"github.com/cloudwego/kitex/server"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
// If you choose the slim template, make sure the client have `Framed` enabled
// "github.com/cloudwego/kitex-examples/kitex_gen/slim/api"
// "github.com/cloudwego/kitex-examples/kitex_gen/slim/api/echo"
)

type EchoImpl struct{}

func (e EchoImpl) Echo(ctx context.Context, req *api.Request) (r *api.Response, err error) {
return &api.Response{Message: req.Message}, nil
}

func frugalServer() {
code := thrift.NewThriftCodecWithConfig(thrift.FrugalRead | thrift.FrugalWrite)
svr := echo.NewServer(new(EchoImpl), server.WithPayloadCodec(code))
err := svr.Run()
if err != nil {
panic(err)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
require (
github.com/apache/thrift v0.16.0
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b
github.com/cloudwego/kitex v0.7.0
github.com/cloudwego/kitex v0.7.2
github.com/kitex-contrib/monitor-prometheus v0.1.0
github.com/kitex-contrib/obs-opentelemetry v0.2.3
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20230819133448-76093321aa8e
Expand All @@ -22,6 +22,7 @@ require (
require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/frugal v0.1.8
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.uber.org/atomic v1.10.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -675,22 +675,22 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cloudwego/configmanager v0.2.0 h1:niVpVg+wQ+npNqnH3dup96SMbR02Pk+tNErubYCJqKo=
github.com/cloudwego/configmanager v0.2.0/go.mod h1:FLIQTjxsZRGjnmDhTttWQTy6f6DghPTatfBVOs2gQLk=
github.com/cloudwego/dynamicgo v0.1.0/go.mod h1:Mdsz0XGsIImi15vxhZaHZpspNChEmBMIiWkUfD6JDKg=
github.com/cloudwego/dynamicgo v0.1.2 h1:t5KMzo/UkT002n3EvGI0Y6+Me73NGDzFI/AQlT1LQME=
github.com/cloudwego/dynamicgo v0.1.2/go.mod h1:AdPqyFN+0+fc3iVSSWojDCnOGPkzH+T0rI65017GCUA=
github.com/cloudwego/dynamicgo v0.1.3 h1:xK2rFS3E7cGbo4CWhqP1HIWeQcVH4Po5YgdNFDC+CfI=
github.com/cloudwego/dynamicgo v0.1.3/go.mod h1:AdPqyFN+0+fc3iVSSWojDCnOGPkzH+T0rI65017GCUA=
github.com/cloudwego/fastpb v0.0.3/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/fastpb v0.0.4 h1:/ROVVfoFtpfc+1pkQLzGs+azjxUbSOsAqSY4tAAx4mg=
github.com/cloudwego/fastpb v0.0.4/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/frugal v0.1.3/go.mod h1:b981ViPYdhI56aFYsoMjl9kv6yeqYSO+iEz2jrhkCgI=
github.com/cloudwego/frugal v0.1.6/go.mod h1:9ElktKsh5qd2zDBQ5ENhPSQV7F2dZ/mXlr1eaZGDBFs=
github.com/cloudwego/frugal v0.1.7 h1:Ggyk8mk0WrhBlM4g4RJxdOcVWJl/Hxbd8NJ19J8My6c=
github.com/cloudwego/frugal v0.1.7/go.mod h1:3VECBCSiTYwm3QApqHXjZB9NDH+8hUw7txxlr+6pPb4=
github.com/cloudwego/frugal v0.1.8 h1:MaJDRfvSnepsbUyMlQA9cySJ2+Y/we+r57tv5txx3sE=
github.com/cloudwego/frugal v0.1.8/go.mod h1:F0mLIWHymuQgh6r8N0owTA/ARv1B4SOiKa88tpOAfEU=
github.com/cloudwego/kitex v0.0.4/go.mod h1:EIjPJ4Dom2ornk7xDCdKpUpOnf4Tulevimh4Tn05OGc=
github.com/cloudwego/kitex v0.3.2/go.mod h1:/XD07VpUD9VQWmmoepASgZ6iw//vgWikVA9MpzLC5i0=
github.com/cloudwego/kitex v0.4.4/go.mod h1:3FcH5h9Qw+dhRljSzuGSpWuThttA8DvK0BsL7HUYydo=
github.com/cloudwego/kitex v0.5.2/go.mod h1:JpJVDdecl6bU5hlmtxooZgcnAvLXpYTU/A7UDdrTcPk=
github.com/cloudwego/kitex v0.6.1/go.mod h1:zI1GBrjT0qloTikcCfQTgxg3Ws+yQMyaChEEOcGNUvA=
github.com/cloudwego/kitex v0.7.0 h1:v58nahUC5f0ETbIHVshgZbekMxw5gAyC6Pm5xzI4xmY=
github.com/cloudwego/kitex v0.7.0/go.mod h1:RVWi+MbiPzI0Gi7fz8KZp+zsxB1/pLJZkr4kEwAuX6k=
github.com/cloudwego/kitex v0.7.2 h1:lF+Yb058k+91C4dLCvG7YzcoK4CRlV15mltl5WZIbek=
github.com/cloudwego/kitex v0.7.2/go.mod h1:56xuO0DCgKwOMv8NjAs/ooDe1Zb9W8iMKiw04Wv0o7Q=
github.com/cloudwego/localsession v0.0.2 h1:N9/IDtCPj1fCL9bCTP+DbXx3f40YjVYWcwkJG0YhQkY=
github.com/cloudwego/localsession v0.0.2/go.mod h1:kiJxmvAcy4PLgKtEnPS5AXed3xCiXcs7Z+KBHP72Wv8=
github.com/cloudwego/netpoll v0.0.2/go.mod h1:rZOiNI0FYjuvNybXKKhAPUja03loJi/cdv2F55AE6E8=
Expand All @@ -699,8 +699,8 @@ github.com/cloudwego/netpoll v0.2.4/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzC
github.com/cloudwego/netpoll v0.3.1/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E=
github.com/cloudwego/netpoll v0.3.2/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/netpoll v0.4.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/netpoll v0.4.1 h1:/pGsY7Rs09KqEXEniB9fcsEWfi1iY+66bKUO3/NO6hc=
github.com/cloudwego/netpoll v0.4.1/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/netpoll v0.5.0 h1:oRrOp58cPCvK2QbMozZNDESvrxQaEHW2dCimmwH1lcU=
github.com/cloudwego/netpoll v0.5.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/netpoll-http2 v0.0.4/go.mod h1:iFr5SzJCXIYgBg0ubL0fZiCQ6W36s9p0KjXpV04lmoY=
github.com/cloudwego/thriftgo v0.1.2/go.mod h1:LzeafuLSiHA9JTiWC8TIMIq64iadeObgRUhmVG1OC/w=
github.com/cloudwego/thriftgo v0.2.4/go.mod h1:8i9AF5uDdWHGqzUhXDlubCjx4MEfKvWXGQlMWyR0tM4=
Expand Down
64 changes: 64 additions & 0 deletions kitex_gen/slim/api/echo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions kitex_gen/slim/api/echo/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 22ba921

Please sign in to comment.