Skip to content

Incorrect lock used in Since/Until methods of Simulator #1

@nnikolash

Description

@nnikolash

Since and Until methods of simulator are using exclusive lock mode, which is not required here:

// Since returns the time elapsed since t.
func (s *Simulator) Since(t time.Time) time.Duration {
	s.Lock()
	defer s.Unlock()
	return s.now.Sub(t)
}

// Until returns the duration until t.
func (s *Simulator) Until(t time.Time) time.Duration {
	s.Lock()
	defer s.Unlock()
	return t.Sub(s.now)
}

It can be replaced with shared lock, as done in Now method:

// Now returns the current time.
func (s *Simulator) Now() time.Time {
	s.RLock()
	defer s.RUnlock()
	return s.now
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions