Copilot commented on code in PR #763: URL: https://github.com/apache/dubbo-go-pixiu/pull/763#discussion_r2371306703
########## tools/benchmark/test/grpc_suite/grpc_test.go: ########## @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 grpc_suite + +import ( + "context" + "io/ioutil" Review Comment: The 'io/ioutil' package is deprecated since Go 1.16. Use 'io' and 'os' packages instead. Replace 'ioutil.ReadAll' with 'io.ReadAll' and 'ioutil.Discard' with 'io.Discard'. ```suggestion ``` ########## tools/benchmark/test/triple_suite/proto_suite/proto_test.go: ########## @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 proto_suite + +import ( + "context" + "fmt" + "io/ioutil" Review Comment: The 'io/ioutil' package is deprecated since Go 1.16. Use 'io' and 'os' packages instead. Replace 'ioutil.ReadAll' with 'io.ReadAll' and 'ioutil.Discard' with 'io.Discard'. ########## tools/benchmark/protocol/grpc/go-server/cmd/server.go: ########## @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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" + "fmt" + "net" + "os" + "os/signal" + "syscall" + "time" +) + +import ( + "dubbo-go-pixiu-benchmark/protocol/grpc/proto" + + perrors "github.com/pkg/errors" + + "google.golang.org/grpc" + "google.golang.org/grpc/keepalive" + "google.golang.org/grpc/reflection" +) + +const ( + MsgUserNotFound = "user not found" + MsgUserQuerySuccessfully = "user(s) query successfully" +) + +// Test Cases +// curl http://127.0.0.1:8881/api/v1/provider.UserProvider/GetUser +// curl http://127.0.0.1:8881/api/v1/provider.UserProvider/GetUser -X POST -d '{"userId":1}' + +type server struct { + users map[int32]*proto.User + proto.UnimplementedUserProviderServer +} + +func (s *server) GetUser(ctx context.Context, request *proto.GetUserRequest) (*proto.GetUsersResponse, error) { + us := make([]*proto.User, 0) + if request.GetUserId() == 0 { + for i := 1; i <= 2; i++ { + us = append(us, s.users[int32(i)]) + } + } else { + u, ok := s.users[request.GetUserId()] + if !ok { + return &proto.GetUsersResponse{}, perrors.New("Invalid User ID") + } + us = append(us, u) + } + return &proto.GetUsersResponse{Users: us}, nil +} + +func (s *server) GetUsers(ctx context.Context, request *proto.GetUsersRequest) (*proto.GetUsersResponse, error) { + us := make([]*proto.User, 0) + for _, userId := range request.UserId { + u, ok := s.users[userId] + if ok { + us = append(us, u) + } + } + return &proto.GetUsersResponse{Users: us}, nil +} + +func (s *server) GetUserByName(ctx context.Context, request *proto.GetUserByNameRequest) (*proto.GetUsersResponse, error) { + for i, user := range s.users { + if user.Name == request.Name { + return &proto.GetUsersResponse{Users: []*proto.User{s.users[i]}}, nil + } + } + return &proto.GetUsersResponse{}, perrors.New("Invalid User Name") +} + +func initUsers(s *server) { + s.users[1] = &proto.User{UserId: 1, Name: "Kenway"} + s.users[2] = &proto.User{UserId: 2, Name: "Ken"} +} + +func main() { + l, err := net.Listen("tcp", ":50001") //nolint:gosec + if err != nil { + panic(err) + } + + s := &server{users: make(map[int32]*proto.User)} + initUsers(s) + + keepAliveArgs := keepalive.ServerParameters{ + Time: 60 * time.Second, + Timeout: 5 * time.Second, + } + gs := grpc.NewServer(grpc.KeepaliveParams(keepAliveArgs)) + + proto.RegisterUserProviderServer(gs, s) + + // registers the server reflection service on the given gRPC server. + reflection.Register(gs) + + fmt.Println("grpc test server is now running...") + go func() { + err = gs.Serve(l) + if err != nil { + panic(err) + } + }() + + initSignal() + gs.GracefulStop() // handle request until all of them is done +} + +func initSignal() { + signals := make(chan os.Signal, 1) + // It is not possible to block SIGKILL or syscall.SIGSTOP + signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + sig := <-signals + switch sig { + case syscall.SIGHUP: + // reload() + default: + time.AfterFunc(time.Duration(int(3e9)), func() { Review Comment: Use a named constant instead of the magic number '3e9'. Consider defining 'const shutdownTimeout = 3 * time.Second' and using 'time.AfterFunc(shutdownTimeout, func() {'. ########## tools/benchmark/test/dubbo_suite/dubbo_test.go: ########## @@ -0,0 +1,256 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 dubbo_suite + +import ( + "context" + "fmt" + "io/ioutil" Review Comment: The 'io/ioutil' package is deprecated since Go 1.16. Use 'io' and 'os' packages instead. Replace 'ioutil.ReadAll' with 'io.ReadAll' and 'ioutil.Discard' with 'io.Discard'. ########## tools/benchmark/test/test_tool.go: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 test + +import ( + "io/ioutil" Review Comment: The 'io/ioutil' package is deprecated since Go 1.16. Use 'io' and 'os' packages instead. Replace 'ioutil.Discard' with 'io.Discard'. ########## tools/benchmark/protocol/dubbo/go-server/cmd/server.go: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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" + "os" + "os/signal" + "syscall" + "time" +) + +import ( + "dubbo-go-pixiu-benchmark/protocol/dubbo/go-server/pkg" + + "dubbo.apache.org/dubbo-go/v3/config" + _ "dubbo.apache.org/dubbo-go/v3/imports" + + hessian "github.com/apache/dubbo-go-hessian2" + + "github.com/dubbogo/gost/log/logger" +) + +func main() { + // ------for hessian2------ + hessian.RegisterJavaEnum(pkg.Gender(pkg.MAN)) + hessian.RegisterJavaEnum(pkg.Gender(pkg.WOMAN)) + hessian.RegisterPOJO(&pkg.User{}) + config.SetProviderService(&pkg.UserProvider{}) + // ------------ + curPath, err := os.Getwd() + curPath = curPath + "/../../protocol/dubbo/go-server/conf/dubbogo.yml" + if err != nil { + panic(err) + } + if err := config.Load(config.WithPath(curPath)); err != nil { + panic(err) + } + + initSignal() + +} + +func initSignal() { + signals := make(chan os.Signal, 1) + // It is not possible to block SIGKILL or syscall.SIGSTOP + signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + sig := <-signals + logger.Infof("get signal %s", sig.String()) + switch sig { + case syscall.SIGHUP: + // reload() + default: + time.AfterFunc(time.Duration(int(3e9)), func() { Review Comment: Use a named constant instead of the magic number '3e9'. Consider defining 'const shutdownTimeout = 3 * time.Second' and using 'time.AfterFunc(shutdownTimeout, func() {'. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
