Skip to content

Commit 2efa5dd

Browse files
committed
sean feedback!
1 parent 873b06b commit 2efa5dd

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

ticktock.go renamed to main.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
const (
17-
TIMEOUT = 3 //seconds
17+
TIMEOUT = 3 * time.Second
1818
GITHUB = "https://status.github.com/api/status.json"
1919
CODECOV = "https://wdzsn5dlywj9.statuspage.io/api/v2/status.json"
2020
TRAVIS = "https://pnpcptp8xh9k.statuspage.io/api/v2/status.json"
@@ -40,20 +40,13 @@ func updaterInit() {
4040
ticker := time.NewTicker(1 * time.Minute)
4141
updateState(DataStore)
4242
go func() {
43-
for {
44-
select {
45-
case <-ticker.C:
46-
updateState(DataStore)
47-
}
43+
for _ = range ticker.C {
44+
updateState(DataStore)
4845
}
4946
}()
5047
}
5148

52-
func statusFetch(url string) (*[]byte, error) {
53-
timeout := time.Duration(TIMEOUT * time.Second)
54-
client := http.Client{
55-
Timeout: timeout,
56-
}
49+
func statusFetch(url string, client http.Client) ([]byte, error) {
5750
resp, err := client.Get(url)
5851
if err != nil {
5952
return nil, err
@@ -63,38 +56,44 @@ func statusFetch(url string) (*[]byte, error) {
6356
if err != nil {
6457
return nil, err
6558
}
66-
return &body, nil
59+
return body, nil
6760
}
6861

6962
func updateState(store *state.Store) {
70-
var wg sync.WaitGroup
63+
wg := sync.WaitGroup{}
64+
7165
sources := []string{GITHUB, TRAVIS, QUAY, CODECOV}
7266
// match the length of sources
73-
wg.Add(4)
67+
wg.Add(len(sources))
68+
69+
timeout := time.Duration(TIMEOUT)
70+
client := http.Client{
71+
Timeout: timeout,
72+
}
7473

7574
for _, url := range sources {
7675
go func(url string) {
7776
log.Printf("Started Fetching: %s", url)
7877
InflightStatus.Inc()
7978
defer InflightStatus.Dec()
8079
defer wg.Done()
81-
body, err := statusFetch(url)
80+
body, err := statusFetch(url, client)
8281
if err != nil {
8382
log.Printf("Error fetching: %s, %s", url, err)
8483
return
8584
}
8685
log.Printf("Succeeded fetching: %s", url)
8786

88-
var good bool
87+
var parser parsing.Parser
8988
switch url {
9089
case GITHUB:
91-
source := parsing.GithubStatus{}
92-
good = source.Parse(body)
90+
parser = parsing.GithubParser
9391
case CODECOV, TRAVIS, QUAY:
94-
source := parsing.StatusPageStatus{}
95-
good = source.Parse(body)
92+
parser = parsing.StatusPageParser
93+
default:
94+
parser = parsing.DefaultParser
9695
}
97-
96+
good := parser(body)
9897
r := state.Refined{
9998
Good: good,
10099
SourceMessage: body,

parsing/parsing.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@ type GithubStatus struct {
2727
LastUpdated time.Time `json:"last_updated"`
2828
}
2929

30-
type Parser interface {
31-
Parse(body *[]byte) bool
32-
}
30+
type Parser func([]byte) bool
3331

34-
func (g *GithubStatus) Parse(body *[]byte) bool {
32+
func GithubParser(body []byte) bool {
3533
parsed := GithubStatus{}
36-
json.Unmarshal(*body, &parsed)
34+
json.Unmarshal(body, &parsed)
3735
return parsed.Status == "good"
3836
}
3937

40-
func (g *StatusPageStatus) Parse(body *[]byte) bool {
38+
func StatusPageParser(body []byte) bool {
4139
parsed := StatusPageStatus{
4240
Page: StatusPageInnerPage{},
4341
Status: StatusPageInnerStatus{},
4442
}
45-
json.Unmarshal(*body, &parsed)
43+
json.Unmarshal(body, &parsed)
4644
return parsed.Status.Indicator == "none"
4745
}
46+
47+
func DefaultParser(body []byte) bool {
48+
return false
49+
}

state/state.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ type Refined struct {
1010
Url string
1111
LastUpdated time.Time
1212
Good bool
13-
SourceMessage *[]byte
13+
SourceMessage []byte
1414
}
1515

1616
type Store struct {
17+
sync.RWMutex
1718
Statuses map[string]*Refined
1819
LastUpdated time.Time
19-
mutex *sync.RWMutex
2020
}
2121

2222
func NewStore() *Store {
2323
return &Store{
2424
LastUpdated: time.Now(),
2525
Statuses: make(map[string]*Refined),
26-
mutex: new(sync.RWMutex),
2726
}
2827
}
2928

@@ -33,35 +32,35 @@ type ReadWrite interface {
3332
CurrentValue() *Front
3433
}
3534

36-
func (s *Store) Read(url string) (*Refined, error) {
37-
s.mutex.RLock()
35+
func (s Store) Read(url string) (*Refined, error) {
36+
s.RLock()
3837
target, ok := s.Statuses[url]
39-
s.mutex.RUnlock()
38+
s.RUnlock()
4039
if ok == false {
4140
return nil, errors.New("URL not found")
4241
}
4342
return target, nil
4443
}
4544

46-
func (s *Store) Write(newStore *Refined) {
47-
s.mutex.Lock()
48-
s.Statuses[newStore.Url] = newStore
45+
func (s *Store) Write(status *Refined) {
46+
s.Lock()
47+
s.Statuses[status.Url] = status
4948
s.LastUpdated = time.Now()
50-
s.mutex.Unlock()
49+
s.Unlock()
5150
}
5251

5352
type Front struct {
54-
Alarm bool `json:"alarm"`
55-
LastUpdated *time.Time `json:"last_updated"`
53+
Alarm bool `json:"alarm"`
54+
LastUpdated time.Time `json:"last_updated"`
5655
}
5756

58-
func (s *Store) CurrentValue() *Front {
59-
s.mutex.RLock()
57+
func (s Store) CurrentValue() *Front {
58+
s.RLock()
6059
acc := true
6160
for _, r := range s.Statuses {
6261
acc = acc && r.Good
6362
}
64-
ret := &Front{Alarm: acc, LastUpdated: &s.LastUpdated}
65-
s.mutex.RUnlock()
63+
ret := &Front{Alarm: acc, LastUpdated: s.LastUpdated}
64+
s.RUnlock()
6665
return ret
6766
}

0 commit comments

Comments
 (0)