-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Felix021
authored
Oct 6, 2023
1 parent
912eb54
commit 22ba921
Showing
14 changed files
with
560 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.