You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

grpc_server.cc 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/grpc_server.h"
  17. namespace mindspore::serving {
  18. Status GrpcServer::Start(std::shared_ptr<grpc::Service> service, const std::string &ip, uint32_t grpc_port,
  19. int max_msg_mb_size, const std::string &server_tag) {
  20. service_ = service;
  21. Status status;
  22. if (in_running_) {
  23. return INFER_STATUS_LOG_ERROR(SYSTEM_ERROR) << "Serving Error: " << server_tag << " server is already running";
  24. }
  25. std::string server_address = ip + ":" + std::to_string(grpc_port);
  26. grpc::EnableDefaultHealthCheckService(true);
  27. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  28. // Set the port is not reuseable
  29. auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
  30. grpc::ServerBuilder serverBuilder;
  31. serverBuilder.SetOption(std::move(option));
  32. if (max_msg_mb_size > 0) {
  33. serverBuilder.SetMaxSendMessageSize(static_cast<int>(max_msg_mb_size * (1u << 20)));
  34. serverBuilder.SetMaxReceiveMessageSize(static_cast<int>(max_msg_mb_size * (1u << 20)));
  35. }
  36. serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  37. serverBuilder.RegisterService(service.get());
  38. server_ = serverBuilder.BuildAndStart();
  39. if (server_ == nullptr) {
  40. return INFER_STATUS_LOG_ERROR(FAILED) << "Serving Error: " << server_tag
  41. << " server start failed, create server failed, address " << server_address;
  42. }
  43. auto grpc_server_run = [this, server_address, server_tag]() {
  44. MSI_LOG(INFO) << server_tag << " server start success, listening on " << server_address;
  45. std::cout << "Serving: " << server_tag << " server start success, listening on " << server_address << std::endl;
  46. server_->Wait();
  47. };
  48. grpc_thread_ = std::thread(grpc_server_run);
  49. in_running_ = true;
  50. return SUCCESS;
  51. }
  52. void GrpcServer::Stop() {
  53. if (in_running_) {
  54. server_->Shutdown();
  55. grpc_thread_.join();
  56. server_ = nullptr;
  57. }
  58. in_running_ = false;
  59. }
  60. std::shared_ptr<grpc::Channel> GrpcServer::CreateChannel(const std::string &target_str) {
  61. grpc::ChannelArguments channel_args;
  62. channel_args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, gRpcMaxMBMsgSize * 1024 * 1024);
  63. std::shared_ptr<grpc::Channel> channel =
  64. grpc::CreateCustomChannel(target_str, grpc::InsecureChannelCredentials(), channel_args);
  65. return channel;
  66. }
  67. } // namespace mindspore::serving

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.