forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.go
33 lines (27 loc) · 879 Bytes
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package observability
import (
"log/slog"
"os"
"path/filepath"
"github.com/google/uuid"
)
// GetOrCreateInstanceID creates a file named dicedb.iid in a temp directory with a unique UUID v6.
// If the file exists, it reads the value and returns it. Otherwise, it creates the file and writes a new UUID v6 to it.
func GetOrCreateInstanceID() string {
tempDir := os.TempDir()
filePath := filepath.Join(tempDir, "dicedb.iid")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
id := uuid.New().String()
if err := os.WriteFile(filePath, []byte(id), 0600); err != nil {
slog.Error("unable to create dicedb.iid hence running anon", slog.Any("error", err))
return ""
}
return id
}
data, err := os.ReadFile(filePath)
if err != nil {
slog.Error("unable to read dicedb.iid hence running anon", slog.Any("error", err))
return ""
}
return string(data)
}