-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f6d151
commit 965076e
Showing
4 changed files
with
105 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package core | ||
|
||
import ( | ||
"log" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Watcher struct { | ||
config *WatchConfig | ||
tester Tester | ||
quit chan bool | ||
out chan TestResult | ||
} | ||
|
||
func NewWatcher(config *WatchConfig) Watcher { | ||
outC := make(chan TestResult, 50) | ||
quit := make(chan bool) | ||
tester := NewTester(config, outC, quit) | ||
return Watcher{ | ||
config: config, | ||
tester: tester, | ||
quit: quit, | ||
out: outC, | ||
} | ||
} | ||
|
||
func (w *Watcher) Watch(f func(TestResult)) { | ||
for { | ||
err := w.doWatch() | ||
|
||
if err != nil { | ||
log.Fatalf("Fatal: %v", err) | ||
return | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-w.quit: | ||
return | ||
case rec := <-w.out: | ||
f(rec) | ||
} | ||
} | ||
}() | ||
|
||
time.Sleep(time.Duration(w.config.UpdateInterval) * time.Second) | ||
w.quit <- true | ||
} | ||
} | ||
|
||
func (w *Watcher) doWatch() error { | ||
log.Default().Println("Fetching new config...") | ||
config, err := w.config.Update() | ||
|
||
if err != nil { | ||
log.Fatalf("Could not obtain a config: %v", err.Error()) | ||
return err | ||
} | ||
|
||
records := strings.Split(config, "\n") | ||
for _, addr := range records { | ||
u, err := url.Parse(addr) | ||
if err != nil { | ||
log.Default().Printf("Invalid url: %v, skipping...", addr) | ||
continue | ||
} | ||
|
||
go w.tester.Test(u) | ||
} | ||
|
||
return nil | ||
} |