Skip to content

Removing unsafe lib by switching to atomic.Pointer #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pkg/epp/backend/metrics/pod_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand All @@ -36,8 +35,8 @@ const (
)

type podMetrics struct {
pod unsafe.Pointer // stores a *Pod
metrics unsafe.Pointer // stores a *Metrics
pod atomic.Pointer[Pod]
metrics atomic.Pointer[Metrics]
pmc PodMetricsClient
ds Datastore
interval time.Duration
Expand All @@ -58,15 +57,15 @@ func (pm *podMetrics) String() string {
}

func (pm *podMetrics) GetPod() *Pod {
return (*Pod)(atomic.LoadPointer(&pm.pod))
return pm.pod.Load()
}

func (pm *podMetrics) GetMetrics() *Metrics {
return (*Metrics)(atomic.LoadPointer(&pm.metrics))
return pm.metrics.Load()
}

func (pm *podMetrics) UpdatePod(in *corev1.Pod) {
atomic.StorePointer(&pm.pod, unsafe.Pointer(toInternalPod(in)))
pm.pod.Store(toInternalPod(in))
}

func toInternalPod(in *corev1.Pod) *Pod {
Expand Down Expand Up @@ -128,7 +127,7 @@ func (pm *podMetrics) refreshMetrics() error {
if updated != nil {
updated.UpdateTime = time.Now()
pm.logger.V(logutil.TRACE).Info("Refreshed metrics", "updated", updated)
atomic.StorePointer(&pm.metrics, unsafe.Pointer(updated))
pm.metrics.Store(updated)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/epp/backend/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"sync"
"time"
"unsafe"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -43,8 +42,6 @@ type PodMetricsFactory struct {

func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.Pod, ds Datastore) PodMetrics {
pm := &podMetrics{
pod: unsafe.Pointer(toInternalPod(in)),
metrics: unsafe.Pointer(newMetrics()),
pmc: f.pmc,
ds: ds,
interval: f.refreshMetricsInterval,
Expand All @@ -53,6 +50,9 @@ func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.
done: make(chan struct{}),
logger: log.FromContext(parentCtx),
}
pm.pod.Store(toInternalPod(in))
pm.metrics.Store(newMetrics())

pm.startRefreshLoop()
return pm
}
Expand Down