Skip to content

Commit

Permalink
start GRPCWebServer in goroutine
Browse files Browse the repository at this point in the history
so it don't block other code from executing.

Closes cosmos#9703
  • Loading branch information
yihuang committed Jul 15, 2021
1 parent eb9b18d commit 7374804
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions server/grpc/grpc_web.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package grpc

import (
"fmt"
"net/http"
"time"

"github.com/improbable-eng/grpc-web/go/grpcweb"
"google.golang.org/grpc"
Expand All @@ -28,8 +30,16 @@ func StartGRPCWeb(grpcSrv *grpc.Server, config config.Config) (*http.Server, err
Addr: config.GRPCWeb.Address,
Handler: http.HandlerFunc(handler),
}
if err := grpcWebSrv.ListenAndServe(); err != nil {
errCh := make(chan error)
go func() {
if err := grpcWebSrv.ListenAndServe(); err != nil {
errCh <- fmt.Errorf("failed to serve: %w", err)
}
}()
select {
case err := <-errCh:
return nil, err
case <-time.After(5 * time.Second): // assume server started successfully
return grpcWebSrv, nil
}
return grpcWebSrv, nil
}

0 comments on commit 7374804

Please sign in to comment.