Giter VIP home page Giter VIP logo

Comments (3)

zasweq avatar zasweq commented on June 20, 2024

What does "is busy" semantically mean here? If you want to check if the server just doesn't create the stream, you can use the timeout in the context, and it will return an error. Once a stream is created, your application handler can use it to signal back to the client based off properties about the server whether it is busy or not.

from grpc-go.

mostafafarzaneh avatar mostafafarzaneh commented on June 20, 2024

I did more testing and figured out that I needed to read from the stream on the client side to get the error. I was expecting to get the error on stream creation, which I suppose was naive.

Here is the server code:

grpc::Status TestV1ServiceImpl::run_session(::grpc::ServerContext *context,
                                            ::grpc::ServerReader<::test::v1::Request> *stream,
                                            ::google::protobuf::Empty *) {
  {
    auto cpu = utils::get_cpu_usage();
    if (cpu > 0) {
      printf("1111CPU usage %f exeeds 80\n", cpu);
      return grpc::Status(grpc::StatusCode::RESOURCE_EXHAUSTED, "CPU resource exhausted");
    }
  }


  proto::Request req;
  while(true) {
    auto res = stream->Read(&req);
    if (!res) {
      printf("Cancel connection. Read error\f");
      return grpc::Status::CANCELLED;
    } else {
      printf("RECEIVED!\n");
    }
  }
}

And here is my client:

func main() {
        conn, err := createConnection()
        if err != nil {
                fmt.Println("1: ", err)
                panic(err)
        }

        ctx, _ := context.WithCancel(context.Background())

        c := test.NewTestClient(conn)
        stream, err := c.RunSession(ctx)

        if err != nil {
                fmt.Println("2: ", err)
                panic(err)
        }

        m := new(emptypb.Empty)
        for err == nil {
                if err := stream.RecvMsg(m); err != nil {
                        fmt.Println("4: ", err)
                        panic(err)
                }

        }

        fmt.Println("####EXITING####")
        fmt.Println("5: ", err)
        return
}

from grpc-go.

arvindbr8 avatar arvindbr8 commented on June 20, 2024

Great. Hope that solves your issue :)

from grpc-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.