hotfix: cleanup resource if daemon rocess be killed

This commit is contained in:
naison
2024-12-07 06:43:48 +00:00
parent d9d4091905
commit b5ac5b7d9a
2 changed files with 35 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package cmds
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
@@ -17,6 +18,7 @@ import (
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/action"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
"github.com/wencaiwulue/kubevpn/v2/pkg/dns"
"github.com/wencaiwulue/kubevpn/v2/pkg/util"
)
@@ -45,6 +47,22 @@ func CmdDaemon(_ cmdutil.Factory) *cobra.Command {
return initLogfile(action.GetDaemonLogPath())
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer func() {
var cli rpc.DaemonClient
if !opt.IsSudo {
cli = daemon.GetClient(true)
} else {
cli = daemon.GetClient(false)
}
if cli == nil {
return
}
resp, err := cli.Quit(context.Background(), &rpc.QuitRequest{})
if err != nil {
return
}
_ = util.PrintGRPCStream[rpc.QuitResponse](resp, nil)
}()
defer opt.Stop()
defer func() {
if errors.Is(err, http.ErrServerClosed) {

View File

@@ -50,12 +50,12 @@ func (o *SvrOption) Start(ctx context.Context) error {
LocalTime: true,
Compress: false,
}
// for gssapi to lookup KDCs in DNS
// c.LibDefaults.DNSLookupKDC = true
// c.LibDefaults.DNSLookupRealm = true
net.DefaultResolver.PreferGo = true
util.InitLoggerForServer(true)
log.SetOutput(l)
klog.SetOutput(l)
@@ -102,7 +102,11 @@ func (o *SvrOption) Start(ctx context.Context) error {
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
// startup a http server
// With downgrading-capable gRPC server, which can also handle HTTP.
downgradingServer := &http.Server{}
downgradingServer := &http.Server{
BaseContext: func(listener net.Listener) context.Context {
return o.ctx
},
}
defer downgradingServer.Close()
var h2Server http2.Server
err = http2.ConfigureServer(downgradingServer, &h2Server)
@@ -122,7 +126,16 @@ func (o *SvrOption) Start(ctx context.Context) error {
}
o.svr = &action.Server{Cancel: cancel, IsSudo: o.IsSudo, GetClient: GetClient, LogFile: l, ID: o.ID}
rpc.RegisterDaemonServer(svr, o.svr)
return downgradingServer.Serve(lis)
var errChan = make(chan error)
go func() {
errChan <- downgradingServer.Serve(lis)
}()
select {
case err = <-errChan:
return err
case <-o.ctx.Done():
return nil
}
//return o.svr.Serve(lis)
}