Skip to content

Commit

Permalink
Merge pull request #10 from tachyon-protocol/wip_rc1
Browse files Browse the repository at this point in the history
add serverCheckHelper tool
  • Loading branch information
daniel-henderson-ty authored Oct 23, 2019
2 parents 7fc5bfd + aaef5ec commit bb4c487
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/github.com/tachyon-protocol/udw"]
path = src/github.com/tachyon-protocol/udw
url = git@github.com:tachyon-protocol/udw
url = https://github.com/tachyon-protocol/udw
86 changes: 86 additions & 0 deletions src/make/serverCheckHelper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"sync"
)

func main() {
if len(os.Args) <= 1 {
fmt.Println(`CheckHelper usage:
CheckHelper [ServerId]
`)
os.Exit(-1)
return
}
fmt.Println("CheckHelper start server, try to listen to 443 port.")
serverId := strings.TrimSpace(os.Args[1])
l, err := net.Listen("tcp", ":443")
if err != nil {
fmt.Println("net.Listen fail " + err.Error())
os.Exit(-1)
return
}
gLocker :=sync.Mutex{}
gIsCloser:=false
go func() {
err := http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(serverId))
}))
gLocker.Lock()
isCloser:=gIsCloser
gLocker.Unlock()
if isCloser{
return
}
if err != nil {
fmt.Println("http.Serve fail " + err.Error())
os.Exit(-1)
return
}
}()
time.Sleep(time.Millisecond)
for i := 0; ; i++ {
if i == 9 {
fmt.Println("CheckHelper start server fail time out")
l.Close()
os.Exit(-1)
return
}
resp, err := http.Get("http://127.0.0.1:443")
if err != nil || resp.StatusCode != 200 {
time.Sleep(time.Second)
continue
}
body := make([]byte, len(serverId)+4096)
nr, err := io.ReadAtLeast(resp.Body, body, len(serverId))
if err != nil || nr != len(serverId) || bytes.Equal(body[:nr], []byte(serverId)) == false {
time.Sleep(time.Second)
continue
}
break
}
fmt.Println("CheckHelper ✔")
fmt.Println("use Ctrl+C or kill " + strconv.Itoa(os.Getpid()) + " to close it.")
waitForExit()
gLocker.Lock()
gIsCloser = true
gLocker.Unlock()
l.Close()
}

func waitForExit() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
<-ch
}
13 changes: 13 additions & 0 deletions src/make/serverCheckHelper/serverCheckHelperBuild/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "github.com/tachyon-protocol/udw/udwGoSource/udwGoBuild"

func main(){
udwGoBuild.MustBuild(udwGoBuild.BuildRequest{
PkgPath: "make/serverCheckHelper",
})
udwGoBuild.MustBuild(udwGoBuild.BuildRequest{
PkgPath: "make/serverCheckHelper",
TargetOsCpuArch: udwGoBuild.TargetLinuxAmd64,
})
}

0 comments on commit bb4c487

Please sign in to comment.