Добавить X-Instance-Id, задержку drain при SIGTERM и отдельный запуск миграций
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
package ident
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
@@ -25,6 +27,15 @@ func New() uuid.UUID {
|
||||
return id
|
||||
}
|
||||
|
||||
// NewAlias names one server process for X-Instance-Id. It is random rather
|
||||
// than the hostname, so the header reveals nothing about the infrastructure,
|
||||
// and a restarted process gets a new one.
|
||||
func NewAlias() string {
|
||||
var raw [8]byte
|
||||
_, _ = rand.Read(raw[:]) // crypto/rand.Read never returns an error since Go 1.24.
|
||||
return hex.EncodeToString(raw[:])
|
||||
}
|
||||
|
||||
// TimeOf extracts the creation time carried by a UUIDv7 as UTC with
|
||||
// millisecond precision.
|
||||
func TimeOf(id uuid.UUID) time.Time {
|
||||
|
||||
@@ -2,6 +2,7 @@ package ident_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -70,3 +71,14 @@ func TestParseRejectsNonCanonicalForms(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAliasIs16HexAndRandom(t *testing.T) {
|
||||
a, b := ident.NewAlias(), ident.NewAlias()
|
||||
hex16 := regexp.MustCompile(`^[0-9a-f]{16}$`)
|
||||
if !hex16.MatchString(a) || !hex16.MatchString(b) {
|
||||
t.Fatalf("aliases %q, %q are not 16 lowercase hex characters", a, b)
|
||||
}
|
||||
if a == b {
|
||||
t.Fatalf("two aliases are equal: %q", a)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user