2022-11-01 15:15:09 +00:00
|
|
|
// Copyright 2016-2022, Pulumi Corporation.
|
2018-05-22 19:43:36 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2017-03-11 17:23:09 +00:00
|
|
|
|
|
|
|
package rpcutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strconv"
|
Introduce an interface to read config
This change adds an engine gRPC interface, and associated implementation,
so that plugins may do interesting things that require "phoning home".
Previously, the engine would fire up plugins and talk to them directly,
but there was no way for a plugin to ask the engine to do anything.
The motivation here is so that plugins can read evaluator state, such
as config information, but this change also allows richer logging
functionality than previously possible. We will still auto-log any
stdout/stderr writes; however, explicit errors, warnings, informational,
and even debug messages may be written over the Log API.
2017-06-21 02:45:07 +00:00
|
|
|
"strings"
|
2017-03-11 17:23:09 +00:00
|
|
|
|
2021-06-17 21:46:05 +00:00
|
|
|
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
|
2019-09-16 21:16:43 +00:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-04-19 21:46:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-03-11 17:23:09 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-06-06 12:28:00 +00:00
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
|
2017-03-11 17:23:09 +00:00
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
)
|
|
|
|
|
2020-04-20 22:25:51 +00:00
|
|
|
// maxRPCMessageSize raises the gRPC Max Message size from `4194304` (4mb) to `419430400` (400mb)
|
|
|
|
var maxRPCMessageSize = 1024 * 1024 * 400
|
|
|
|
|
|
|
|
// GrpcChannelOptions returns the defaultCallOptions with the max_receive_message_length increased to 400mb
|
|
|
|
// We want to increase the default message size as per pulumi/pulumi#2319
|
|
|
|
func GrpcChannelOptions() grpc.DialOption {
|
|
|
|
return grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxRPCMessageSize))
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:31:06 +00:00
|
|
|
// IsBenignCloseErr returns true if the error is "expected" upon shutdown of the server.
|
|
|
|
func IsBenignCloseErr(err error) bool {
|
|
|
|
msg := err.Error()
|
|
|
|
return strings.HasSuffix(msg, "use of closed network connection") ||
|
|
|
|
strings.HasSuffix(msg, "grpc: the server has been stopped")
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:15:09 +00:00
|
|
|
type ServeOptions struct {
|
|
|
|
// Port to listen on. Passing 0 makes the system choose a port automatically.
|
|
|
|
Port int
|
|
|
|
|
|
|
|
// Initializer for the server. A typical Init registers handlers.
|
|
|
|
Init func(*grpc.Server) error
|
|
|
|
|
|
|
|
// If non-nil, Serve will gracefully terminate the server when Cancel is closed or receives true.
|
|
|
|
Cancel chan bool
|
|
|
|
|
|
|
|
// Options for serving gRPC.
|
|
|
|
Options []grpc.ServerOption
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServeHandle struct {
|
|
|
|
// Port the server is listening on.
|
|
|
|
Port int
|
|
|
|
|
|
|
|
// The channel is non-nil and is closed when the server stops serving. The server will pass a non-nil error on
|
|
|
|
// this channel if something went wrong in the background and it did not terminate gracefully.
|
|
|
|
Done <-chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeWithOptions creates a new gRPC server, calls opts.Init and listens on a TCP port.
|
|
|
|
func ServeWithOptions(opts ServeOptions) (ServeHandle, error) {
|
|
|
|
h, _, err := serveWithOptions(opts)
|
|
|
|
return h, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveWithOptions(opts ServeOptions) (ServeHandle, chan error, error) {
|
|
|
|
port := opts.Port
|
2019-09-16 21:16:43 +00:00
|
|
|
|
2017-03-11 17:23:09 +00:00
|
|
|
// Listen on a TCP port, but let the kernel choose a free port for us.
|
2017-09-21 17:56:45 +00:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
|
2017-03-11 17:23:09 +00:00
|
|
|
if err != nil {
|
2022-11-01 15:15:09 +00:00
|
|
|
return ServeHandle{Port: port}, nil,
|
|
|
|
errors.Errorf("failed to listen on TCP port ':%v': %v", port, err)
|
2017-03-11 17:23:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 12:28:00 +00:00
|
|
|
health := health.NewServer()
|
|
|
|
|
2017-03-11 17:23:09 +00:00
|
|
|
// Now new up a gRPC server and register any RPC interfaces the caller wants.
|
2022-11-01 15:15:09 +00:00
|
|
|
|
|
|
|
srv := grpc.NewServer(append(opts.Options, grpc.MaxRecvMsgSize(maxRPCMessageSize))...)
|
|
|
|
|
|
|
|
if opts.Init != nil {
|
|
|
|
if err := opts.Init(srv); err != nil {
|
|
|
|
return ServeHandle{Port: port}, nil,
|
|
|
|
errors.Errorf("failed to Init GRPC to register RPC handlers: %v", err)
|
2017-03-11 17:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 15:15:09 +00:00
|
|
|
|
2022-06-06 12:28:00 +00:00
|
|
|
healthgrpc.RegisterHealthServer(srv, health) // enable health checks
|
|
|
|
reflection.Register(srv) // enable reflection.
|
|
|
|
|
|
|
|
// Set health checks for all the services that they are being served
|
|
|
|
services := srv.GetServiceInfo()
|
|
|
|
for serviceName := range services {
|
|
|
|
health.SetServingStatus(serviceName, healthgrpc.HealthCheckResponse_SERVING)
|
|
|
|
}
|
2017-03-11 17:23:09 +00:00
|
|
|
|
|
|
|
// If the port was 0, look up what port the kernel chosen, by accessing the underlying TCP listener/address.
|
|
|
|
if port == 0 {
|
|
|
|
tcpl := lis.(*net.TCPListener)
|
|
|
|
tcpa := tcpl.Addr().(*net.TCPAddr)
|
|
|
|
port = tcpa.Port
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:15:09 +00:00
|
|
|
cancel := opts.Cancel
|
2017-10-24 21:29:33 +00:00
|
|
|
if cancel != nil {
|
|
|
|
go func() {
|
|
|
|
for v, ok := <-cancel; !v && ok; v, ok = <-cancel {
|
Introduce an interface to read config
This change adds an engine gRPC interface, and associated implementation,
so that plugins may do interesting things that require "phoning home".
Previously, the engine would fire up plugins and talk to them directly,
but there was no way for a plugin to ask the engine to do anything.
The motivation here is so that plugins can read evaluator state, such
as config information, but this change also allows richer logging
functionality than previously possible. We will still auto-log any
stdout/stderr writes; however, explicit errors, warnings, informational,
and even debug messages may be written over the Log API.
2017-06-21 02:45:07 +00:00
|
|
|
}
|
2017-10-24 21:29:33 +00:00
|
|
|
|
|
|
|
srv.GracefulStop()
|
|
|
|
}()
|
|
|
|
}
|
2017-03-11 17:23:09 +00:00
|
|
|
|
|
|
|
// Finally, serve; this returns only once the server shuts down (e.g., due to a signal).
|
|
|
|
done := make(chan error)
|
|
|
|
go func() {
|
2017-06-21 17:31:06 +00:00
|
|
|
if err := srv.Serve(lis); err != nil && !IsBenignCloseErr(err) {
|
2017-04-19 21:46:50 +00:00
|
|
|
done <- errors.Errorf("stopped serving: %v", err)
|
2017-03-11 17:23:09 +00:00
|
|
|
} else {
|
|
|
|
done <- nil // send a signal so caller knows we're done, even though it's nil.
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2022-11-01 15:15:09 +00:00
|
|
|
return ServeHandle{Port: port, Done: done}, done, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated. Please use ServeWithOptions and OpenTracingServerInterceptorOptions.
|
|
|
|
func Serve(port int, cancel chan bool, registers []func(*grpc.Server) error,
|
2023-03-03 16:36:39 +00:00
|
|
|
parentSpan opentracing.Span, options ...otgrpc.Option,
|
|
|
|
) (int, chan error, error) {
|
2022-11-01 15:15:09 +00:00
|
|
|
opts := ServeOptions{
|
|
|
|
Port: port,
|
|
|
|
Cancel: cancel,
|
|
|
|
Init: func(s *grpc.Server) error {
|
|
|
|
for _, r := range registers {
|
|
|
|
if err := r(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Options: OpenTracingServerInterceptorOptions(parentSpan, options...),
|
|
|
|
}
|
|
|
|
|
|
|
|
handle, done, err := serveWithOptions(opts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle.Port, done, nil
|
2017-03-11 17:23:09 +00:00
|
|
|
}
|