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 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.SetMaxReceiveMessageSize(static_cast<int>(max_msg_mb_size * (1u << 20)));
  34. }
  35. serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  36. serverBuilder.RegisterService(service.get());
  37. server_ = serverBuilder.BuildAndStart();
  38. if (server_ == nullptr) {
  39. return INFER_STATUS_LOG_ERROR(FAILED) << "Serving Error: " << server_tag
  40. << " server start failed, create server failed, address " << server_address;
  41. }
  42. auto grpc_server_run = [this, server_address, server_tag]() {
  43. MSI_LOG(INFO) << server_tag << " server start success, listening on " << server_address;
  44. std::cout << "Serving: " << server_tag << " server start success, listening on " << server_address << std::endl;
  45. server_->Wait();
  46. };
  47. grpc_thread_ = std::thread(grpc_server_run);
  48. in_running_ = true;
  49. return SUCCESS;
  50. }
  51. void GrpcServer::Stop() {
  52. if (in_running_) {
  53. server_->Shutdown();
  54. grpc_thread_.join();
  55. }
  56. in_running_ = false;
  57. }
  58. } // namespace mindspore::serving

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