<commit_before>package routinepool

import "errors"

var errBlocked = errors.New("Pool stopped, cannot push any further tasks")

//Pool is the struct which handles the worker pool
type Pool struct {
	//size is the pool size, i.e. no.of go routines
	size uint64

	//channelSize is size of the channel buffer to which the works/jobs are sent
	channelSize uint8

	//workerPool is the channel buffer to which all works/jobs are sent
	workerPool chan poolFn

	//active is the channel used to keep count of active works
	active chan *struct{}

	//block if set to true, will block any further jobs being pushed/send to the pool
	block bool

	//quit is used to exit all the routes
	quit chan struct{}
}

type poolFn func()

type worker struct {
	fn poolFn
}

type workerPool chan []worker

//Push pushes a task to the worker pool
func (p *Pool) Push(work poolFn) error {
	if p.block {
		return errBlocked
	}

	p.workerPool <- work
	return nil
}

//Start starts the worker Q
func (p *Pool) Start() {
	p.quit = make(chan struct{})

	for i := uint64(0); i < p.size; i++ {
		go func(pl *Pool, quit chan struct{}) {
			for {
				select {
				//Listening for quit signal
				case <-quit:
					return

				// Pulling work from the channel buffer
				case work := <-pl.workerPool:
					p.active <- nil
					work()
					if pl.block == false {
						<-p.active
					}
				}
			}
		}(p, p.quit)
	}
}

//Stop stops the pool and exits all the go routines immediately
func (p *Pool) Stop() {
	//block accepting any furhter tasks
	p.block = true

	//Graceful shutdown of pool, makes sure if all pending tasks are completed
	for {
		select {
		case <-p.active:
			if len(p.workerPool) == 0 && len(p.active) == 0 {
				close(p.quit)
				return
			}
		}<commit_msg>close all channels before quitting<commit_after>package routinepool

import "errors"

var errBlocked = errors.New("Pool stopped, cannot push any further tasks")

//Pool is the struct which handles the worker pool
type Pool struct {
	//size is the pool size, i.e. no.of go routines
	size uint64

	//channelSize is size of the channel buffer to which the works/jobs are sent
	channelSize uint8

	//workerPool is the channel buffer to which all works/jobs are sent
	workerPool chan poolFn

	//active is the channel used to keep count of active works
	active chan *struct{}

	//block if set to true, will block any further jobs being pushed/send to the pool
	block bool

	//quit is used to exit all the routes
	quit chan struct{}
}

type poolFn func()

type worker struct {
	fn poolFn
}

type workerPool chan []worker

//Push pushes a task to the worker pool
func (p *Pool) Push(work poolFn) error {
	if p.block {
		return errBlocked
	}

	p.workerPool <- work
	return nil
}

//Start starts the worker Q
func (p *Pool) Start() {
	p.quit = make(chan struct{})

	for i := uint64(0); i < p.size; i++ {
		go func(pl *Pool, quit chan struct{}) {
			for {
				select {
				//Listening for quit signal
				case <-quit:
					return

				// Pulling work from the channel buffer
				case work := <-pl.workerPool:
					p.active <- nil
					work()
					if pl.block == false {
						<-p.active
					}
				}
			}
		}(p, p.quit)
	}
}

//Stop stops the pool and exits all the go routines immediately
func (p *Pool) Stop() {
	//block accepting any furhter tasks
	p.block = true

	//Graceful shutdown of pool, makes sure if all pending tasks are completed
	for {
		select {
		case <-p.active:
			if len(p.workerPool) == 0 && len(p.active) == 0 {
				//close all channels
				close(p.quit)
				close(p.active)
				close(p.workerPool)
				return
			}
		}
{'fraction_non_alphanumeric': 0.09753363228699552, 'fraction_numerical': 0.004484304932735426, 'mean_word_length': 2.7887473460721868, 'pattern_counts': {'":': 0, '<': 17, '<?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': '688180', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1119, 'n_words': 485}