forked from hellofresh/health-go
-
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.
Merge pull request hellofresh#54 from mecitsemerci/feature/memcached-…
…check Added memcached health check support
- Loading branch information
Showing
7 changed files
with
93 additions
and
41 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
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,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 | ||
} | ||
} |
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,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 | ||
} |
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