Last active
December 30, 2024 12:31
-
-
Save s3rgeym/872fef2aa28926bef29792e009be1793 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"os" | |
"sync" | |
) | |
type Config struct { | |
InputFile string | |
OutputFile string | |
Workers int | |
} | |
func main() { | |
config := parseFlags() | |
urls, err := readInput(config.InputFile) | |
if err != nil { | |
fmt.Printf("Error reading input: %v\n", err) | |
os.Exit(1) | |
} | |
results := make(chan string) | |
var wg sync.WaitGroup | |
for i := 0; i < config.Workers; i++ { | |
wg.Add(1) | |
go worker(urls, results, &wg) | |
} | |
outputDone := make(chan struct{}) | |
go func() { | |
if err := writeOutput(config.OutputFile, results); err != nil { | |
fmt.Printf("Error writing output: %v\n", err) | |
os.Exit(1) | |
} | |
close(outputDone) | |
}() | |
wg.Wait() | |
close(results) | |
<-outputDone | |
} | |
func parseFlags() Config { | |
inputFile := flag.String("i", "-", "Input file with URLs (default: stdin)") | |
outputFile := flag.String("o", "-", "Output file for results (default: stdout)") | |
workers := flag.Int("w", 4, "Number of workers") | |
flag.Parse() | |
return Config{ | |
InputFile: *inputFile, | |
OutputFile: *outputFile, | |
Workers: *workers, | |
} | |
} | |
func readInput(filePath string) ([]string, error) { | |
file := os.Stdin | |
if filePath != "-" { | |
var err error | |
file, err = os.Open(filePath) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
} | |
return readLines(file) | |
} | |
func writeOutput(filePath string, results <-chan string) error { | |
file := os.Stdout | |
if filePath != "-" { | |
var err error | |
file, err = os.Create(filePath) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
} | |
writer := bufio.NewWriter(file) | |
defer writer.Flush() | |
for result := range results { | |
if _, err := fmt.Fprintln(writer, result); err != nil { | |
return err | |
} | |
writer.Flush() | |
} | |
return nil | |
} | |
func readLines(file *os.File) ([]string, error) { | |
var lines []string | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
return lines, scanner.Err() | |
} | |
func worker(urls []string, results chan<- string, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for _, url := range urls { | |
results <- processURL(url) | |
} | |
} | |
func processURL(url string) string { | |
return fmt.Sprintf("Processed: %s", url) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sem.Acquire() | |
// defer sem.Release() | |
type Semaphore chan struct{} | |
func NewSemaphore(n int) Semaphore { | |
return make(chan struct{}, n) | |
} | |
func (s Semaphore) Acquire() { | |
s <- struct{}{} | |
} | |
func (s Semaphore) Release() { | |
<-s | |
} |
Author
s3rgeym
commented
Dec 30, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment