Skip to content

Commit

Permalink
perf: check the health of reverse platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Aur0ra-m committed Apr 23, 2023
1 parent 82953bf commit 1a548d9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
27 changes: 27 additions & 0 deletions core/async/asyncCheckEngineX.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func NewAsyncCheckEngine() *AsyncCheckEngine {
}

func (e *AsyncCheckEngine) Start() {
// heart beat detection
if !e.heartbeat() {
logger.Errorln("cannot access target website successfully: http://api.ceye.io")
return
}

// build a request
request, _ := http.NewRequest("GET", e.httpAPI, nil)
client := http.Client{}
Expand Down Expand Up @@ -66,6 +72,27 @@ func (e *AsyncCheckEngine) Start() {
}
}

//
// heartbeat
// @Description: check the health of http://api.ceye.io/ three times
// @receiver e
// @return bool
//
func (e *AsyncCheckEngine) heartbeat() bool {
request, _ := http.NewRequest("GET", e.httpAPI, nil)
client := http.Client{}

for i := 0; i < 3; i++ {
response, err := client.Do(request)
if err != nil || response.StatusCode >= 500 {
logger.Debugln(err)
} else {
return true
}
}
return false
}

func (e *AsyncCheckEngine) check(token string) {
logger.Infoln(fmt.Sprintf("[async check] token: %s", token))

Expand Down
36 changes: 35 additions & 1 deletion core/async/asyncCheckEngineX_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package async

import "testing"
import (
"testing"
)

func TestAsyncCheckEngine_Start(t *testing.T) {
type fields struct {
Expand All @@ -24,3 +26,35 @@ func TestAsyncCheckEngine_Start(t *testing.T) {
})
}
}

func TestAsyncCheckEngine_heartbeat(t *testing.T) {
type fields struct {
httpAPI string
lastRecordId string
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "",
fields: fields{
httpAPI: "http://api.ceye.io/v1/records?token=0920449a5ed8b9db7a287a66a6632498&type=http",
lastRecordId: "xxxxx",
},
want: false,
}, // TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AsyncCheckEngine{
httpAPI: tt.fields.httpAPI,
lastRecordId: tt.fields.lastRecordId,
}
if got := e.heartbeat(); got != tt.want {
t.Errorf("heartbeat() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1a548d9

Please # to comment.