Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active March 1, 2019 18:26
Show Gist options
  • Save haproxytechblog/098d9dfea5929a5a3291847a1d5c57fc to your computer and use it in GitHub Desktop.
Save haproxytechblog/098d9dfea5929a5a3291847a1d5c57fc to your computer and use it in GitHub Desktop.
API Gateway Part 3
backend apiservers
balance roundrobin
server server1 192.168.50.3:80 check
server server1 192.168.50.3:80 check inter 5s downinter 5s fall 3 rise 3
backend apiservers
balance roundrobin
option httpchk GET /check
server server1 192.168.50.3:80 check
option httpchk GET /check
server server1 192.168.50.3:443 check port 80
option httpchk GET /check HTTP/1.1\r\nHost:\ mywebsite.com
server server1 192.168.50.3:80 check port 80
backend apiservers
balance roundrobin
option httpchk GET /check
http-check expect status 200
server server1 192.168.50.3:80 check
http-check expect string success
backend apiservers
balance roundrobin
option redispatch
server server1 192.168.50.3:80 check inter 2m downinter 2m observe layer4 error-limit 10 on-error mark-down
Server apiservers/server1 is DOWN, reason: Health analyze, info: "Detected 10 consecutive errors, last one was: L4 unsuccessful connection". 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server apiservers/server1 is UP, reason: Layer4 check passed, check duration: 0ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
server server1 192.168.50.3:80 check inter 2m downinter 2m observe layer7 error-limit 10 on-error mark-down
backend apiservers
balance roundrobin
server server1 192.168.50.3:80 check weight 100 agent-check agent-inter 5s agent-addr 192.168.50.3 agent-port 9999
package main
import (
"fmt"
"time"
"github.com/firstrow/tcp_server"
"github.com/mackerelio/go-osstat/cpu"
)
func main() {
server := tcp_server.New(":9999")
server.OnNewClient(func(c *tcp_server.Client) {
fmt.Println("Client connected")
cpuIdle, err := getIdleTime()
if err != nil {
fmt.Println(err)
c.Close()
return
}
if cpuIdle < 10 {
// Set server weight to half
c.Send("50%\n")
} else {
c.Send("100%\n")
}
c.Close()
})
server.Listen()
}
func getIdleTime() (float64, error) {
before, err := cpu.Get()
if err != nil {
return 0, err
}
time.Sleep(time.Duration(1) * time.Second)
after, err := cpu.Get()
if err != nil {
return 0, err
}
total := float64(after.Total - before.Total)
cpuIdle := float64(after.Idle-before.Idle) / total * 100
return cpuIdle, nil
}
listen stats
bind *:8404
stats enable
stats uri /
stats refresh 5s
backend apiservers
balance roundrobin
server server1 192.168.50.3:80 check maxconn 30
backend apiservers
balance leastconn
timeout queue 5s
server server1 192.168.50.3:80 check maxconn 30
frontend api_gateway_northamerica
bind :80
acl northamerica_toobusy avg_queue(northamerica) gt 20
acl europe_up srv_is_up(europe/server1)
use_backend europe if northamerica_toobusy europe_up
default_backend northamerica
backend northamerica
balance leastconn
timeout queue 5s
server server1 server1:80 check maxconn 30
backend europe
option httpchk GET /check
server server1 eu-api.mysite.com:80 check port 8080
frontend api_gateway_europe
bind :80
default_backend europe
frontend health_check
bind :8080
monitor-uri /check
acl europe_toobusy avg_queue(europe) gt 20
monitor fail if europe_toobusy
backend europe
balance leastconn
timeout queue 5s
server server1 server1:80 check maxconn 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment