From 24249e87450b4455267ad1ed1d00c812ee3bfc5a Mon Sep 17 00:00:00 2001 From: Gray Liang Date: Wed, 10 Jan 2024 19:34:01 +0800 Subject: [PATCH] Control concurrency using sync.Mutex --- control/netns_utils.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/control/netns_utils.go b/control/netns_utils.go index abb6e37928..6fc4aa47ba 100644 --- a/control/netns_utils.go +++ b/control/netns_utils.go @@ -26,7 +26,7 @@ var ( ) type DaeNetns struct { - once sync.Once + mu sync.Mutex dae0, dae0peer netlink.Link hostNs, daeNs netns.NsHandle @@ -41,11 +41,15 @@ func (ns *DaeNetns) Setup() (err error) { return nil } - ns.once.Do(func() { - if err = ns.setup(); err != nil { - logrus.Fatal("Failed to setup dae netns: %v", err) - } - }) + ns.mu.Lock() + defer ns.mu.Unlock() + if ns.daeNs != 0 && ns.hostNs != 0 { + return nil + } + if err = ns.setup(); err != nil { + ns.daeNs = 0 + ns.hostNs = 0 + } return err }