Skip to content

Implement WorkerPool and Fibonacci examples in Go#338

Open
mhijazi1 wants to merge 3 commits intomainfrom
mhijazi1-patch-1
Open

Implement WorkerPool and Fibonacci examples in Go#338
mhijazi1 wants to merge 3 commits intomainfrom
mhijazi1-patch-1

Conversation

@mhijazi1
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a standalone Go program intended to demonstrate concurrency patterns (a goroutine worker pool) and a Fibonacci example.

Changes:

  • Introduces a WorkerPool type with job submission, worker startup, and result collection.
  • Adds a recursive Fibonacci implementation and a printer helper.
  • Adds a “simulated HTTP server” section (currently incomplete and deadlocking).
Comments suppressed due to low confidence (1)

test.go:121

  • This loop reads 5 values from responses, but nothing in main writes to that channel, so the program will block here at runtime. Ensure a producer writes the expected number of responses (or closes the channel and range over it) before reading.
	for i := 0; i < 5; i++ {
		fmt.Println(<-responses)
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

test.go Outdated
Comment on lines +113 to +114
//go simulateHTTPServer(requests, responses, quit)

Comment on lines +98 to +102

sum := 0
for result := range wp.results {
sum += result
}
Comment on lines +1 to +5
package main

import (
"fmt"
"math/rand"
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a Go file to the Fastify JavaScript/Node.js web framework repository. The file implements three examples: a WorkerPool pattern for concurrent job processing, recursive Fibonacci number calculation, and a simulated HTTP server using channels.

Changes:

  • Added test.go with WorkerPool, Fibonacci, and HTTP server simulation examples in Go
Comments suppressed due to low confidence (2)

test.go:122

  • Potential deadlock: The results channel is never closed, causing the range loop to block indefinitely after all results are processed. Add a goroutine after wp.Wait() to close the results channel, or close it explicitly before this loop.
	for result := range wp.results {
		sum += result
	}

test.go:105

  • rand.Seed is deprecated as of Go 1.20. Use rand.New(rand.NewSource(time.Now().UnixNano())) to create a new Rand instance, or remove this line entirely as the global rand is automatically seeded in Go 1.20+.
	rand.Seed(time.Now().UnixNano())

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

test.go Outdated

// process simulates a time-consuming job
func process(n int) int {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(200)))
Comment on lines +1 to +145
package main

import (
"fmt"
"math/rand"
"sync"
"time"
)

// 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 {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(200)))
return n * n
}

// fibonacci calculates the nth Fibonacci number recursively
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}

// 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)
}()

}

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
}
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 +97 to +101
go func() {
wp.Wait()
close(wp.results)
}()

Comment on lines +67 to +72
// fibonacci calculates the nth Fibonacci number recursively
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
test.go Outdated

// process simulates a time-consuming job
func process(n int) int {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(200)))
Copy link
Copy Markdown
Author

@mhijazi1 mhijazi1 Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
time.Sleep(time.Millisecond * time.Duration(rand.Intn(200)))
foobar

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Mo <mhijazi1@github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new Go file implementing concurrency patterns including a WorkerPool for parallel job processing, a recursive Fibonacci calculator, and an HTTP server simulation. However, the code contains multiple critical syntax errors and logic issues that prevent it from compiling or running correctly.

Changes:

  • Added WorkerPool implementation with goroutine-based job processing
  • Added Fibonacci sequence calculator and printer
  • Added simulated HTTP server with channel-based request/response handling
Comments suppressed due to low confidence (1)

test.go:126

  • Potential deadlock issue. The main function waits for workers to complete with wp.Wait() on line 126, but the results channel is never closed until after wp.Wait() returns (the goroutine that closes results would normally be started but is misplaced at lines 106-109). This causes the range loop on line 129 to block indefinitely, waiting for the results channel to close. The goroutine that closes wp.results should be started before wp.Wait() is called.
	wp.Wait()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

func (wp *WorkerPool) Wait() {
wp.wg.Wait()
}
}
rngMu.Unlock()

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

// fibonacci calculates the nth Fibonacci number recursively
Hi Mom
Comment on lines +106 to +111
go func() {
wp.Wait()
close(wp.results)
}()

}
}

func main() {
rand.Seed(time.Now().UnixNano())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants