Skip to content

Instantly share code, notes, and snippets.

@akovardin
Created August 1, 2018 10:44
Show Gist options
  • Select an option

  • Save akovardin/f8d8184f36ee7cfe6ac5bc12a908a335 to your computer and use it in GitHub Desktop.

Select an option

Save akovardin/f8d8184f36ee7cfe6ac5bc12a908a335 to your computer and use it in GitHub Desktop.
errgroup
package gateway
import (
"context"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"golang.org/x/sync/errgroup"
"github.com/propellerads/regressor/api"
"github.com/propellerads/regressor/app/log"
)
func Start(ctx context.Context, addr, endpoint string) error {
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
if err := api.RegisterRegressorServiceHandlerFromEndpoint(ctx, mux, endpoint, opts); err != nil {
log.Warnw("failed to listen grpc addr", "value", err)
return err
}
srv := &http.Server{
Handler: mux,
Addr: addr,
}
log.Infow("starting grpc gateway server", "address", addr)
e, _ := errgroup.WithContext(ctx)
e.Go(func() error {
return srv.ListenAndServe()
})
e.Go(func() error {
<-ctx.Done()
return srv.Shutdown(ctx)
})
return e.Wait()
}errgroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment