7
7
8
8
"github.com/cespare/xxhash/v2"
9
9
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
10
+ "github.com/rs/zerolog/log"
10
11
"google.golang.org/grpc"
11
12
)
12
13
@@ -22,6 +23,23 @@ type nodeIDHandle struct {
22
23
23
24
var defaultNodeID string
24
25
26
+ func init () {
27
+ hostname , err := os .Hostname ()
28
+ if err != nil {
29
+ log .Warn ().Err (err ).Msg ("failed to get hostname, using an empty node ID" )
30
+ return
31
+ }
32
+
33
+ // Hash the hostname to get the final default node ID.
34
+ hasher := xxhash .New ()
35
+ if _ , err := hasher .WriteString (hostname ); err != nil {
36
+ log .Warn ().Err (err ).Msg ("failed to hash hostname, using an empty node ID" )
37
+ return
38
+ }
39
+
40
+ defaultNodeID = spiceDBPrefix + fmt .Sprintf ("%x" , hasher .Sum (nil ))
41
+ }
42
+
25
43
// ContextWithHandle adds a placeholder to a context that will later be
26
44
// filled by the Node ID.
27
45
func ContextWithHandle (ctx context.Context ) context.Context {
@@ -37,21 +55,6 @@ func FromContext(ctx context.Context) (string, error) {
37
55
}
38
56
}
39
57
40
- if defaultNodeID == "" {
41
- hostname , err := os .Hostname ()
42
- if err != nil {
43
- return "" , err
44
- }
45
-
46
- // Hash the hostname to get the final default node ID.
47
- hasher := xxhash .New ()
48
- if _ , err := hasher .WriteString (hostname ); err != nil {
49
- return "" , fmt .Errorf ("failed to hash hostname: %w" , err )
50
- }
51
-
52
- defaultNodeID = spiceDBPrefix + fmt .Sprintf ("%x" , hasher .Sum (nil ))
53
- }
54
-
55
58
if err := setInContext (ctx , defaultNodeID ); err != nil {
56
59
return "" , err
57
60
}
0 commit comments