Skip to content

Instantly share code, notes, and snippets.

@dipankardas011
Created July 13, 2024 18:00
Show Gist options
  • Save dipankardas011/238391416f7b2b62c710300aa9cdff7a to your computer and use it in GitHub Desktop.
Save dipankardas011/238391416f7b2b62c710300aa9cdff7a to your computer and use it in GitHub Desktop.
go workerpools
package main
import (
"fmt"
"log/slog"
"net/http"
"sync"
)
var urls = []string{
"https://www.google.com/",
"https://golang.org/",
"https://blog.golang.org/",
"https://ksctl.com",
"https://docs.ksctl.com",
"https://kubernetes.io",
"https://github.com",
"https://gitlab.com",
"https://bitbucket.org",
"https://www.atlassian.com",
"https://www.jfrog.com",
"https://www.sonatype.com",
"https://www.sonarqube.org",
"https://www.sonarcloud.io",
"https://www.jenkins.io",
"https://www.circleci.com",
"https://www.travis-ci.com",
}
func httpCalls(url string) (bool, error) {
s, err := http.Head(url)
if err != nil {
return false, err
}
if s.StatusCode != http.StatusOK {
return false, fmt.Errorf("status code: %d", s.StatusCode)
}
return true, nil
}
type Event struct {
url string
status string
}
func Logger(e <-chan Event) {
for event := range e {
slog.Info("[EVENT]", "url", event.url, "status", event.status)
}
}
func Poller(wg *sync.WaitGroup, urls <-chan string, events chan<- Event) {
for url := range urls {
_, err := httpCalls(url)
if err != nil {
events <- Event{url: url, status: "error: " + err.Error()}
} else {
events <- Event{url: url, status: "success"}
}
wg.Done()
}
}
func main() {
urlChan := make(chan string)
events := make(chan Event)
var wg sync.WaitGroup
go Logger(events)
numOfWorkerPools := 3
for i := 0; i < numOfWorkerPools; i++ {
go Poller(&wg, urlChan, events)
}
wg.Add(len(urls))
for _, url := range urls {
urlChan <- url
}
close(urlChan)
wg.Wait()
close(events)
fmt.Println("All done!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment