Skip to content

Commit

Permalink
ref: handle lock/unlock better
Browse files Browse the repository at this point in the history
Handle Unlocking right after finish working with list
  • Loading branch information
alipourhabibi committed Aug 24, 2024
1 parent b3bd840 commit f98fd15
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions api/v2/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func (s *server) Insert(c echo.Context) error {
}

s.mutex.Lock()
defer s.mutex.Unlock()

ok := s.list.Insert(data.Index, data.Value)
s.mutex.Unlock()

if !ok {
return echo.NewHTTPError(echo.ErrBadRequest.Code, "Invalid index")
}
Expand All @@ -114,9 +114,9 @@ func (s *server) Remove(c echo.Context) error {
}

s.mutex.Lock()
defer s.mutex.Unlock()

ok := s.list.Remove(uint(index))
s.mutex.Unlock()

if !ok {
return echo.NewHTTPError(echo.ErrNotFound.Code, "Index not found")
}
Expand All @@ -134,9 +134,9 @@ func (s *server) Find(c echo.Context) error {
}

s.mutex.Lock()
defer s.mutex.Unlock()

index, ok := s.list.Find(value)
s.mutex.Unlock()

if !ok {
return echo.NewHTTPError(echo.ErrNotFound.Code, "Value not found")
}
Expand All @@ -157,9 +157,9 @@ func (s *server) Get(c echo.Context) error {
}

s.mutex.Lock()
defer s.mutex.Unlock()

value, ok := s.list.Get(uint(index))
s.mutex.Unlock()

if !ok {
return echo.NewHTTPError(echo.ErrNotFound.Code, "Index not found")
}
Expand All @@ -181,9 +181,9 @@ func (s *server) RWMutexFind(c echo.Context) error {
}

s.mutex.RLock()
defer s.mutex.RUnlock()

index, ok := s.list.Find(value)
s.mutex.RUnlock()

if !ok {
return echo.NewHTTPError(echo.ErrNotFound.Code, "Value not found")
}
Expand All @@ -204,9 +204,9 @@ func (s *server) RWMutexGet(c echo.Context) error {
}

s.mutex.RLock()
defer s.mutex.RUnlock()

value, ok := s.list.Get(uint(index))
s.mutex.RUnlock()

if !ok {
return echo.NewHTTPError(echo.ErrNotFound.Code, "Index not found")
}
Expand Down

0 comments on commit f98fd15

Please sign in to comment.