Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.modify the record method of the number of clients accessing the ser… #205

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (server *Server) loadRdbFile() error {
defer func() {
_ = rdbFile.Close()
}()
decoder := rdb.NewDecoder(rdbFile)
decoder := rdb.NewDecoder(rdbFile) // 创建一个新的RDB解码器
err = server.LoadRDB(decoder)
if err != nil {
return fmt.Errorf("load rdb file failed " + err.Error())
Expand Down
3 changes: 3 additions & 0 deletions database/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ func NewStandaloneServer() *Server {
singleDB := makeDB()
singleDB.index = i
holder := &atomic.Value{}
// set init value
holder.Store(singleDB)
server.dbSet[i] = holder
}
server.hub = pubsub.MakeHub()
// record aof
validAof := false
// default value is false
if config.Properties.AppendOnly {
validAof = fileExists(config.Properties.AppendFilename)
aofHandler, err := NewPersister(server,
Expand All @@ -77,6 +79,7 @@ func NewStandaloneServer() *Server {
}
server.bindPersister(aofHandler)
}
// dbfilename value is test.rdb
if config.Properties.RDBFilename != "" && !validAof {
// load rdb
err := server.loadRdbFile()
Expand Down
1 change: 1 addition & 0 deletions redis/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (h *Handler) Handle(ctx context.Context, conn net.Conn) {
return
}

// reuse link object in sync.Pool
client := connection.NewConn(conn)
h.activeConn.Store(client, struct{}{})

Expand Down
9 changes: 7 additions & 2 deletions tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {

// ClientCounter Record the number of clients in the current Godis server
var ClientCounter int
var mu sync.Mutex

// ListenAndServeWithSignal binds port and handle requests, blocking until receive stop signal
func ListenAndServeWithSignal(cfg *Config, handler tcp.Handler) error {
Expand Down Expand Up @@ -66,7 +67,11 @@ func ListenAndServe(listener net.Listener, handler tcp.Handler, closeChan <-chan
_ = listener.Close() // listener.Accept() will return err immediately
_ = handler.Close() // close connections
}()

increment := func() {
mu.Lock()
defer mu.Unlock()
ClientCounter--
}
ctx := context.Background()
var waitDone sync.WaitGroup
for {
Expand All @@ -87,8 +92,8 @@ func ListenAndServe(listener net.Listener, handler tcp.Handler, closeChan <-chan
waitDone.Add(1)
go func() {
defer func() {
increment()
waitDone.Done()
ClientCounter--
}()
handler.Handle(ctx, conn)
}()
Expand Down