worker

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2023 License: MIT Imports: 2 Imported by: 0

README

Sure! Here's the content as Markdown:

# Worker Pool for Concurrent Task Processing

The `worker` package provides an efficient worker pool implementation to process tasks concurrently using goroutines. The pool consists of a set of worker goroutines that can handle tasks in a concurrent and synchronized manner. Tasks are represented by the `Task` struct, which includes an `EventType` indicating the type of event (e.g., creation, write, removal) and the `Name` of the file associated with the event.

## How to Use

To use the worker pool, follow these simple steps:

1. Import the package and the required dependencies:
```go
package main

import (
	"sync"
	"github.com/fsnotify/fsnotify"
	"github.com/cploutarchou/syncpkg/worker"
)
  1. Create a new worker pool using NewWorkerPool, specifying the capacity of the pool (maximum number of concurrent workers):
// Create a worker pool with a capacity of 10 workers
pool := worker.NewWorkerPool(10)
  1. Start the worker goroutines to process tasks. In this example, we launch 10 worker goroutines:
for i := 0; i < cap(pool.Tasks); i++ {
	go pool.Worker()
}
  1. Submit tasks to the worker pool through the Tasks channel. Each worker goroutine will process tasks as they arrive:
// Submit tasks to the worker pool
pool.Tasks <- worker.Task{EventType: fsnotify.Create, Name: "file1.txt"}
pool.Tasks <- worker.Task{EventType: fsnotify.Write, Name: "file2.txt"}
pool.Tasks <- worker.Task{EventType: fsnotify.Remove, Name: "file3.txt"}

Example Usage

Here's an example of how you can use the worker pool:

package main

import (
	"sync"
	"github.com/fsnotify/fsnotify"
	"github.com/cploutarchou/syncpkg/worker"
)

func main() {
	// Create a worker pool with a capacity of 10 workers
	pool := worker.NewWorkerPool(10)

	// Start the worker goroutines to process tasks
	for i := 0; i < cap(pool.Tasks); i++ {
		go pool.Worker()
	}

	// Submit tasks to the worker pool
	pool.Tasks <- worker.Task{EventType: fsnotify.Create, Name: "file1.txt"}
	pool.Tasks <- worker.Task{EventType: fsnotify.Write, Name: "file2.txt"}
	pool.Tasks <- worker.Task{EventType: fsnotify.Remove, Name: "file3.txt"}

	// Wait for all tasks to be completed
	pool.WG.Wait()
}

License

This package is licensed under the MIT License - see the LICENSE file for details.

The worker package empowers your Go applications to process tasks concurrently using a well-designed worker pool, ensuring efficient and synchronized handling of multiple tasks simultaneously.

Documentation

Overview

Package worker implements a worker pool to process tasks concurrently using goroutines.

The worker pool consists of a set of worker goroutines that can process tasks concurrently. Tasks are represented by the Task struct, which includes an EventType indicating the type of event (e.g., creation, write, removal) and the Name of the file associated with the event.

To use the worker pool, create a new Pool using NewWorkerPool, specifying the capacity of the pool, i.e., the maximum number of concurrent workers. Then, tasks can be submitted to the worker pool through the Tasks channel. Each worker goroutine in the pool will process tasks as they arrive. The worker pool ensures that tasks are processed in a concurrent and synchronized manner, allowing for efficient processing of multiple tasks simultaneously.

Example usage:

// Create a worker pool with a capacity of 10 workers
pool := NewWorkerPool(10)

// Start the worker goroutines to process tasks
for i := 0; i < cap(pool.Tasks); i++ {
  go pool.Worker()
}

// Submit tasks to the worker pool
pool.Tasks <- Task{EventType: fsnotify.Create, Name: "file1.txt"}
pool.Tasks <- Task{EventType: fsnotify.Write, Name: "file2.txt"}
pool.Tasks <- Task{EventType: fsnotify.Remove, Name: "file3.txt"}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	Tasks chan Task      // Tasks is the channel through which tasks are submitted to the worker pool.
	WG    sync.WaitGroup // WG is used to wait for all worker goroutines to finish their tasks.
}

Pool is a pool of worker goroutines that can process tasks concurrently.

func NewWorkerPool

func NewWorkerPool(capacity int) *Pool

NewWorkerPool constructs a new WorkerPool with the given capacity. The capacity specifies the maximum number of concurrent workers in the pool.

type Task

type Task struct {
	EventType fsnotify.Op
	Name      string
}

Task represents a task that the WorkerPool operates on. It includes the EventType, indicating the type of file event (e.g., create, write, remove), and the Name, which is the file name associated with the event.

Jump to

Keyboard shortcuts

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