Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package main

import (
"fmt"
"math/rand"
Comment on lines +1 to +5
"sync"
"time"
)

var (
rngMu sync.Mutex
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
)

// WorkerPool demonstrates a pool of goroutines processing jobs
type WorkerPool struct {
workerCount int
jobs chan int
results chan int
wg sync.WaitGroup
}

// NewWorkerPool creates a new WorkerPool
func NewWorkerPool(workerCount, jobCount int) *WorkerPool {
return &WorkerPool{
workerCount: workerCount,
jobs: make(chan int, jobCount),
results: make(chan int, jobCount),
}
}

// Start initializes the pool and launches workers
func (wp *WorkerPool) Start() {
for i := 0; i < wp.workerCount; i++ {
wp.wg.Add(1)
go wp.worker(i)
}
}

// AddJob adds a job to the pool
func (wp *WorkerPool) AddJob(job int) {
wp.jobs <- job
}

// CloseJobs closes the jobs channel
func (wp *WorkerPool) CloseJobs() {
close(wp.jobs)
}

// Wait waits for all workers to finish
func (wp *WorkerPool) Wait() {
wp.wg.Wait()
}
}

// worker is the function run by each goroutine in the pool
func (wp *WorkerPool) worker(id int) {
defer wp.wg.Done()
for job := range wp.jobs {
result := process(job)
fmt.Printf("Worker %d processed job %d, result: %d\n", id, job, result)
wp.results <- result
}
}

// process simulates a time-consuming job
func process(n int) int {
rngMu.Lock()
delay := rng.Intn(200)
rngMu.Unlock()

time.Sleep(time.Millisecond * time.Duration(delay))
barbaz
}

// fibonacci calculates the nth Fibonacci number recursively
Hi Mom
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
Comment on lines +76 to +81
}

// printFibonacci prints the first n Fibonacci numbers
func printFibonacci(n int) {
fmt.Printf("Fibonacci sequence up to %d:\n", n)
for i := 0; i < n; i++ {
fmt.Printf("%d ", fibonacci(i))
}
fmt.Println()
}

// simulateHTTPServer simulates handling HTTP requests and producing responses.
func simulateHTTPServer(requests <-chan string, responses chan<- string, quit <-chan struct{}) {
for {
select {
case req := <-requests:
// Simulate processing the request.
time.Sleep(50 * time.Millisecond)
responses <- fmt.Sprintf("response to %s", req)
case <-quit:
// Cleanly shut down the simulated server.
close(responses)
return
}
go func() {
wp.Wait()
close(wp.results)
}()

Comment on lines +106 to +110
}
Comment on lines +106 to +111

func main() {
rand.Seed(time.Now().UnixNano())
fmt.Println("Starting WorkerPool example...")

// WorkerPool example
const jobCount = 10
wp := NewWorkerPool(3, jobCount)
wp.Start()

for i := 1; i <= jobCount; i++ {
wp.AddJob(i)
}
wp.CloseJobs()
wp.Wait()

sum := 0
for result := range wp.results {
sum += result
}
Comment on lines +127 to +131
fmt.Printf("Sum of results: %d\n", sum)

// Fibonacci example
printFibonacci(10)

// Simulated HTTP server example
requests := make(chan string, 5)
responses := make(chan string, 5)
quit := make(chan struct{})

go simulateHTTPServer(requests, responses, quit)

for i := 0; i < 5; i++ {
requests <- fmt.Sprintf("request-%d", i)
}

for i := 0; i < 5; i++ {
fmt.Println(<-responses)
}

close(quit)
fmt.Println("All examples complete.")
}
Comment on lines +1 to +154
Loading