Skip to content

Commit

Permalink
修复死锁的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
injoyai committed Dec 14, 2023
1 parent eefbc59 commit 3f9e496
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 1 addition & 3 deletions io_client_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,7 @@ func (this *ClientManage) CloseClient(key string) error {

// CloseClientAll 关闭所有客户端
func (this *ClientManage) CloseClientAll() {
this.mu.Lock()
defer this.mu.Unlock()
for _, v := range this.m {
for _, v := range this.CopyClientMap() {
v.CloseAll()
}
this.m = make(map[string]*Client)
Expand Down
15 changes: 12 additions & 3 deletions io_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func RunServer(newListen ListenFunc, options ...OptionServer) error {
return s.Run()
}

// NewServer 定义Listen,不用忘记运行Run,不要回出现能连接,服务无反应的情况
func NewServer(newListen ListenFunc, options ...OptionServer) (*Server, error) {
return NewServerWithContext(context.Background(), newListen, options...)
}
Expand All @@ -36,10 +37,9 @@ func NewServerWithContext(ctx context.Context, newListen func() (Listener, error
tag: maps.NewSafe(),
listener: listener,
}
s.ICloser.Logger = s.Logger
s.ClientManage.Logger = s.Logger
s.SetLogger(s.Logger)
//开启基础信息打印
s.Logger.Debug()
s.Debug()
//设置关闭函数
s.ICloser.SetCloseFunc(func(ctx context.Context, msg Message) {
//关闭listener
Expand Down Expand Up @@ -70,9 +70,18 @@ type Server struct {

func (this *Server) Debug(b ...bool) {
this.Logger.Debug(b...)
this.ICloser.Logger.Debug(b...)
this.ClientManage.Logger.Debug(b...)
}

func (this *Server) SetLogger(logger Logger) *Server {
l := newLogger(logger)
this.Logger = l
this.ICloser.Logger = l
this.ClientManage.Logger = l
return this
}

func (this *Server) Tag() *maps.Safe {
return this.tag
}
Expand Down
21 changes: 21 additions & 0 deletions testdata/testserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/injoyai/io"
"github.com/injoyai/io/listen"
"github.com/injoyai/logs"
"time"
)

func main() {

listen.RunTCPServer(10086, func(s *io.Server) {
go func() {
for {
<-time.After(time.Second * 5)
logs.Debug(s.GetClientLen())
}
}()
})

}

0 comments on commit 3f9e496

Please sign in to comment.