Skip to content

Commit

Permalink
remove unnecessary sleeps
Browse files Browse the repository at this point in the history
  • Loading branch information
nitwhiz committed Aug 22, 2024
1 parent faa8d93 commit 49e2165
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
10 changes: 2 additions & 8 deletions pkg/lock/lock_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ func (t *Table) Lock(ctx context.Context, name string, clientId uint64) bool {
result := false

wg := &sync.WaitGroup{}
isRunning := false

wg.Add(1)

go func() {
wg.Add(1)
defer wg.Done()

isRunning = true

for {
select {
case <-ctx.Done():
Expand All @@ -52,10 +50,6 @@ func (t *Table) Lock(ctx context.Context, name string, clientId uint64) bool {
}
}()

for !isRunning {
<-time.After(time.Millisecond)
}

wg.Wait()

return result
Expand Down
7 changes: 3 additions & 4 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net"
"sync"
"time"
)

type Server struct {
Expand Down Expand Up @@ -54,7 +53,6 @@ func New(ctx context.Context, opts ...Option) (*Server, error) {
}

func (s *Server) acceptor() {
s.wg.Add(1)
defer s.wg.Done()

for {
Expand Down Expand Up @@ -104,16 +102,17 @@ func (s *Server) wait() {
}

func (s *Server) Run() {
s.wg.Add(1)

go s.startCommandListener()

for i := 0; i < s.options.acceptorCount; i++ {
s.wg.Add(1)
go s.acceptor()
}

log.Println("Ready to serve connections")

<-time.After(time.Millisecond)

s.wait()
}

Expand Down
1 change: 0 additions & 1 deletion pkg/server/server_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (s *Server) handleCommand(cmd *client.Command) {
}

func (s *Server) startCommandListener() {
s.wg.Add(1)
defer s.wg.Done()

for {
Expand Down
10 changes: 4 additions & 6 deletions test/integration/server_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ func TestLockWithTimeoutAndConcurrency(t *testing.T) {

wg2 := &sync.WaitGroup{}

go func() {
wg2.Add(1)
wg2.Add(1)

go func() {
conn1 := connect(t, serverAddr)

defer func(conn *net.TCPConn) {
Expand Down Expand Up @@ -247,9 +247,9 @@ func TestLockWithTimeoutAndConcurrency(t *testing.T) {
time.Sleep(time.Millisecond * 250)
}()

go func() {
wg2.Add(1)
wg2.Add(1)

go func() {
conn1 := connect(t, serverAddr)

defer func(conn *net.TCPConn) {
Expand Down Expand Up @@ -282,7 +282,5 @@ func TestLockWithTimeoutAndConcurrency(t *testing.T) {
}
}()

time.Sleep(time.Millisecond)

wg2.Wait()
}

0 comments on commit 49e2165

Please sign in to comment.