Skip to content

Commit

Permalink
update examples with context
Browse files Browse the repository at this point in the history
  • Loading branch information
emar-kar committed Jun 13, 2024
1 parent 19bcc3e commit 2e25407
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/complex-http-crawler/pkg/model/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"context"
"net/http"
"time"
)
Expand All @@ -18,7 +19,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
transport := &http.Transport{
DisableCompression: true,
}
Expand Down
3 changes: 2 additions & 1 deletion examples/metadata/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"math/rand"
"time"

Expand All @@ -15,7 +16,7 @@ func New() *MyTask {
return &MyTask{}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion examples/nopper/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"math/rand"
"time"

Expand All @@ -14,7 +15,7 @@ func New() *MyTask {
return &MyTask{}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion examples/prometheus/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"

Expand All @@ -18,7 +19,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
response, err := http.Get(t.Url)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion examples/random-error/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"math/rand"
"time"
Expand All @@ -22,7 +23,7 @@ func New(index int, sleepSeconds int) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(t.SleepSeconds) * time.Second)
if rand.Float64() < t.ErrorProbability {
return errors.New("an error occurred")
Expand Down
3 changes: 2 additions & 1 deletion examples/result-channel/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log/slog"
Expand All @@ -20,7 +21,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
response, err := http.Get(t.Url)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion examples/simple-http-crawler/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"

Expand All @@ -18,7 +19,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
response, err := http.Get(t.Url)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion examples/sleeper/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"math/rand"
"time"

Expand All @@ -19,7 +20,7 @@ func New(index int, sleepSeconds int) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(t.SleepSeconds) * time.Second)
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions examples/tcp-port-scanner/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "net"
import (
"context"
"net"
)

type MyTask struct {
IP string `json:"ip"`
Expand All @@ -16,7 +19,7 @@ func New(ip string, port uint16) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: net.ParseIP(t.IP),
Port: int(t.Port),
Expand Down

0 comments on commit 2e25407

Please sign in to comment.