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

Guard raceable properties with mutex, fixes #19 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 36 additions & 5 deletions stk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/url"
"strconv"
"sync"
"time"

statuscake "github.com/mtulio/statuscake"
Expand All @@ -22,9 +23,16 @@ type StkAPI struct {
waitIntervalSec uint32
EnableTests bool
EnableTestsSSL bool
Tests []*statuscake.Test
TestsSSL []*statuscake.Ssl
controlInit bool

testsMut sync.Mutex
Tests []*statuscake.Test

testsSSLMut sync.Mutex
TestsSSL []*statuscake.Ssl

cmut sync.Mutex
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmut sync.Mutex
cInitMut sync.Mutex

controlInit bool

sslFlagsEnabled map[string]bool
}

Expand Down Expand Up @@ -64,12 +72,24 @@ func (stk *StkAPI) GetTags() string {

// GetTests return all StatusCake Tests from last API discovery.
func (stk *StkAPI) GetTests() []*statuscake.Test {
return stk.Tests
stk.testsMut.Lock()
defer stk.testsMut.Unlock()

ret := make([]*statuscake.Test, len(stk.Tests))
copy(ret, stk.Tests)

return ret
}

// GetTestsSSL return all StatusCake SSL Tests from last API discovery.
func (stk *StkAPI) GetTestsSSL() []*statuscake.Ssl {
return stk.TestsSSL
stk.testsSSLMut.Lock()
defer stk.testsSSLMut.Unlock()

ret := make([]*statuscake.Ssl, len(stk.TestsSSL))
copy(ret, stk.TestsSSL)

return ret
}

// SetWaitInterval define API data scrape wait internval.
Expand Down Expand Up @@ -110,18 +130,24 @@ func (stk *StkAPI) gatherTest() {
if err != nil {
log.Println(err)
}
stk.testsMut.Lock()
stk.Tests = tests
stk.cmut.Lock()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stk.cmut.Lock()
stk.cInitMut.Lock()

if !stk.controlInit {
log.Println(" Initial API discovery returns the Total of Tests:", len(tests))
stk.controlInit = true
}
stk.cmut.Unlock()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stk.cmut.Unlock()
stk.cInitMut.Unlock()

stk.testsMut.Unlock()
time.Sleep(time.Second * time.Duration(stk.waitIntervalSec))
}
}

func (stk *StkAPI) gatherTestsData() {
for {
stk.testsMut.Lock()
if len(stk.Tests) <= 0 {
stk.testsMut.Unlock()
time.Sleep(time.Second * 10)
continue
}
Expand All @@ -139,6 +165,7 @@ func (stk *StkAPI) gatherTestsData() {
}
test.PerformanceData = perfData
}
stk.testsMut.Unlock()
time.Sleep(time.Second * time.Duration(stk.waitIntervalSec))
}
}
Expand All @@ -150,10 +177,14 @@ func (stk *StkAPI) gatherTestsSSL() {
if err != nil {
log.Println(err)
}
stk.testsSSLMut.Lock()
stk.TestsSSL = ssls
stk.cmut.Lock()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stk.cmut.Lock()
stk.cInitMut.Lock()

if !stk.controlInit {
log.Println(" Initial API discovery returns the Total of SSL Tests:", len(stk.TestsSSL))
}
stk.cmut.Unlock()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stk.cmut.Unlock()
stk.cInitMut.Unlock()

stk.testsSSLMut.Unlock()
time.Sleep(time.Second * time.Duration(stk.waitIntervalSec))
}
}
Expand Down