<commit_before>package procspy

import (
	"bytes"
	"sync"

	"github.com/weaveworks/scope/probe/process"
)

var bufPool = sync.Pool{
	New: func() interface{} {
		return bytes.NewBuffer(make([]byte, 0, 5000))
	},
}

type pnConnIter struct {
	pn    *ProcNet
	buf   *bytes.Buffer
	procs map[uint64]*Proc
}

func (c *pnConnIter) Next() *Connection {
	n := c.pn.Next()
	if n == nil {
		// Done!
		bufPool.Put(c.buf)
		return nil
	}
	if proc, ok := c.procs[n.Inode]; ok {
		n.Proc = *proc
	}
	return n
}

// NewConnectionScanner creates a new Linux ConnectionScanner
func NewConnectionScanner(walker process.Walker, processes bool) ConnectionScanner {
	scanner := &linuxScanner{}
	if processes {
		scanner.r = newBackgroundReader(walker)
	}
	return scanner
}

// NewSyncConnectionScanner creates a new synchronous Linux ConnectionScanner
func NewSyncConnectionScanner(walker process.Walker, processes bool) ConnectionScanner {
	scanner := &linuxScanner{}
	if processes {
		scanner.r = newForegroundReader(walker)
	}
	return scanner
}

type linuxScanner struct {
	r reader
}

func (s *linuxScanner) Connections() (ConnIter, error) {
	// buffer for contents of /proc/<pid>/net/tcp<commit_msg>ensure connections from /proc/net/tcp{,6} get the right pid<commit_after>package procspy

import (
	"bytes"
	"sync"

	"github.com/weaveworks/scope/probe/process"
)

var bufPool = sync.Pool{
	New: func() interface{} {
		return bytes.NewBuffer(make([]byte, 0, 5000))
	},
}

type pnConnIter struct {
	pn    *ProcNet
	buf   *bytes.Buffer
	procs map[uint64]*Proc
}

func (c *pnConnIter) Next() *Connection {
	n := c.pn.Next()
	if n == nil {
		// Done!
		bufPool.Put(c.buf)
		return nil
	}
	if proc, ok := c.procs[n.Inode]; ok {
		n.Proc = *proc
	} else {
		// ProcNet.Next() always returns a pointer to the same
		// struct. We therefore must clear any garbage left over from
		// the previous call.
		n.Proc = Proc{}
	}
	return n
}

// NewConnectionScanner creates a new Linux ConnectionScanner
func NewConnectionScanner(walker process.Walker, processes bool) ConnectionScanner {
	scanner := &linuxScanner{}
	if processes {
		scanner.r = newBackgroundReader(walker)
	}
	return scanner
}

// NewSyncConnectionScanner creates a new synchronous Linux ConnectionScanner
func NewSyncConnectionScanner(walker process.Walker, processes bool) ConnectionScanner {
	scanner := &linuxScanner{}
	if processes {
		scanner.r = newForegroundReader(walker)
	}
	return scanner
}

type linuxScanner struct {
	r reader
}

func (s *linuxScanner) Connections() (ConnIter, error) {
	// buffer for contents of /proc/<pid>/net/tcp
{'fraction_non_alphanumeric': 0.1172147001934236, 'fraction_numerical': 0.005802707930367505, 'mean_word_length': 4.090551181102362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7230849', 'n_tokens_mistral': 934, 'n_tokens_neox': 877, 'n_words': 240}