-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: combine multiple packages into one main package
- Loading branch information
Xavier Zhao
committed
Dec 21, 2019
1 parent
fc2b3c2
commit 8aa1ba8
Showing
23 changed files
with
421 additions
and
406 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import "log" | ||
|
||
func debug(format string, v ...interface{}) { | ||
if debugFlag { | ||
log.Printf(format, v...) | ||
} | ||
} |
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,7 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestDebug(t *testing.T) { | ||
debug("%s %d", "test", 123) | ||
} |
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,83 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
var ( | ||
debugFlag bool | ||
urlFlag string | ||
cycleFlag time.Duration | ||
timeoutFlag time.Duration | ||
headerTimeoutFlag time.Duration | ||
|
||
mailFromNameFlag string | ||
mailFromAddrFlag string | ||
mailFromPwdFlag string | ||
mailToAddrFlag string | ||
mailAuthHostFlag string | ||
mailServerAddrFlag string | ||
) | ||
|
||
func init() { | ||
var cycle, timeout, headerTimeout int | ||
|
||
flag.BoolVar(&debugFlag, "debug", false, "turn on debug mode") | ||
|
||
flag.StringVar(&urlFlag, "url", "", "HTTP URL to monitor") | ||
flag.IntVar(&cycle, "cycle", 20000, "detection cycle in milliseconds") | ||
flag.IntVar(&timeout, "timeout", 5000, "timeout for waiting for HTTP response, in milliseconds") | ||
flag.IntVar(&headerTimeout, "header-timeout", 2000, "timeout for waiting for HTTP response headers after establishing a connection, in milliseconds") | ||
|
||
flag.StringVar(&mailFromNameFlag, "mail-fname", "Site Monitor", "mail sender's name") | ||
flag.StringVar(&mailFromAddrFlag, "mail-faddr", "", "mail sender's address") | ||
flag.StringVar(&mailFromPwdFlag, "mail-fpwd", "", "mail sender's SMTP password") | ||
flag.StringVar(&mailToAddrFlag, "mail-taddr", "", "mail recipient address") | ||
flag.StringVar(&mailAuthHostFlag, "mail-auth", "", "SMTP authentication host address") | ||
flag.StringVar(&mailServerAddrFlag, "mail-server", "", "SMTP server address") | ||
|
||
//testing.Init() // uncomment when executing unit tests | ||
flag.Parse() | ||
|
||
var miss string | ||
|
||
if urlFlag == "" { | ||
miss += "-url, " | ||
} | ||
if mailFromNameFlag == "" { | ||
miss += "-mail-fname, " | ||
} | ||
if mailFromAddrFlag == "" { | ||
miss += "-mail-faddr, " | ||
} | ||
if mailFromPwdFlag == "" { | ||
miss += "-mail-fpwd, " | ||
} | ||
if mailToAddrFlag == "" { | ||
miss += "-mail-taddr, " | ||
} | ||
if mailAuthHostFlag == "" { | ||
miss += "-mail-auth, " | ||
} | ||
if mailServerAddrFlag == "" { | ||
miss += "-mail-server, " | ||
} | ||
if miss != "" { | ||
log.Fatalf("missing the necessary flag(s): %srun \"site-monitor -h\" for more information about each flag.", miss) | ||
} | ||
|
||
convertDuration(cycle, &cycleFlag) | ||
convertDuration(timeout, &timeoutFlag) | ||
convertDuration(headerTimeout, &headerTimeoutFlag) | ||
} | ||
|
||
func convertDuration(n int, du *time.Duration) { | ||
d, err := time.ParseDuration(strconv.Itoa(n) + "ms") | ||
if err != nil { | ||
log.Fatal("the input time format is not available") | ||
} | ||
*du = d | ||
} |
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,13 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestConvertDuration(t *testing.T) { | ||
var du time.Duration | ||
if convertDuration(10, &du); du.String() != "10ms" { | ||
t.Fail() | ||
} | ||
} |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func newHTTPClient(timeout, responseHeaderTimeout time.Duration) http.Client { | ||
tr := http.Transport{ | ||
DisableKeepAlives: true, | ||
ResponseHeaderTimeout: responseHeaderTimeout, | ||
} | ||
client := http.Client{ | ||
Transport: &tr, | ||
Timeout: timeout, | ||
} | ||
|
||
return client | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewHTTPClient(t *testing.T) { | ||
c := newHTTPClient(time.Second*5, time.Second*2) | ||
if c.Timeout.String() != "5s" { | ||
t.Fail() | ||
return | ||
} | ||
if tr := c.Transport.(*http.Transport); tr.ResponseHeaderTimeout.String() != "2s" { | ||
t.Fail() | ||
return | ||
} | ||
resp, err := c.Get("https://jsonplaceholder.typicode.com/users/1") | ||
if err != nil { | ||
t.Log(err) | ||
t.Fail() | ||
return | ||
} | ||
defer resp.Body.Close() | ||
fmt.Println(resp.StatusCode) | ||
} |
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,66 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
loggerInstance logger | ||
) | ||
|
||
type logRecord struct { | ||
time time.Time | ||
isNormal bool | ||
statusCode int | ||
isTimeout bool | ||
err error | ||
} | ||
|
||
type logger struct { | ||
sync.Mutex | ||
records []logRecord | ||
} | ||
|
||
func (l *logger) string() string { | ||
var msg string | ||
for _, record := range l.records { | ||
msg += fmt.Sprintf("* %s\tnormal: %v\tstatus code: %d\ttimeout: %v\terror: %v\n", | ||
record.time.Format("2006-01-02 15:04:05"), record.isNormal, record.statusCode, record.isTimeout, record.err) | ||
} | ||
return msg | ||
} | ||
|
||
func logPush(record logRecord) { | ||
loggerInstance.Lock() | ||
defer loggerInstance.Unlock() | ||
|
||
if len(loggerInstance.records) >= 10 { | ||
loggerInstance.records = loggerInstance.records[1:] | ||
} | ||
loggerInstance.records = append(loggerInstance.records, record) | ||
|
||
logCheck() | ||
} | ||
|
||
func logCheck() { | ||
var unNormal int | ||
for _, r := range loggerInstance.records { | ||
if !r.isNormal { | ||
unNormal++ | ||
} | ||
} | ||
if unNormal >= 5 { | ||
debug("* Site status: unhealthy") | ||
go sendMail(loggerInstance.string()) | ||
} else if float64(unNormal)/float64(len(loggerInstance.records)) > 0.5 { | ||
debug("* Site status: unhealthy") | ||
} else { | ||
debug("* Site status: healthy") | ||
} | ||
} | ||
|
||
func init() { | ||
loggerInstance.records = make([]logRecord, 0, 10) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.