Skip to content

Commit

Permalink
Add RW operations to liveness probe for memcache
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.ru>
  • Loading branch information
teran committed Dec 31, 2024
1 parent d3e79f8 commit f43e8d0
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cmd/publisher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"database/sql"
"errors"
"net/http"
"time"

Expand All @@ -25,6 +26,7 @@ import (
"github.com/teran/archived/repositories/cache/metadata/memcache"
"github.com/teran/archived/repositories/metadata/postgresql"
"github.com/teran/archived/service"
"github.com/teran/go-random"
)

var (
Expand Down Expand Up @@ -141,11 +143,37 @@ func main() {
me.Use(echoprometheus.NewMiddleware("publisher_metrics"))
me.Use(middleware.Recover())

instanceID := random.String(random.AlphaNumeric, 32)

checkFn := func() error {
if len(cfg.MemcacheServers) > 0 {
if err := cli.Ping(); err != nil {
return err
}

key := "_ping:" + instanceID
ts := time.Now().Format(time.RFC3339Nano)

defer func() {
if err := cli.Delete(key); err != nil {
log.Warnf("ping key delete failed: %s", err)
}
}()

if err := cli.Set(&memcacheCli.Item{
Key: key,
Value: []byte(ts),
}); err != nil {
return err
}

item, err := cli.Get(key)
if err != nil {
return err
}
if string(item.Value) != ts {
return errors.New("value mismatch: memcache probably failed to store the value. failing the probe")

Check failure on line 175 in cmd/publisher/main.go

View workflow job for this annotation

GitHub Actions / golangci

do not define dynamic errors, use wrapped static errors instead: "errors.New(\"value mismatch: memcache probably failed to store the value. failing the probe\")" (err113)
}
}

if err := db.Ping(); err != nil {
Expand Down

0 comments on commit f43e8d0

Please sign in to comment.