Forráskód Böngészése

process: implement lazy retrieval of network namespace

Anton Petruhin 2 éve
szülő
commit
4d233eb524
2 módosított fájl, 18 hozzáadás és 10 törlés
  1. 2 2
      containers/container.go
  2. 16 8
      containers/process.go

+ 2 - 2
containers/container.go

@@ -922,7 +922,7 @@ func (c *Container) gc(now time.Time) {
 	listens := map[netaddr.IPPort]string{}
 	listens := map[netaddr.IPPort]string{}
 	seenNamespaces := map[string]bool{}
 	seenNamespaces := map[string]bool{}
 	for _, p := range c.processes {
 	for _, p := range c.processes {
-		if seenNamespaces[p.NetNsId] {
+		if seenNamespaces[p.NetNsId()] {
 			continue
 			continue
 		}
 		}
 		sockets, err := proc.GetSockets(p.Pid)
 		sockets, err := proc.GetSockets(p.Pid)
@@ -937,7 +937,7 @@ func (c *Container) gc(now time.Time) {
 				establishedDst[s.DAddr] = struct{}{}
 				establishedDst[s.DAddr] = struct{}{}
 			}
 			}
 		}
 		}
-		seenNamespaces[p.NetNsId] = true
+		seenNamespaces[p.NetNsId()] = true
 	}
 	}
 
 
 	for ns := range c.ipsByNs {
 	for ns := range c.ipsByNs {

+ 16 - 8
containers/process.go

@@ -15,7 +15,8 @@ import (
 type Process struct {
 type Process struct {
 	Pid       uint32
 	Pid       uint32
 	StartedAt time.Time
 	StartedAt time.Time
-	NetNsId   string
+
+	netNsId string
 
 
 	ctx        context.Context
 	ctx        context.Context
 	cancelFunc context.CancelFunc
 	cancelFunc context.CancelFunc
@@ -28,19 +29,26 @@ type Process struct {
 }
 }
 
 
 func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
 func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
-	ns, err := proc.GetNetNs(pid)
-	if err != nil {
-		return nil
-	}
-	defer ns.Close()
-	p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
+	p := &Process{Pid: pid, StartedAt: stats.BeginTime}
 	p.ctx, p.cancelFunc = context.WithCancel(context.Background())
 	p.ctx, p.cancelFunc = context.WithCancel(context.Background())
 	go p.instrument()
 	go p.instrument()
 	return p
 	return p
 }
 }
 
 
+func (p *Process) NetNsId() string {
+	if p.netNsId == "" {
+		ns, err := proc.GetNetNs(p.Pid)
+		if err != nil {
+			return ""
+		}
+		p.netNsId = ns.UniqueId()
+		_ = ns.Close()
+	}
+	return p.netNsId
+}
+
 func (p *Process) isHostNs() bool {
 func (p *Process) isHostNs() bool {
-	return p.NetNsId == hostNetNsId
+	return p.NetNsId() == hostNetNsId
 }
 }
 
 
 func (p *Process) instrument() {
 func (p *Process) instrument() {