Skip to content

Instantly share code, notes, and snippets.

@jarvis394
Forked from skillissueru/main.go
Created June 1, 2025 18:04
Show Gist options
  • Select an option

  • Save jarvis394/b1acc26d8d5a6a835cabdcb1a1693a4b to your computer and use it in GitHub Desktop.

Select an option

Save jarvis394/b1acc26d8d5a6a835cabdcb1a1693a4b to your computer and use it in GitHub Desktop.

Revisions

  1. @skillissueru skillissueru revised this gist May 15, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,7 @@ func worker(ctx context.Context, in <-chan int, out chan<- int, wg *sync.WaitGro

    val, err := processData(ctx, v)
    if errors.Is(err, errTimeout) {
    // log error, etc...
    return
    }
    //...handle other error types
    select {
  2. @skillissueru skillissueru revised this gist May 15, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,7 @@ func worker(ctx context.Context, in <-chan int, out chan<- int, wg *sync.WaitGro

    val, err := processData(ctx, v)
    if errors.Is(err, errTimeout) {
    return
    // log error, etc...
    }
    //...handle other error types
    select {
  3. @skillissueru skillissueru revised this gist May 8, 2025. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,12 @@ import (
    "time"
    )

    // Более оптимальное решение задачи из этого видео:
    // https://youtu.be/wZCLVt_5-4c
    // Из сигнатуры функции processData убран кастомный тип + убран один селект
    // (он не был прям сильно лишним, но от него можно избавиться +- безболезненно)

    // условие задачи:
    // реализовать функцию processParallel
    // прокинуть контекст

  4. @skillissueru skillissueru revised this gist May 8, 2025. No changes.
  5. @skillissueru skillissueru revised this gist May 8, 2025. No changes.
  6. @skillissueru skillissueru revised this gist May 8, 2025. No changes.
  7. @skillissueru skillissueru revised this gist May 8, 2025. No changes.
  8. @skillissueru skillissueru renamed this gist May 8, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @skillissueru skillissueru created this gist May 8, 2025.
    99 changes: 99 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    package main

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

    // реализовать функцию processParallel
    // прокинуть контекст

    var errTimeout = errors.New("timed out")

    func processData(ctx context.Context, v int) (int, error) {
    ch := make(chan struct{})

    go func() {
    time.Sleep(time.Duration(rand.Intn(10)) * time.Second)
    close(ch)
    }()

    select {
    case <-ch:
    case <-ctx.Done():
    return 0, errTimeout
    }

    return v * 2, nil
    }

    func main() {
    in := make(chan int)
    out := make(chan int)

    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    go func() {
    defer close(in)
    for i := range 10 {
    select {
    case in <- i + 1:
    case <-ctx.Done():
    return
    }
    }
    }()

    start := time.Now()
    processParallel(ctx, in, out, 5)

    for v := range out {
    fmt.Println("v =", v)
    }
    fmt.Println("main duration:", time.Since(start))
    }

    func processParallel(ctx context.Context, in <-chan int, out chan<- int, numWorkers int) {
    wg := &sync.WaitGroup{}

    for range numWorkers {
    wg.Add(1)
    go worker(ctx, in, out, wg)
    }

    go func() {
    wg.Wait()
    close(out)
    }()
    }

    func worker(ctx context.Context, in <-chan int, out chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()

    for {
    select {
    case v, ok := <-in:
    if !ok {
    return
    }

    val, err := processData(ctx, v)
    if errors.Is(err, errTimeout) {
    return
    }
    //...handle other error types
    select {
    case <-ctx.Done():
    return
    case out <- val:
    }
    case <-ctx.Done():
    return
    }
    }
    }