Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

affected/package: stream send need a timeout option to avoid slow client attack #192

Open
ArmstrongCN opened this issue Nov 29, 2024 · 1 comment

Comments

@ArmstrongCN
Copy link

Is your feature request related to a problem?

Slow client read under a high speed server send connection, may hang the server go routine and resource util client destroy. we observe more than 6k seconds hang in a case.

What is the solution you'd like?

add a timeout for every server send calling, so the go routine has the chance to abort or drop some buffer.

What alternatives have you considered?

may be server read has the same problem?

@WineChord
Copy link
Contributor

The design of trpc's stream is somewhat like grpc; the stream does not have a timeout on each send/recv but on the whole stream.

You can add a timeout control by yourself such as

func (s *testStreamImpl) ServerStream(req *pb.HelloReq, stream pb.TestStream_ServerStreamServer) error {
	log.Infof("ServerStream receive Msg: %s", req.GetMsg())
	for i := 0; i < 5; i++ {
		// Continuously call Send to send the response.
		t := time.NewTimer(time.Second)
		done := make(chan struct{})
		go func() {
			defer func() {
				close(done)
			}()
			if err := stream.SendMsg(&pb.HelloRsp{Msg: fmt.Sprintf(" pong: %v", i)}); err != nil {
				// ...
			}
		}()
		select {
		case <-t.C:
			return errors.New("server stream send timeout")
		case <-done:
			continue
		}
	}
	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants