Skip to content

Commit

Permalink
change ping logix
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Aug 15, 2021
1 parent 28696ac commit a28a211
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ test:
${GO} test ./...

build: sower sowerd

.PHONY: sower
sower:
${GO} build -ldflags "\
-X main.version=$(shell git describe --tags --always) \
-X main.date=$(shell date +%Y-%m-%d)" \
-o sower ./cmd/sower
.PHONY: sowerd
sowerd:
${GO} build -ldflags "\
-X main.version=$(shell git describe --tags --always) \
Expand Down
30 changes: 22 additions & 8 deletions router/ping.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package router

import (
"net"
"net/http"
"sync"
"time"

"github.com/wweir/deferlog"
"github.com/rs/zerolog/log"
)

var pingClient = http.Client{
Expand All @@ -30,11 +30,25 @@ type ping struct {
}

func (p *ping) Fulfill(key string) error {
// just like `curl -I http://domain.com:80`
_, err := pingClient.Head(net.JoinHostPort(key, "80"))
deferlog.Std.DebugWarn(err).
Str("domain", key).
Msg("detect if site is accessible")
p.isAccess = (err == nil)
wg := sync.WaitGroup{}
wg.Add(2)
var err80, err443 error
go func() {
_, err80 = pingClient.Head("http://" + key)
wg.Done()
}()
go func() {
_, err443 = pingClient.Head("https://" + key)
wg.Done()
}()
wg.Wait()

if err80 != nil && err443 != nil {
log.Warn().
Errs("errs", []error{err80, err443}).
Msg("Failed to ping")
}

p.isAccess = (err80 == nil && err443 == nil)
return nil
}

0 comments on commit a28a211

Please sign in to comment.