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

Modify code comments and unnecessary type conversions #210

Merged
merged 3 commits into from
Feb 14, 2024
Merged
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
4 changes: 2 additions & 2 deletions datastruct/dict/concurrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (dict *ConcurrentDict) PutIfAbsentWithLock(key string, val interface{}) (re
return 1
}

// PutIfExists puts value if the key is exist and returns the number of inserted key-value
// PutIfExists puts value if the key is existed and returns the number of inserted key-value
func (dict *ConcurrentDict) PutIfExists(key string, val interface{}) (result int) {
if dict == nil {
panic("dict is nil")
Expand Down Expand Up @@ -262,7 +262,7 @@ func (dict *ConcurrentDict) decreaseCount() int32 {
}

// ForEach traversal the dict
// it may not visits new entry inserted during traversal
// it may not visit new entry inserted during traversal
func (dict *ConcurrentDict) ForEach(consumer Consumer) {
if dict == nil {
panic("dict is nil")
Expand Down
2 changes: 1 addition & 1 deletion datastruct/dict/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (dict *SimpleDict) PutIfAbsent(key string, val interface{}) (result int) {
return 1
}

// PutIfExists puts value if the key is exist and returns the number of inserted key-value
// PutIfExists puts value if the key is existed and returns the number of inserted key-value
func (dict *SimpleDict) PutIfExists(key string, val interface{}) (result int) {
_, existed := dict.m[key]
if existed {
Expand Down
6 changes: 3 additions & 3 deletions datastruct/lock/lock_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (locks *Locks) spread(hashCode uint32) uint32 {
panic("dict is nil")
}
tableSize := uint32(len(locks.table))
return (tableSize - 1) & uint32(hashCode)
return (tableSize - 1) & hashCode
}

// Lock obtains exclusive lock for writing
Expand Down Expand Up @@ -90,7 +90,7 @@ func (locks *Locks) toLockIndices(keys []string, reverse bool) []uint32 {
}

// Locks obtains multiple exclusive locks for writing
// invoking Lock in loop may cause dead lock, please use Locks
// invoking Lock in loop may cause deadlock, please use Locks
func (locks *Locks) Locks(keys ...string) {
indices := locks.toLockIndices(keys, false)
for _, index := range indices {
Expand All @@ -100,7 +100,7 @@ func (locks *Locks) Locks(keys ...string) {
}

// RLocks obtains multiple shared locks for reading
// invoking RLock in loop may cause dead lock, please use RLocks
// invoking RLock in loop may cause deadlock, please use RLocks
func (locks *Locks) RLocks(keys ...string) {
indices := locks.toLockIndices(keys, false)
for _, index := range indices {
Expand Down
6 changes: 3 additions & 3 deletions datastruct/sortedset/sortedset.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (sortedSet *SortedSet) GetRank(member string, desc bool) (rank int64) {

// ForEachByRank visits each member which rank within [start, stop), sort by ascending order, rank starts from 0
func (sortedSet *SortedSet) ForEachByRank(start int64, stop int64, desc bool, consumer func(element *Element) bool) {
size := int64(sortedSet.Len())
size := sortedSet.Len()
if start < 0 || start >= size {
panic("illegal start " + strconv.FormatInt(start, 10))
}
Expand All @@ -91,12 +91,12 @@ func (sortedSet *SortedSet) ForEachByRank(start int64, stop int64, desc bool, co
if desc {
node = sortedSet.skiplist.tail
if start > 0 {
node = sortedSet.skiplist.getByRank(int64(size - start))
node = sortedSet.skiplist.getByRank(size - start)
}
} else {
node = sortedSet.skiplist.header.level[0].forward
if start > 0 {
node = sortedSet.skiplist.getByRank(int64(start + 1))
node = sortedSet.skiplist.getByRank(start + 1)
}
}

Expand Down
35 changes: 35 additions & 0 deletions tcp/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,38 @@ func TestListenAndServe(t *testing.T) {
closeChan <- struct{}{}
time.Sleep(time.Second)
}

func TestClientCounter(t *testing.T) {
var err error
closeChan := make(chan struct{})
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Error(err)
return
}
addr := listener.Addr().String()
go ListenAndServe(listener, MakeEchoHandler(), closeChan)

sleepUntil := time.Now().Add(3 * time.Second)
subtime := func() time.Duration {
return sleepUntil.Sub(time.Now())
}

for i := 0; i < 1000; i++ {
go func() {
conn, err := net.Dial("tcp", addr)
if err != nil {
t.Errorf(err.Error())
}
defer conn.Close()

time.Sleep(subtime())
}()
time.Sleep(5 * time.Microsecond)
}

time.Sleep(3 * time.Second)
if ClientCounter != 0 {
t.Errorf("client counter error: %d", ClientCounter)
}
}
5 changes: 3 additions & 2 deletions tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"

Expand All @@ -26,7 +27,7 @@ type Config struct {
}

// ClientCounter Record the number of clients in the current Godis server
var ClientCounter int
var ClientCounter int32

// ListenAndServeWithSignal binds port and handle requests, blocking until receive stop signal
func ListenAndServeWithSignal(cfg *Config, handler tcp.Handler) error {
Expand Down Expand Up @@ -88,7 +89,7 @@ func ListenAndServe(listener net.Listener, handler tcp.Handler, closeChan <-chan
go func() {
defer func() {
waitDone.Done()
ClientCounter--
atomic.AddInt32(&ClientCounter, -1)
}()
handler.Handle(ctx, conn)
}()
Expand Down