gopool

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

Gopool

A Golang goroutine pool with graceful shutdown.

Installation

To install this package, you need to setup your Go workspace. The simplest way to install the library is to run:

go get github.com/snapp-incubator/gopool

Example

package main

import (
	"fmt"
	"time"

	"github.com/snapp-incubator/gopool"
)

type sample struct {
	counter int
}

func newSample() gopool.Job {
	return &sample{
		counter: 0,
	}
}

func (t *sample) Do() {
	fmt.Println("Task started at:", time.Now().Format("2006-01-02 15:04:05"))
	t.counter += 1
	fmt.Println("Task finished at:", time.Now().Format("2006-01-02 15:04:05"))
}

func main() {
	wp := gopool.NewWorkerPool(2, 3)
	requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}

	for _, r := range requests {
		wp.AddTask(newSample())
		fmt.Printf("Handling Task: %s\n", r)
		time.Sleep(time.Second)
	}

	wp.Shutdown()
}

Documentation

Overview

Package gopool is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job interface {
	Do()
}

Job is an interface that specifies the function for a worker to execute.

type MockJob

type MockJob struct {
	// contains filtered or unexported fields
}

MockJob is a mock of Job interface.

func NewMockJob

func NewMockJob(ctrl *gomock.Controller) *MockJob

NewMockJob creates a new mock instance.

func (*MockJob) Do

func (m *MockJob) Do()

Do mocks base method.

func (*MockJob) EXPECT

func (m *MockJob) EXPECT() *MockJobMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

type MockJobMockRecorder

type MockJobMockRecorder struct {
	// contains filtered or unexported fields
}

MockJobMockRecorder is the mock recorder for MockJob.

func (*MockJobMockRecorder) Do

func (mr *MockJobMockRecorder) Do() *gomock.Call

Do indicates an expected call of Do.

type MockWorkerPool

type MockWorkerPool struct {
	// contains filtered or unexported fields
}

MockWorkerPool is a mock of WorkerPool interface.

func NewMockWorkerPool

func NewMockWorkerPool(ctrl *gomock.Controller) *MockWorkerPool

NewMockWorkerPool creates a new mock instance.

func (*MockWorkerPool) AddTask

func (m *MockWorkerPool) AddTask(task Job)

AddTask mocks base method.

func (*MockWorkerPool) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockWorkerPool) Shutdown

func (m *MockWorkerPool) Shutdown()

Shutdown mocks base method.

type MockWorkerPoolMockRecorder

type MockWorkerPoolMockRecorder struct {
	// contains filtered or unexported fields
}

MockWorkerPoolMockRecorder is the mock recorder for MockWorkerPool.

func (*MockWorkerPoolMockRecorder) AddTask

func (mr *MockWorkerPoolMockRecorder) AddTask(task any) *gomock.Call

AddTask indicates an expected call of AddTask.

func (*MockWorkerPoolMockRecorder) Shutdown

func (mr *MockWorkerPoolMockRecorder) Shutdown() *gomock.Call

Shutdown indicates an expected call of Shutdown.

type WorkerPool

type WorkerPool interface {
	AddTask(task Job)
	Shutdown()
}

WorkerPool is an interface that specifies the functions to handle the pool of worker goroutines.

func NewWorkerPool

func NewWorkerPool(maxWorker, capacity uint) WorkerPool

NewWorkerPool is a factory function to create and start a pool of worker goroutines.

The maxWorker parameter specifies the maximum number of workers that can execute tasks concurrently. The capacity parameter specifies the maximum number of tasks that can be put in a queue.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL