2019-01-02 10:23:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-02-04 14:50:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-01-02 10:23:25 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-01-02 10:23:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents a specific gRPC method collection to be registered
|
2019-04-25 09:46:32 +01:00
|
|
|
// on a shared gRPC server. Metainfo, OverlayCache, PieceStore, Kademlia,
|
2019-03-25 22:25:09 +00:00
|
|
|
// etc. are all examples of services.
|
2019-01-02 10:23:25 +00:00
|
|
|
type Service interface {
|
|
|
|
Run(ctx context.Context, server *Server) error
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
type public struct {
|
|
|
|
listener net.Listener
|
|
|
|
grpc *grpc.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
type private struct {
|
|
|
|
listener net.Listener
|
|
|
|
grpc *grpc.Server
|
|
|
|
}
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
// Server represents a bundle of services defined by a specific ID.
|
|
|
|
// Examples of servers are the satellite, the storagenode, and the uplink.
|
|
|
|
type Server struct {
|
2019-03-07 18:19:37 +00:00
|
|
|
public public
|
|
|
|
private private
|
2019-01-02 10:23:25 +00:00
|
|
|
next []Service
|
|
|
|
identity *identity.FullIdentity
|
|
|
|
}
|
|
|
|
|
2019-01-28 15:04:53 +00:00
|
|
|
// New creates a Server out of an Identity, a net.Listener,
|
2019-01-02 10:23:25 +00:00
|
|
|
// a UnaryServerInterceptor, and a set of services.
|
2019-03-07 18:19:37 +00:00
|
|
|
func New(opts *tlsopts.Options, publicAddr, privateAddr string, interceptor grpc.UnaryServerInterceptor, services ...Service) (*Server, error) {
|
2019-06-04 13:55:24 +01:00
|
|
|
unaryInterceptor := logOnErrorUnaryInterceptor
|
2019-01-02 10:23:25 +00:00
|
|
|
if interceptor != nil {
|
2019-06-04 13:55:24 +01:00
|
|
|
unaryInterceptor = CombineInterceptors(unaryInterceptor, interceptor)
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
publicListener, err := net.Listen("tcp", publicAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
public := public{
|
|
|
|
listener: publicListener,
|
2019-01-02 10:23:25 +00:00
|
|
|
grpc: grpc.NewServer(
|
2019-06-04 13:55:24 +01:00
|
|
|
grpc.StreamInterceptor(logOnErrorStreamInterceptor),
|
2019-01-02 10:23:25 +00:00
|
|
|
grpc.UnaryInterceptor(unaryInterceptor),
|
2019-02-11 11:17:32 +00:00
|
|
|
opts.ServerOption(),
|
2019-01-02 10:23:25 +00:00
|
|
|
),
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
privateListener, err := net.Listen("tcp", privateAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, publicListener.Close())
|
|
|
|
}
|
|
|
|
private := private{
|
|
|
|
listener: privateListener,
|
|
|
|
grpc: grpc.NewServer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
2019-01-02 10:23:25 +00:00
|
|
|
next: services,
|
|
|
|
identity: opts.Ident,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identity returns the server's identity
|
|
|
|
func (p *Server) Identity() *identity.FullIdentity { return p.identity }
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
// Addr returns the server's public listener address
|
|
|
|
func (p *Server) Addr() net.Addr { return p.public.listener.Addr() }
|
|
|
|
|
|
|
|
// PrivateAddr returns the server's private listener address
|
|
|
|
func (p *Server) PrivateAddr() net.Addr { return p.private.listener.Addr() }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
|
|
|
// GRPC returns the server's gRPC handle for registration purposes
|
2019-03-07 18:19:37 +00:00
|
|
|
func (p *Server) GRPC() *grpc.Server { return p.public.grpc }
|
|
|
|
|
|
|
|
// PrivateGRPC returns the server's gRPC handle for registration purposes
|
|
|
|
func (p *Server) PrivateGRPC() *grpc.Server { return p.private.grpc }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
|
|
|
// Close shuts down the server
|
|
|
|
func (p *Server) Close() error {
|
2019-03-07 18:19:37 +00:00
|
|
|
p.public.grpc.GracefulStop()
|
|
|
|
p.private.grpc.GracefulStop()
|
2019-01-02 10:23:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run will run the server and all of its services
|
|
|
|
func (p *Server) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// are there any unstarted services? start those first. the
|
|
|
|
// services should know to call Run again once they're ready.
|
|
|
|
if len(p.next) > 0 {
|
|
|
|
next := p.next[0]
|
|
|
|
p.next = p.next[1:]
|
|
|
|
return next.Run(ctx, p)
|
|
|
|
}
|
|
|
|
|
2019-02-04 14:50:55 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
|
|
|
return p.Close()
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-02-06 13:19:14 +00:00
|
|
|
defer cancel()
|
2019-03-07 18:19:37 +00:00
|
|
|
return p.public.grpc.Serve(p.public.listener)
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
return p.private.grpc.Serve(p.private.listener)
|
2019-02-04 14:50:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|