Skip to content

Commit

Permalink
fix shutdown process
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Dec 14, 2023
1 parent cafebe6 commit e2c8b68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
39 changes: 21 additions & 18 deletions src/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,20 @@ func (cr *Cluster) Enable() (err error) {
cr.enabled = true

var keepaliveCtx context.Context
keepaliveCtx, cr.keepalive = context.WithCancel(context.TODO())
keepaliveCtx, cr.keepalive = context.WithCancel(cr.ctx)
createInterval(keepaliveCtx, func() {
cr.KeepAlive()
if !cr.KeepAlive() {
logDebug("Reconnecting due to keepalive failed")
cr.Disable()
if !cr.Connect() {
logError("Cannot reconnect to server, exit.")
os.Exit(1)
}
if err := cr.Enable(); err != nil {
logError("Cannot enable cluster:", err, "; exit.")
os.Exit(1)
}
}
}, KeepAliveInterval)
return
}
Expand All @@ -196,27 +207,19 @@ func (cr *Cluster) KeepAlive() (ok bool) {
logError("Error when keep-alive:", err)
return false
}
if len(data) > 1 && data.Get(0) == nil {
logInfo("Keep-alive success:", hits, bytesToUnit((float64)(hbytes)), data)
} else {
logInfo("Keep-alive failed:", data.Get(0))
cr.Disable()
if !cr.Connect() {
logError("Cannot reconnect to server, exit.")
os.Exit(1)
}
if err := cr.Enable(); err != nil {
logError("Cannot enable cluster:", err, "; exit.")
os.Exit(1)
}
if erro := data.Get(0); len(data) <= 1 || erro != nil {
logError("Keep-alive failed:", erro)
return false
}
logInfo("Keep-alive success:", hits, bytesToUnit((float64)(hbytes)), data)
return true
}

func (cr *Cluster) Disable() (successed bool) {
func (cr *Cluster) Disable() (ok bool) {
cr.mux.Lock()
defer cr.mux.Unlock()

cr.KeepAlive()
if !cr.enabled {
logDebug("Extra disable")
return true
Expand All @@ -236,7 +239,7 @@ func (cr *Cluster) Disable() (successed bool) {
if err != nil {
return false
}
logInfo("disable ack:", data)
logDebug("disable ack:", data)
if !data.GetBool(1) {
logError("Disable failed: " + data.String())
return false
Expand Down Expand Up @@ -270,7 +273,7 @@ func (pair *CertKeyPair) SaveAsFile() (cert, key string, err error) {
}

func (cr *Cluster) RequestCert() (ckp *CertKeyPair, err error) {
logInfo("Requesting cert, please wait ...")
logInfo("Requesting certificates, please wait ...")
data, err := cr.socket.EmitAck("request-cert")
if err != nil {
return
Expand Down
5 changes: 2 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,13 @@ START:
case <-exitCh:
return
case s := <-signalCh:
shutCtx, cancelShut := context.WithTimeout(bgctx, 16*time.Second)
shutCtx, _ := context.WithTimeout(ctx, 16*time.Second)
logWarn("Closing server ...")
cancel()
shutExit := make(chan struct{}, 0)
go hjServer.Shutdown(shutCtx)
go func() {
defer close(shutExit)
defer cancelShut()
defer cancel()
cluster.Disable()
cluster.Server.Shutdown(shutCtx)
}()
Expand Down

0 comments on commit e2c8b68

Please sign in to comment.