Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v1
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ RELEASE=$$(git rev-parse HEAD)

default: bin

lint:
golangci-lint run

lint-fix:
golangci-lint run --fix

bin:
mkdir -p bin
cd ./ && go build -o ./bin/$(APP_NAME)
Expand Down
36 changes: 16 additions & 20 deletions internal/connection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,25 @@ func (c *Connection) Get() (*httputil.ReverseProxy, error) {
}

func (c *Connection) healthCheck() {
for {
select {
case msg := <-c.Messages:
c.Lock()
if msg.Shutdown {
c.Shutdown()
c.Unlock()
return
} else {
backend := msg.Backend
c.healthy = msg.Health
proxy := msg.Proxy

if proxy != nil && c.Backend != backend {
c.Backend = backend
c.proxy = proxy
}
for msg := range c.Messages {
c.Lock()
if msg.Shutdown {
c.Shutdown()
c.Unlock()
return
} else {
backend := msg.Backend
c.healthy = msg.Health
proxy := msg.Proxy

if proxy != nil && c.Backend != backend {
c.Backend = backend
c.proxy = proxy
}

msg.Ack.Done()
c.Unlock()
}

msg.Ack.Done()
c.Unlock()
}
}

Expand Down
14 changes: 9 additions & 5 deletions internal/connection/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package connection

import (
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand All @@ -14,18 +15,21 @@ import (
)

func TestConnection(t *testing.T) {
assertion := &assert.Asserter{T: t}

tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 1 * time.Second,
}

availableHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var message []byte
message = []byte("hello")
w.Write(message)
})
message := []byte("hello")

assertion := &assert.Asserter{T: t}
_, err := w.Write(message)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
})

t.Run("When get is called on a healthy connection", func(t *testing.T) {
availableServer := httptest.NewServer(availableHandler)
Expand Down
5 changes: 4 additions & 1 deletion internal/gracefulserver/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func TestMain(m *testing.M) {
}

func testHandler(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("ok"))
_, err := rw.Write([]byte("ok"))
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
}

func TestListenAndServe(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions internal/healthcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,19 @@ func New(client *http.Client, subscribers []chan connection.Message, backend str
}

func (hc *HealthChecker) Start(startup *sync.WaitGroup) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bg := context.Background()
timeout := 1000 * time.Millisecond
startup.Done()
ctx, cancel := context.WithTimeout(bg, timeout)
hc.check(ctx, cancel)

hc.Lock()
hc.check(ctx)
hc.Unlock()

ticker := time.NewTicker(1000 * time.Millisecond)
ticker := time.NewTicker(timeout)
for {
select {
case <-ticker.C:
cancel()
hc.Lock()
ctx, cancel = context.WithCancel(context.Background())
hc.check(ctx)
hc.Unlock()
case <-ctx.Done():
<-ticker.C
ctx, cancel = context.WithTimeout(bg, timeout)
hc.check(ctx, cancel)
case <-hc.done:
ticker.Stop()
return
Expand All @@ -75,7 +71,11 @@ func (hc *HealthChecker) Reuse(newBackend string, proxy *httputil.ReverseProxy)
return hc
}

func (hc *HealthChecker) check(ctx context.Context) {
func (hc *HealthChecker) check(ctx context.Context, cancel context.CancelFunc) {
hc.Lock()
defer hc.Unlock()
defer cancel()

url := fmt.Sprintf("%s%s", hc.backend, "/health")
var healthy bool

Expand Down
29 changes: 18 additions & 11 deletions internal/healthcheck/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package healthcheck

import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"sync"
Expand All @@ -27,7 +28,10 @@ func TestHealthChecker(t *testing.T) {
availableHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
healthReponse := &Reponse{State: "healthy", Message: ""}
healthMessage, _ := json.Marshal(healthReponse)
w.Write(healthMessage)
_, err := w.Write(healthMessage)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
})

availableServer := httptest.NewServer(availableHandler)
Expand All @@ -45,18 +49,15 @@ func TestHealthChecker(t *testing.T) {
blocker := make(chan bool, 1)
var msg connection.Message
go func() {
for {
select {
case msg = <-resChan:
if !msg.Shutdown {
msg.Ack.Done()
blocker <- true
}
for msg = range resChan {
if !msg.Shutdown {
msg.Ack.Done()
blocker <- true
}
}
}()

hc = hc.Reuse("foobar", nil)
_ = hc.Reuse("foobar", nil)

<-blocker
assertion.Equal(msg.Health, false)
Expand All @@ -69,7 +70,10 @@ func TestHealthChecker(t *testing.T) {
availableHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
healthReponse := &Reponse{State: "healthy", Message: ""}
healthMessage, _ := json.Marshal(healthReponse)
w.Write(healthMessage)
_, err := w.Write(healthMessage)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
})

availableServer := httptest.NewServer(availableHandler)
Expand Down Expand Up @@ -98,7 +102,10 @@ func TestHealthChecker(t *testing.T) {
degradedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
healthReponse := &Reponse{State: "degraded", Message: ""}
healthMessage, _ := json.Marshal(healthReponse)
w.Write(healthMessage)
_, err := w.Write(healthMessage)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
})

degradedServer := httptest.NewServer(degradedHandler)
Expand Down
5 changes: 4 additions & 1 deletion internal/pool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ func (p *pool) Fetch(w http.ResponseWriter, r *http.Request) {
if found {
stats.CacheCounter.WithLabelValues("hit").Add(1)
res := value.(string)
w.Write([]byte(res))
_, err := w.Write([]byte(res))
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
return
}

Expand Down
24 changes: 19 additions & 5 deletions internal/pool/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -29,7 +30,6 @@ func waitForHealthCheck(connectionPool *pool, server string) {
}

hc.Wg.Wait()
return
}

func TestFetch(t *testing.T) {
Expand Down Expand Up @@ -66,7 +66,10 @@ func TestFetch(t *testing.T) {
message = []byte("hello")
}

w.Write(message)
_, err := w.Write(message)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}

go func() {
time.Sleep(1 * time.Second)
Expand Down Expand Up @@ -120,7 +123,11 @@ func TestFetch(t *testing.T) {
message = []byte("hello")
}

w.Write(message)
_, err := w.Write(message)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}

go func() {
time.Sleep(1 * time.Second)
blocker <- true
Expand Down Expand Up @@ -173,7 +180,11 @@ func TestFetch(t *testing.T) {
message = []byte("hello")
}

w.Write(message)
_, err := w.Write(message)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}

go func() {
time.Sleep(1 * time.Second)
blocker <- true
Expand Down Expand Up @@ -229,7 +240,10 @@ func TestFetch(t *testing.T) {
message = []byte("bar")
}

w.Write(message)
_, err := w.Write(message)
if err != nil {
log.Printf("Error writing: %s", err.Error())
}
go func() {
time.Sleep(1 * time.Second)
blocker <- true
Expand Down