2017-07-13 03:45:53 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-07-13 03:45:53 +00:00
|
|
|
"net"
|
2018-01-06 16:54:10 +00:00
|
|
|
"sync/atomic"
|
2017-07-13 03:45:53 +00:00
|
|
|
"time"
|
|
|
|
|
2020-04-23 00:10:31 +00:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
2018-01-06 16:54:10 +00:00
|
|
|
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
|
2020-04-23 00:10:31 +00:00
|
|
|
"github.com/moby/buildkit/util/grpcerrors"
|
2018-01-06 16:54:10 +00:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-07-13 03:45:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-13 03:45:53 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serve(ctx context.Context, grpcServer *grpc.Server, conn net.Conn) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
logrus.Debugf("serving grpc connection")
|
|
|
|
(&http2.Server{}).ServeConn(conn, &http2.ServeConnOpts{Handler: grpcServer})
|
|
|
|
}
|
|
|
|
|
|
|
|
func grpcClientConn(ctx context.Context, conn net.Conn) (context.Context, *grpc.ClientConn, error) {
|
2020-04-23 00:10:31 +00:00
|
|
|
var unary []grpc.UnaryClientInterceptor
|
|
|
|
var stream []grpc.StreamClientInterceptor
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
var dialCount int64
|
2020-07-18 21:51:19 +00:00
|
|
|
dialer := grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
2018-01-06 16:54:10 +00:00
|
|
|
if c := atomic.AddInt64(&dialCount, 1); c > 1 {
|
|
|
|
return nil, errors.Errorf("only one connection allowed")
|
|
|
|
}
|
2017-07-13 03:45:53 +00:00
|
|
|
return conn, nil
|
|
|
|
})
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
dialOpts := []grpc.DialOption{
|
|
|
|
dialer,
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if span := opentracing.SpanFromContext(ctx); span != nil {
|
|
|
|
tracer := span.Tracer()
|
2020-04-23 00:10:31 +00:00
|
|
|
unary = append(unary, otgrpc.OpenTracingClientInterceptor(tracer, traceFilter()))
|
|
|
|
stream = append(stream, otgrpc.OpenTracingStreamClientInterceptor(tracer, traceFilter()))
|
|
|
|
}
|
|
|
|
|
|
|
|
unary = append(unary, grpcerrors.UnaryClientInterceptor)
|
|
|
|
stream = append(stream, grpcerrors.StreamClientInterceptor)
|
|
|
|
|
|
|
|
if len(unary) == 1 {
|
|
|
|
dialOpts = append(dialOpts, grpc.WithUnaryInterceptor(unary[0]))
|
|
|
|
} else if len(unary) > 1 {
|
|
|
|
dialOpts = append(dialOpts, grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(unary...)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(stream) == 1 {
|
|
|
|
dialOpts = append(dialOpts, grpc.WithStreamInterceptor(stream[0]))
|
|
|
|
} else if len(stream) > 1 {
|
|
|
|
dialOpts = append(dialOpts, grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(stream...)))
|
2018-01-06 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 20:58:21 +00:00
|
|
|
cc, err := grpc.DialContext(ctx, "localhost", dialOpts...)
|
2017-07-13 03:45:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "failed to create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
go monitorHealth(ctx, cc, cancel)
|
|
|
|
|
|
|
|
return ctx, cc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func monitorHealth(ctx context.Context, cc *grpc.ClientConn, cancelConn func()) {
|
|
|
|
defer cancelConn()
|
|
|
|
defer cc.Close()
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
2017-07-13 03:45:53 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
healthClient := grpc_health_v1.NewHealthClient(cc)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2018-01-06 16:54:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
2017-07-13 03:45:53 +00:00
|
|
|
_, err := healthClient.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
|
|
|
|
cancel()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|