Skip to content

Commit

Permalink
Also wait for the grpc server to get ready
Browse files Browse the repository at this point in the history
  • Loading branch information
yugui committed Apr 27, 2018
1 parent 917145b commit 63d6aa1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/gateway/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ go_library(
"//runtime:go_default_library",
"@com_github_golang_glog//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//connectivity:go_default_library",
],
)
13 changes: 1 addition & 12 deletions examples/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@ import (
"net/http"
"time"

"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/examples/proto/examplepb"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
)

// newGateway returns a new gateway server which translates HTTP into gRPC.
func newGateway(ctx context.Context, network, addr string, opts []gwruntime.ServeMuxOption) (http.Handler, error) {
conn, err := dial(ctx, network, addr)
if err != nil {
return nil, err
}
go func() {
<-ctx.Done()
if err := conn.Close(); err != nil {
glog.Errorf("Failed to close a client connection to the gRPC server: %v", err)
}
}()
func newGateway(ctx context.Context, conn *grpc.ClientConn, opts []gwruntime.ServeMuxOption) (http.Handler, error) {

mux := gwruntime.NewServeMux(opts...)

Expand Down
14 changes: 11 additions & 3 deletions examples/gateway/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

"github.com/golang/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)

func swaggerServer(dir string) http.HandlerFunc {
Expand Down Expand Up @@ -47,7 +49,13 @@ func preflightHandler(w http.ResponseWriter, r *http.Request) {
glog.Infof("preflight request for %s", r.URL.Path)
}

func healthzHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "ok")
func healthzServer(conn *grpc.ClientConn) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
if s := conn.GetState(); s != connectivity.Ready {
http.Error(w, fmt.Sprintf("grpc server is %s", s), http.StatusBadGateway)
return
}
fmt.Fprintln(w, "ok")
}
}
15 changes: 13 additions & 2 deletions examples/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,22 @@ func Run(ctx context.Context, opts Options) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

conn, err := dial(ctx, opts.GRPCServer.Network, opts.GRPCServer.Addr)
if err != nil {
return err
}
go func() {
<-ctx.Done()
if err := conn.Close(); err != nil {
glog.Errorf("Failed to close a client connection to the gRPC server: %v", err)
}
}()

mux := http.NewServeMux()
mux.HandleFunc("/swagger/", swaggerServer(opts.SwaggerDir))
mux.HandleFunc("/healthz", healthzHandler)
mux.HandleFunc("/healthz", healthzServer(conn))

gw, err := newGateway(ctx, opts.GRPCServer.Network, opts.GRPCServer.Addr, opts.Mux)
gw, err := newGateway(ctx, conn, opts.Mux)
if err != nil {
return err
}
Expand Down

0 comments on commit 63d6aa1

Please sign in to comment.