<commit_before>package pool

import (
	"io"
	"net"
	"os"
	"time"
)

func ExampleRecycler() {
	go func() {
		if l, err := net.Listen("tcp", "localhost:10001"); err == nil {
			p := NewPool(l.Accept)
			if c, err := p.Get(); err == nil {
				io.Copy(os.Stdout, c)
				c.Close()
			}
			l.Close()
		}
	}()
	// Output:
	// Hello World
	// Hello World
	// Hello World

	r := NewRecycler(func() (net.Conn, error) {
		return net.Dial("tcp", "localhost:10001")
	}, 1)
	p := NewPool(r.Get)

	for i := 0; i < 3; i++ {
		if c, err := p.Get(); err == nil {
			io.WriteString(c, "Hello World\n")
			r.Put(c)
		}
	}

	<-time.After(100 * time.Millisecond)
}

func ExamplePool() {
	go func() {
		if l, err := net.Listen("tcp", "localhost:10000"); err == nil {
			p := NewPool(l.Accept)
			for {
				if c, err := p.Get(); err == nil {
					go func() {
						io.Copy(os.Stdout, c)
						c.Close()
					}()
					// Output:
					// hello world
					// hello world
					// hello world
					// hello world
					// hello world
					// hello world
				}
			}
		}
	}()

	p := NewPool(Limiter(func() (net.Conn, error) {
		return net.Dial("tcp", "localhost:10000")
	}, 3))
<commit_msg>extending example<commit_after>package pool

import (
	"io"
	"net"
	"os"
	"time"
)

func ExampleRecycler() {
	go func() {
		if l, err := net.Listen("tcp", "localhost:10001"); err == nil {
			p := NewPool(l.Accept)
			if c, err := p.Get(); err == nil {
				io.Copy(os.Stdout, c)
				c.Close()
			}
			l.Close()
		}
	}()
	// Output:
	// Hello World
	// Hello World
	// Hello World

	r := NewRecycler(func() (net.Conn, error) {
		return net.Dial("tcp", "localhost:10001")
	}, 1)
	p := NewPool(r.Get)

	for i := 0; i < 3; i++ {
		if c, err := p.Get(); err == nil {
			io.WriteString(c, "Hello World\n")
			r.Put(c)
		}
	}

	if c, err := p.Get(); err == nil {
		if c2, err := p.Get(); err == nil {
			r.Put(c)
			r.Put(c2) // should be closed, capacity is 1
		}
	}

	r.Close() // close open connection

	<-time.After(100 * time.Millisecond)
}

func ExamplePool() {
	go func() {
		if l, err := net.Listen("tcp", "localhost:10000"); err == nil {
			p := NewPool(l.Accept)
			for {
				if c, err := p.Get(); err == nil {
					go func() {
						io.Copy(os.Stdout, c)
						c.Close()
					}()
					// Output:
					// hello world
					// hello world
					// hello world
					// hello world
					// hello world
					// hello world
				}
			}
		}
	}()

	p := NewPool(Limiter(func() (net.Conn, error) {
		return net.Dial("tcp", "localhost:10000")
	}, 3))

{'fraction_non_alphanumeric': 0.20408163265306123, 'fraction_numerical': 0.022809123649459785, 'mean_word_length': 2.2133676092544987, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '707565', 'n_tokens_mistral': 1240, 'n_tokens_neox': 996, 'n_words': 247}