2018-05-22 21:43:36 +02:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// 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 18:23:09 +01: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 04:45:07 +02:00
|
|
|
"strings"
|
2017-03-11 18:23:09 +01:00
|
|
|
|
2019-09-16 23:16:43 +02:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-04-19 23:46:50 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-03-11 18:23:09 +01:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
)
|
|
|
|
|
2017-06-21 19:31:06 +02: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")
|
|
|
|
}
|
|
|
|
|
2017-03-11 18:23:09 +01:00
|
|
|
// Serve creates a new gRPC server, calls out to the supplied registration functions to bind interfaces, and then
|
|
|
|
// listens on the supplied TCP port. If the caller wishes for the kernel to choose a free port automatically, pass 0 as
|
|
|
|
// the port number. The return values are: the chosen port (the same as supplied if non-0), a channel that may
|
|
|
|
// eventually return an error, and an error, in case something went wrong. The channel is non-nil and waits until
|
|
|
|
// the server is finished, in the case of a successful launch of the RPC server.
|
2019-09-16 23:16:43 +02:00
|
|
|
func Serve(port int, cancel chan bool, registers []func(*grpc.Server) error,
|
|
|
|
parentSpan opentracing.Span) (int, chan error, error) {
|
|
|
|
|
2017-03-11 18:23:09 +01:00
|
|
|
// Listen on a TCP port, but let the kernel choose a free port for us.
|
2017-09-21 19:56:45 +02:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
|
2017-03-11 18:23:09 +01:00
|
|
|
if err != nil {
|
2017-04-19 23:46:50 +02:00
|
|
|
return port, nil, errors.Errorf("failed to listen on TCP port ':%v': %v", port, err)
|
2017-03-11 18:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now new up a gRPC server and register any RPC interfaces the caller wants.
|
2019-09-16 23:16:43 +02:00
|
|
|
srv := grpc.NewServer(grpc.UnaryInterceptor(OpenTracingServerInterceptor(parentSpan)))
|
2017-03-11 18:23:09 +01:00
|
|
|
for _, register := range registers {
|
|
|
|
if err := register(srv); err != nil {
|
2017-04-19 23:46:50 +02:00
|
|
|
return port, nil, errors.Errorf("failed to register RPC handler: %v", err)
|
2017-03-11 18:23:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reflection.Register(srv) // enable reflection.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-10-24 23:29:33 +02:00
|
|
|
// If the caller provided a cancellation channel, start a goroutine that will gracefully terminate the gRPC server when
|
|
|
|
// that channel is closed or receives a `true` value.
|
|
|
|
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 04:45:07 +02:00
|
|
|
}
|
2017-10-24 23:29:33 +02:00
|
|
|
|
|
|
|
srv.GracefulStop()
|
|
|
|
}()
|
|
|
|
}
|
2017-03-11 18:23:09 +01: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 19:31:06 +02:00
|
|
|
if err := srv.Serve(lis); err != nil && !IsBenignCloseErr(err) {
|
2017-04-19 23:46:50 +02:00
|
|
|
done <- errors.Errorf("stopped serving: %v", err)
|
2017-03-11 18:23:09 +01:00
|
|
|
} else {
|
|
|
|
done <- nil // send a signal so caller knows we're done, even though it's nil.
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return port, done, nil
|
|
|
|
}
|