修改server端的析构逻辑

This commit is contained in:
lwch
2021-08-26 14:31:30 +08:00
parent ecfbc0d14d
commit 6602b69504
3 changed files with 15 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ type client struct {
}
func (c *client) run() {
defer c.parent.parent.closeClient(c)
for {
if time.Since(c.updated).Seconds() > 600 {
links := make([]string, 0, len(c.links))
@@ -28,7 +29,6 @@ func (c *client) run() {
}
c.RUnlock()
logging.Info("%s-%d is not keepalived, links: %v", c.parent.id, c.idx, links)
c.parent.parent.closeClient(c)
return
}
msg, err := c.conn.ReadMessage(c.parent.parent.cfg.ReadTimeout)
@@ -70,7 +70,7 @@ func (c *client) getLinks() []string {
return ret
}
func (c *client) close(id string) {
func (c *client) closeLink(id string) {
var msg network.Msg
msg.From = "server"
msg.To = c.parent.id

View File

@@ -55,4 +55,7 @@ func (cs *clients) close(idx uint32) {
cs.Lock()
delete(cs.data, idx)
cs.Unlock()
if len(cs.data) == 0 {
cs.parent.removeClients(cs.id)
}
}

View File

@@ -185,10 +185,10 @@ func (h *Handler) closeClient(cli *client) {
pair := h.links[t]
h.lockLinks.RUnlock()
if pair[0] != nil {
pair[0].close(t)
pair[0].closeLink(t)
}
if pair[1] != nil {
pair[1].close(t)
pair[1].closeLink(t)
}
h.lockLinks.Lock()
delete(h.links, t)
@@ -197,10 +197,13 @@ func (h *Handler) closeClient(cli *client) {
h.lockClients.RLock()
clients := h.clients[cli.parent.id]
h.lockClients.RUnlock()
if clients != nil {
clients.close(cli.idx)
if len(clients.data) == 0 {
h.lockClients.Lock()
delete(h.clients, clients.id)
h.lockClients.Unlock()
}
}
func (h *Handler) removeClients(id string) {
h.lockClients.Lock()
delete(h.clients, id)
h.lockClients.Unlock()
}