程序很简单,通过简单的http请求来守护服务器后台程序
package main import ( "fmt" "net" "net/http" "strings" "time" ) var ( info []net.Addr ip string geturl string request *http.Request resp *http.Response client *http.Client ipx string ) func main() { info, _ = net.InterfaceAddrs() ip = strings.Split(info[0].String(), "/")[0] fmt.Println(ip) fmt.Println("alarm will begin in 30 second..") fmt.Println("please DO NOT close..") i := 10 for i > 0 { time.Sleep(time.Second * 1) fmt.Println(i) i-- } geturl = "http://" + ip + "/alarmbegin" request, _ = http.NewRequest("GET", geturl, nil) ///not must client = &http.Client{} resp, _ = client.Do(request) fmt.Println(resp) fmt.Println("done") go Iva() Ivs() } func IvsOffLine() { //ivs断线处理 info, _ = net.InterfaceAddrs() ipx = strings.Split(info[0].String(), "/")[0] //fmt.Println(ipx) if ipx == "0.0.0.0" { fmt.Println("network problem....") fmt.Println(strings.Split(info[0].String(), "/")[0]) for strings.Split(info[0].String(), "/")[0] == "0.0.0.0" { time.Sleep(time.Second * 5) info, _ = net.InterfaceAddrs() } fmt.Println("network problem fixed.. alarm will restart in 1 min") time.Sleep(time.Second * 30) geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh" request, _ = http.NewRequest("GET", geturl, nil) resp, _ = client.Do(request) fmt.Println(resp) time.Sleep(time.Second * 2) fmt.Println("alarm begin..") geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin" request, _ = http.NewRequest("GET", geturl, nil) resp, _ = client.Do(request) fmt.Println(resp) fmt.Println("done..") } } //iva 断线处理 func IvaOffLine() { defer func() { //必须要先声明defer,否则不能捕获到panic异常 if err := recover(); err != nil { fmt.Println(err) //panic } }() geturl = "http://192.168.10.57/milsng/SVSProxy/" request, _ = http.NewRequest("GET", geturl, nil) resp, _ = client.Do(request) if resp == nil { fmt.Println("iva connect fail...try to reconnect") for resp == nil { time.Sleep(time.Second * 5) geturl = "http://192.168.10.57/milsng/SVSProxy/" request, _ = http.NewRequest("GET", geturl, nil) resp, _ = client.Do(request) fmt.Println("iva connect fail...try to reconnect") } fmt.Println("iva connect refreshing... pls wait") info, _ = net.InterfaceAddrs() if strings.Split(info[0].String(), "/")[0] != "0.0.0.0" { time.Sleep(time.Minute * 10) //等待10分钟,目的是等待iva服务器恢复 geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh" request, _ = http.NewRequest("GET", geturl, nil) resp, _ = client.Do(request) fmt.Println(resp) time.Sleep(time.Second * 2) fmt.Println("alarm begin..") geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin" request, _ := http.NewRequest("GET", geturl, nil) resp, _ = client.Do(request) fmt.Println(resp) } } //fmt.Println("iva connect success") } func Ivs() { ivs := time.NewTicker(10 * time.Second) for { select { case <-ivs.C: IvsOffLine() } } } func Iva() { iva := time.NewTicker(10 * time.Second) for { select { case <-iva.C: IvaOffLine() } } }
首先 在代码中加入了runtime.GC, 回收垃圾, 结果无效,看来并不是未释放的内存
然后尝试了debug.freeosmemory 从来释放堆内存给系统 ,还是在不断变大!!!
后来发现是 TCP连接,
在代码中加入
if resp != nil { resp.Body.Close() }
关闭 io , 遂解决
版权声明:本文为博主原创文章,未经博主允许不得转载。
有疑问加站长微信联系(非本文作者)