Skip to content

Instantly share code, notes, and snippets.

Last active February 22, 2022 07:32
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
package main
import (
pb ""
type server struct{}
func (s server) FetchResponse(in *pb.Request, srv pb.StreamService_FetchResponseServer) error {
log.Printf("fetch response for id : %d", in.Id)
//use wait group to allow process to be concurrent
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
go func(count int64) {
defer wg.Done()
//time sleep to simulate server process time
time.Sleep(time.Duration(count) * time.Second)
resp := pb.Response{Result: fmt.Sprintf("Request #%d For Id:%d", count, in.Id)}
if err := srv.Send(&resp); err != nil {
log.Printf("send error %v", err)
log.Printf("finishing request number : %d", count)
return nil
func main() {
// create listiner
lis, err := net.Listen("tcp", ":50005")
if err != nil {
log.Fatalf("failed to listen: %v", err)
// create grpc server
s := grpc.NewServer()
pb.RegisterStreamServiceServer(s, server{})
log.Println("start server")
// and start...
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment