Skip to content

Commit

Permalink
Merge pull request hellofresh#54 from mecitsemerci/feature/memcached-…
Browse files Browse the repository at this point in the history
…check

Added memcached health check support
  • Loading branch information
vgarvardt authored Feb 14, 2021
2 parents 8589fde + 7a1bd62 commit 5e2e8cd
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ jobs:
env:
HOST: ":8080"

memcached:
image: memcached:1.6.9-alpine
ports:
- "11211"


steps:
- name: Set up Go
uses: actions/setup-go@v2
Expand All @@ -117,6 +123,7 @@ jobs:
HEALTH_GO_MG_DSN: mongodb://localhost:${{ job.services.mongo.ports[27017] }}/
HEALTH_GO_MS_DSN: test:test@tcp(localhost:${{ job.services.mysql.ports[3306] }})/test?charset=utf8
HEALTH_GO_HTTP_URL: http://localhost:${{ job.services.http.ports[8080] }}/status
HEALTH_GO_MD_DSN: memcached://localhost:${{ job.services.memcached.ports[11211] }}/

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test:
HEALTH_GO_MG_DSN="mongodb://`docker-compose port mongo 27017`/" \
HEALTH_GO_MS_DSN="test:test@tcp(`docker-compose port mysql 3306`)/test?charset=utf8" \
HEALTH_GO_HTTP_URL="http://`docker-compose port http 8080`/status" \
HEALTH_GO_MD_DSN="memcached://localhost:${{ job.services.memcached.ports[11211] }}/" \
go test -cover ./... -coverprofile=coverage.txt -covermode=atomic

lint:
Expand Down
38 changes: 38 additions & 0 deletions checks/memcached/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package memcached

import (
"context"
"fmt"
"strings"

"github.com/bradfitz/gomemcache/memcache"
)

// Config is the Memcached checker configuration settings container.
type Config struct {
// DSN is the Memcached instance connection DSN. Required.
DSN string
}

// New creates new Memcached health check that verifies the following:
// - connection establishing
// - doing the PING command and verifying the response
func New(config Config) func(ctx context.Context) error {
// support all DSN formats (for backward compatibility) - with and w/out schema and path part:
// - memcached://localhost:11211/
// - localhost:11211
memcachedDSN := strings.TrimPrefix(config.DSN, "memcached://")
memcachedDSN = strings.TrimSuffix(memcachedDSN, "/")

return func(ctx context.Context) error {
mdb := memcache.New(memcachedDSN)

err := mdb.Ping()

if err != nil {
return fmt.Errorf("memcached ping failed: %w", err)
}

return nil
}
}
38 changes: 38 additions & 0 deletions checks/memcached/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package memcached

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
)

const rdDSNEnv = "HEALTH_GO_MD_DSN"

func TestNew(t *testing.T) {
check := New(Config{
DSN: getDSN(t),
})

err := check(context.Background())
require.NoError(t, err)
}

func TestNewError(t *testing.T) {
check := New(Config{
DSN: "",
})

err := check(context.Background())
require.Error(t, err)
}

func getDSN(t *testing.T) string {
t.Helper()

redisDSN, ok := os.LookupEnv(rdDSNEnv)
require.True(t, ok)

return redisDSN
}
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ services:
MYSQL_USER: test
MYSQL_PASSWORD: test

memcached:
image: memcached:1.6.9-alpine
ports:
- "11211"

http:
image: pierreprinetti/apimock:latest
ports:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hellofresh/health-go/v4
go 1.15

require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/go-redis/redis/v8 v8.4.2
github.com/go-sql-driver/mysql v1.5.0
github.com/jackc/pgx/v4 v4.10.0
Expand Down
44 changes: 3 additions & 41 deletions go.sum

Large diffs are not rendered by default.

0 comments on commit 5e2e8cd

Please sign in to comment.