migrating rpc command to package.

This commit is contained in:
greg stone
2023-02-23 11:40:28 +00:00
parent 23ed87ceb0
commit d2f2ca7911
5 changed files with 82 additions and 54 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/tutorialedge/go-grpc-tutorial/chat"
"google.golang.org/grpc"
"os"
"time"
)
var (
@@ -51,36 +52,37 @@ var seedCmd = &cobra.Command{
// RPC
//
if viper.GetBool("rpc-enable") {
rpc.RunWith(ctx, func(srv *grpc.Server) {
chat.RegisterChatServiceServer(srv, &chat.Server{})
})
log.I.Ln("enabling rpc server")
select {
case <-rpc.CantStart():
if err = rpc.ConfigureWithViper(); check(err) {
os.Exit(1)
}
log.I.Ln("issues starting the rpc server")
log.I.Ln("attempting a graceful shutdown")
rpc.Register(func(srv *grpc.Server) {
chat.RegisterChatServiceServer(srv, &chat.Server{})
})
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
log.I.Ln("starting rpc server")
go rpc.Start(ctx)
rpc.Shutdown(ctx)
select {
case <-rpc.CantStart():
case <-ctx.Done():
log.I.Ln("issues starting the rpc server")
log.I.Ln("attempting a graceful shutdown")
rpc.Shutdown()
log.I.Ln("can't shutdown gracefully, exiting.")
os.Exit(1)
case <-rpc.IsReady():
default:
log.I.Ln("rpc server is ready!")
log.I.Ln("graceful shutdown complete")
os.Exit(0)
}
case <-rpc.IsReady():
log.I.Ln("rpc server is ready!")
}
//

View File

@@ -1,22 +1,37 @@
package rpc
import (
"context"
"github.com/davecgh/go-spew/spew"
"github.com/spf13/viper"
"google.golang.org/grpc"
"os"
)
func ConfigureWithViper() (err error) {
func RunWith(ctx context.Context, r func(srv *grpc.Server)) {
log.I.Ln("initializing the rpc server")
var err error
if err = configureWithViper(); check(err) {
os.Exit(1)
}
r(server)
log.I.Ln("starting rpc server")
go Start(ctx)
}
func configureWithViper() (err error) {
log.I.Ln("configuring the rpc server")
configureUnixSocket()
configureTunnel()
log.I.Ln("rpc listeners:")
log.I.F("- [/ip4/0.0.0.0/udp/%d", devicePort)
log.I.F("/ip4/0.0.0.0/udp/%d", devicePort)
log.I.F("/ip6/:::/udp/%d", devicePort)
log.I.F("/unix" + unixPath + "]")
return
}
@@ -26,53 +41,64 @@ func configureUnixSocket() {
return
}
log.I.Ln("enabling unix listener:", viper.GetString(unixPath))
log.I.F("enabling rpc unix listener [/unix%s]", viper.GetString(unixPathFlag))
isUnixSockEnabled = true
}
func configureTunnel() {
if !viper.GetBool("rpc-tun-enable") {
if !viper.GetBool(tunEnableFlag) {
log.I.Ln("disabling rpc tunnel")
return
}
enableTunnel()
log.I.Ln("enabling rpc tunnel")
configureTunnelKey()
configureTunnelPort()
configurePeerWhitelist()
enableTunnel()
spew.Dump(viper.AllSettings())
os.Exit(0)
}
func configureTunnelKey() {
if viper.GetString("rpc-tun-key") == "" {
if viper.GetString(tunKeyFlag) == "" {
log.I.Ln("rpc tunnel key not provided, generating a new one.")
tunKey, _ = NewPrivateKey()
viper.Set("rpc-tun-key", tunKey.Encode())
viper.Set(tunKeyFlag, tunKey.Encode())
}
tunKey = &RPCPrivateKey{}
tunKey.Decode(viper.GetString(tunKeyFlag))
log.I.Ln("rpc public key:")
log.I.Ln("-", tunKey.PubKey().Encode())
}
func configureTunnelPort() {
if viper.GetUint16("rpc-tun-port") == NullPort {
log.I.Ln("rpc tunnel port not provided, generating a random one.")
viper.Set("rpc-tun-port", genRandomPort(10000))
if viper.GetUint16(tunPortFlag) != NullPort {
return
}
log.I.Ln("rpc tunnel port not provided, generating a random one.")
viper.Set(tunPortFlag, genRandomPort(10000))
}
func configurePeerWhitelist() {
for _, peer := range viper.GetStringSlice("rpc-whitelist-peer") {
for _, peer := range viper.GetStringSlice(tunPeersFlag) {
var pubKey RPCPublicKey

View File

@@ -46,11 +46,13 @@ func Start(ctx context.Context) {
select {
case <-ctx.Done():
Shutdown()
Shutdown(context.Background())
}
}
func Shutdown() {
func Shutdown(ctx context.Context) {
defer ctx.Done()
log.I.Ln("shutting down rpc server")

View File

@@ -3,6 +3,7 @@ package rpc
import (
"google.golang.org/grpc"
"net"
"os"
)
var (
@@ -17,7 +18,7 @@ func startUnixSocket(srv *grpc.Server) (err error) {
return
}
if unixSock, err = net.Listen("unix", unixPath); check(err) {
if unixSock, err = net.Listen("unix", unixPath); err != nil {
return
}
@@ -32,13 +33,13 @@ func stopUnixSocket() (err error) {
return
}
if err = unixSock.Close(); check(err) {
// continue
if unixSock != nil {
if err = unixSock.Close(); check(err) {
// continue
}
}
//if err = os.Remove(unixPath); check(err) {
// // continue
//}
os.Remove(unixPath)
return
}

View File

@@ -2,7 +2,6 @@ package seed
import (
"context"
"git-indra.lan/indra-labs/indra/pkg/rpc"
"time"
"github.com/libp2p/go-libp2p"
@@ -42,8 +41,6 @@ func (srv *Server) Restart() (err error) {
func (srv *Server) Shutdown() (err error) {
log.I.Ln("shutting down [p2p.host]")
if err = srv.host.Close(); check(err) {
// continue
}
@@ -76,13 +73,13 @@ func (srv *Server) Serve() (err error) {
go metrics.HostStatus(ctx, srv.host)
var client *rpc.RPCClient
if client, err = rpc.NewClient(rpc.DefaultClientConfig); check(err) {
return err
}
client.Start()
//var client *rpc.RPCClient
//
//if client, err = rpc.NewClient(rpc.DefaultClientConfig); check(err) {
// return err
//}
//
//client.Start()
select {