<commit_before>package pooly

import (
	"net"
	"time"
)

// Conn abstracts user connections that are part of a Pool.
type Conn struct {
	iface  interface{}
	timer  *time.Timer
	closed bool
}

// Create a new connection container, wrapping up a user defined connection object.
func NewConn(i interface{}) *Conn {
	return &Conn{iface: i}
}

// Interface returns an interface referring to the underlying user object.
func (c *Conn) Interface() interface{} {
	return c.iface
}

// NetConn is a helper for underlying user objects that satisfy
// the standard library net.Conn interface
func (c *Conn) NetConn() net.Conn {
	return c.iface.(net.Conn)
}

func (c *Conn) isClosed() bool {
	return c.closed
}

func (c *Conn) setClosed() {
	if c.timer != nil {
		c.timer.Stop()
	}
	c.closed = true
}

func (c *Conn) setIdle(p *Pool) {
	if p.IdleTimeout > 0 {
		c.timer = time.AfterFunc(p.IdleTimeout, func() {
			// The connection has been idle for too long,
			// send it to the garbage collector
			p.gc <- c
		})
	}
}

func (c *Conn) setActive() bool {
	if c.timer != nil {
		return c.timer.Stop()
	}
	return true
}<commit_msg>Fix a subtle race condition with the usage of time.AfterFunc<commit_after>package pooly

import (
	"net"
	"time"
)

// Conn abstracts user connections that are part of a Pool.
type Conn struct {
	iface     interface{}
	timer     *time.Timer
	timerStop chan bool
	closed    bool
}

// Create a new connection container, wrapping up a user defined connection object.
func NewConn(i interface{}) *Conn {
	return &Conn{
		iface:     i,
		timerStop: make(chan bool),
	}
}

// Interface returns an interface referring to the underlying user object.
func (c *Conn) Interface() interface{} {
	return c.iface
}

// NetConn is a helper for underlying user objects that satisfy
// the standard library net.Conn interface
func (c *Conn) NetConn() net.Conn {
	return c.iface.(net.Conn)
}

func (c *Conn) isClosed() bool {
	return c.closed
}

func (c *Conn) setClosed() {
	if c.timer != nil {
		select {
		case c.timerStop <- true:
		default:
		}
	}
	c.closed = true
}

func (c *Conn) setIdle(p *Pool) {
	if p.IdleTimeout > 0 {
		c.timer = time.NewTimer(p.IdleTimeout)
		go func() {
			select {
			case <-c.timerStop:
				return
			case <-c.timer.C:
				// The connection has been idle for too long,
				// send it to the garbage collector
				p.gc <- c
			}
		}()
	}
}

func (c *Conn) setActive() bool {
	if c.timer != nil {
		select {
		case c.timerStop <- true:
		default:
			return false
		}
	}
	return true
}
{'fraction_non_alphanumeric': 0.11834789515488484, 'fraction_numerical': 0.0007942811755361397, 'mean_word_length': 3.0563607085346214, 'pattern_counts': {'":': 0, '<': 9, '<?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': '1702119', 'n_tokens_mistral': 922, 'n_tokens_neox': 858, 'n_words': 308}