Skip to content

Commit 5e2e8cd

Browse files
authored
Merge pull request #54 from mecitsemerci/feature/memcached-check
Added memcached health check support
2 parents 8589fde + 7a1bd62 commit 5e2e8cd

File tree

7 files changed

+93
-41
lines changed

7 files changed

+93
-41
lines changed

.github/workflows/testing.yml

+7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ jobs:
100100
env:
101101
HOST: ":8080"
102102

103+
memcached:
104+
image: memcached:1.6.9-alpine
105+
ports:
106+
- "11211"
107+
108+
103109
steps:
104110
- name: Set up Go
105111
uses: actions/setup-go@v2
@@ -117,6 +123,7 @@ jobs:
117123
HEALTH_GO_MG_DSN: mongodb://localhost:${{ job.services.mongo.ports[27017] }}/
118124
HEALTH_GO_MS_DSN: test:test@tcp(localhost:${{ job.services.mysql.ports[3306] }})/test?charset=utf8
119125
HEALTH_GO_HTTP_URL: http://localhost:${{ job.services.http.ports[8080] }}/status
126+
HEALTH_GO_MD_DSN: memcached://localhost:${{ job.services.memcached.ports[11211] }}/
120127

121128
- name: Upload coverage to Codecov
122129
uses: codecov/codecov-action@v1

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test:
1313
HEALTH_GO_MG_DSN="mongodb://`docker-compose port mongo 27017`/" \
1414
HEALTH_GO_MS_DSN="test:test@tcp(`docker-compose port mysql 3306`)/test?charset=utf8" \
1515
HEALTH_GO_HTTP_URL="http://`docker-compose port http 8080`/status" \
16+
HEALTH_GO_MD_DSN="memcached://localhost:${{ job.services.memcached.ports[11211] }}/" \
1617
go test -cover ./... -coverprofile=coverage.txt -covermode=atomic
1718

1819
lint:

checks/memcached/check.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package memcached
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/bradfitz/gomemcache/memcache"
9+
)
10+
11+
// Config is the Memcached checker configuration settings container.
12+
type Config struct {
13+
// DSN is the Memcached instance connection DSN. Required.
14+
DSN string
15+
}
16+
17+
// New creates new Memcached health check that verifies the following:
18+
// - connection establishing
19+
// - doing the PING command and verifying the response
20+
func New(config Config) func(ctx context.Context) error {
21+
// support all DSN formats (for backward compatibility) - with and w/out schema and path part:
22+
// - memcached://localhost:11211/
23+
// - localhost:11211
24+
memcachedDSN := strings.TrimPrefix(config.DSN, "memcached://")
25+
memcachedDSN = strings.TrimSuffix(memcachedDSN, "/")
26+
27+
return func(ctx context.Context) error {
28+
mdb := memcache.New(memcachedDSN)
29+
30+
err := mdb.Ping()
31+
32+
if err != nil {
33+
return fmt.Errorf("memcached ping failed: %w", err)
34+
}
35+
36+
return nil
37+
}
38+
}

checks/memcached/check_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package memcached
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
const rdDSNEnv = "HEALTH_GO_MD_DSN"
12+
13+
func TestNew(t *testing.T) {
14+
check := New(Config{
15+
DSN: getDSN(t),
16+
})
17+
18+
err := check(context.Background())
19+
require.NoError(t, err)
20+
}
21+
22+
func TestNewError(t *testing.T) {
23+
check := New(Config{
24+
DSN: "",
25+
})
26+
27+
err := check(context.Background())
28+
require.Error(t, err)
29+
}
30+
31+
func getDSN(t *testing.T) string {
32+
t.Helper()
33+
34+
redisDSN, ok := os.LookupEnv(rdDSNEnv)
35+
require.True(t, ok)
36+
37+
return redisDSN
38+
}

docker-compose.yml

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ services:
4545
MYSQL_USER: test
4646
MYSQL_PASSWORD: test
4747

48+
memcached:
49+
image: memcached:1.6.9-alpine
50+
ports:
51+
- "11211"
52+
4853
http:
4954
image: pierreprinetti/apimock:latest
5055
ports:

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/hellofresh/health-go/v4
33
go 1.15
44

55
require (
6+
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
67
github.com/go-redis/redis/v8 v8.4.2
78
github.com/go-sql-driver/mysql v1.5.0
89
github.com/jackc/pgx/v4 v4.10.0

go.sum

+3-41
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)