-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
28 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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
build: | ||
CGO_ENABLED=0 go build -o ./bin/plumber . | ||
|
||
docker-build: | ||
docker build --push -t krapi0314/plumber . | ||
|
||
docker-compose-up: | ||
docker-compose -f ./docker/docker-compose.yml up --build -d | ||
|
||
docker-compose-down: | ||
docker-compose -f ./docker/docker-compose.yml down |
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
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
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
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,68 @@ | ||
package health | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/krapie/plumber/internal/backend" | ||
) | ||
|
||
const TCP = "tcp" | ||
|
||
type Checker struct { | ||
backends []*backend.Backend | ||
interval int | ||
} | ||
|
||
func NewHealthChecker(interval int) *Checker { | ||
return &Checker{ | ||
backends: nil, | ||
interval: interval, | ||
} | ||
} | ||
|
||
func (c *Checker) Run() { | ||
go c.healthCheck() | ||
} | ||
|
||
func (c *Checker) AddBackends(backends []*backend.Backend) { | ||
c.backends = backends | ||
} | ||
|
||
func (c *Checker) healthCheck() { | ||
t := time.NewTicker(time.Duration(c.interval) * time.Second) | ||
for { | ||
select { | ||
case <-t.C: | ||
// log.Printf("[Health] Running health check") | ||
c.checkBackendLiveness() | ||
} | ||
} | ||
} | ||
|
||
func (c *Checker) checkBackendLiveness() { | ||
for _, b := range c.backends { | ||
isAlive := c.checkTCPConnection(b.Addr, c.interval) | ||
if isAlive { | ||
b.SetAlive(backend.ALIVE_UP) | ||
} else { | ||
b.SetAlive(backend.ALIVE_DOWN) | ||
} | ||
// log.Printf("[Health] Backend %s is %v", b.Addr, b.IsAlive()) | ||
} | ||
} | ||
|
||
func (c *Checker) checkTCPConnection(addr *url.URL, interval int) bool { | ||
conn, err := net.DialTimeout(TCP, addr.Host, time.Duration(interval)*time.Second) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
err = conn.Close() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
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
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
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,2 @@ | ||
#!/bin/bash | ||
for i in {1..100}; do curl -s http://localhost | grep Hostname; done | sort | uniq -c | sort -nr |
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,2 @@ | ||
#!/bin/bash | ||
for i in {1..1000}; do curl -s http://localhost | grep Hostname; sleep 0.5; done |