-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmetadata.go
76 lines (65 loc) · 2.54 KB
/
metadata.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build linux
// +build linux
// Package link allows storing netlink.Link in per Connection.Id metadata
package link
import (
"context"
"github.com/vishvananda/netlink"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)
type key struct{}
// Store sets the netlink.Link stored in per Connection.Id metadata.
func Store(ctx context.Context, isClient bool, link netlink.Link) {
metadata.Map(ctx, isClient).Store(key{}, link)
}
// Delete deletes the netlink.Link stored in per Connection.Id metadata
func Delete(ctx context.Context, isClient bool) {
metadata.Map(ctx, isClient).Delete(key{})
}
// Load returns the netlink.Link stored in per Connection.Id metadata, or nil if no
// value is present.
// The ok result indicates whether value was found in the per Connection.Id metadata.
func Load(ctx context.Context, isClient bool) (value netlink.Link, ok bool) {
rawValue, ok := metadata.Map(ctx, isClient).Load(key{})
if !ok {
return
}
value, ok = rawValue.(netlink.Link)
return value, ok
}
// LoadOrStore returns the existing netlink.Link stored in per Connection.Id metadata if present.
// Otherwise, it stores and returns the given nterface_types.InterfaceIndex.
// The loaded result is true if the value was loaded, false if stored.
func LoadOrStore(ctx context.Context, isClient bool, link netlink.Link) (value netlink.Link, ok bool) {
rawValue, ok := metadata.Map(ctx, isClient).LoadOrStore(key{}, link)
if !ok {
return
}
value, ok = rawValue.(netlink.Link)
return value, ok
}
// LoadAndDelete deletes the netlink.Link stored in per Connection.Id metadata,
// returning the previous value if any. The loaded result reports whether the key was present.
func LoadAndDelete(ctx context.Context, isClient bool) (value netlink.Link, ok bool) {
rawValue, ok := metadata.Map(ctx, isClient).LoadAndDelete(key{})
if !ok {
return
}
value, ok = rawValue.(netlink.Link)
return value, ok
}