snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
111
examples/features/flow_control/server/main.go
Normal file
111
examples/features/flow_control/server/main.go
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 gRPC authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// Binary server demonstrates how gRPC flow control block sending when the
|
||||
// receiver is not ready.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "google.golang.org/grpc/examples/features/proto/echo"
|
||||
"google.golang.org/grpc/internal/grpcsync"
|
||||
)
|
||||
|
||||
var port = flag.Int("port", 50052, "port number")
|
||||
|
||||
var payload = string(make([]byte, 8*1024)) // 8KB
|
||||
|
||||
// server is used to implement EchoServer.
|
||||
type server struct {
|
||||
pb.UnimplementedEchoServer
|
||||
}
|
||||
|
||||
func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
|
||||
log.Printf("New stream began.")
|
||||
// First, we wait 2 seconds before reading from the stream, to give the
|
||||
// client an opportunity to block while sending its requests.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Next, read all the data sent by the client to allow it to unblock.
|
||||
for i := 0; true; i++ {
|
||||
if _, err := stream.Recv(); err != nil {
|
||||
log.Printf("Read %v messages.", i)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
log.Printf("Error receiving data: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, send data until we block, then end the stream after we unblock.
|
||||
stopSending := grpcsync.NewEvent()
|
||||
sentOne := make(chan struct{})
|
||||
go func() {
|
||||
for !stopSending.HasFired() {
|
||||
after := time.NewTimer(time.Second)
|
||||
select {
|
||||
case <-sentOne:
|
||||
after.Stop()
|
||||
case <-after.C:
|
||||
log.Printf("Sending is blocked.")
|
||||
stopSending.Fire()
|
||||
<-sentOne
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
i := 0
|
||||
for !stopSending.HasFired() {
|
||||
i++
|
||||
if err := stream.Send(&pb.EchoResponse{Message: payload}); err != nil {
|
||||
log.Printf("Error sending data: %v", err)
|
||||
return err
|
||||
}
|
||||
sentOne <- struct{}{}
|
||||
}
|
||||
log.Printf("Sent %v messages.", i)
|
||||
|
||||
log.Printf("Stream ended successfully.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
address := fmt.Sprintf(":%v", *port)
|
||||
lis, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
pb.RegisterEchoServer(grpcServer, &server{})
|
||||
|
||||
if err := grpcServer.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user