Skip to content
This repository was archived by the owner on Feb 20, 2024. It is now read-only.

Commit ae53f26

Browse files
authored
Merge pull request #29 from catmullet/update-readme
Update README.md (WIP)
2 parents 3b03dab + 3a3b3eb commit ae53f26

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ go get github.com/catmullet/go-workers
1717

1818
### Add the import to your project
1919
giving an alias helps since go-workers doesn't exactly follow conventions.
20-
_(If your using a JetBrains IDE it should automatically give it an alias)_
20+
_(If you're using a JetBrains IDE it should automatically give it an alias)_
2121
```go
2222
import (
23-
worker "github.com/catmullet/go-workers"
23+
workers "github.com/catmullet/go-workers"
2424
)
2525
```
2626
### Create a new worker <img src="https://raw.githubusercontent.com/catmullet/go-workers/assets/constworker.png" alt="worker" width="35"/>
@@ -29,25 +29,25 @@ _(Method chaining can be performed on this method like calling .Work() immediate
2929
```go
3030
type MyWorker struct {}
3131

32-
func NewMyWorker() *MyWorker {
32+
func NewMyWorker() Worker {
3333
return &MyWorker{}
3434
}
3535

36-
func (my *MyWorker) Work(w *goworker.Worker, in interface{}) error {
36+
func (my *MyWorker) Work(in interface{}, out chan<- interface{}) error {
3737
// work iteration here
3838
}
3939

40-
worker := worker.NewWorker(ctx, NewMyWorker(), numberOfWorkers)
40+
runner := workers.NewRunner(ctx, NewMyWorker(), numberOfWorkers)
4141
```
4242
### Send work to worker
4343
Send accepts an interface. So send it anything you want.
4444
```go
45-
worker.Send("Hello World")
45+
runner.Send("Hello World")
4646
```
47-
### Close and wait for the worker to finish and handle errors
47+
### Wait for the worker to finish and handle errors
4848
Any error that bubbles up from your worker functions will return here.
4949
```go
50-
if err := worker.Close(); err != nil {
50+
if err := runner.Wait(); err != nil {
5151
//Handle error
5252
}
5353
```
@@ -57,48 +57,49 @@ if err := worker.Close(); err != nil {
5757

5858
By using the InFrom method you can tell `workerTwo` to accept output from `workerOne`
5959
```go
60-
workerOne := worker.NewWorker(ctx, NewMyWorker(), 100).Work()
61-
workerTwo := worker.NewWorker(ctx, NewMyWorkerTwo(), 100).InFrom(workerOne).Work()
60+
runnerOne := workers.NewRunner(ctx, NewMyWorker(), 100).Work()
61+
runnerTwo := workers.NewRunner(ctx, NewMyWorkerTwo(), 100).InFrom(workerOne).Work()
6262
```
6363
### Accepting output from multiple workers
6464
It is possible to accept output from more than one worker but it is up to you to determine what is coming from which worker. (They will send on the same channel.)
6565
```go
66-
workerOne := worker.NewWorker(ctx, NewMyWorker(), 100).Work()
67-
workerTwo := worker.NewWorker(ctx, NewMyWorkerTwo(), 100).Work()
68-
workerThree := worker.NewWorker(ctx, NewMyWorkerThree(), 100).InFrom(workerOne, workerTwo).Work()
66+
runnerOne := workers.NewRunner(ctx, NewMyWorker(), 100).Work()
67+
runnerTwo := workers.NewRunner(ctx, NewMyWorkerTwo(), 100).Work()
68+
runnerThree := workers.NewRunner(ctx, NewMyWorkerThree(), 100).InFrom(workerOne, workerTwo).Work()
6969
```
7070

7171
## Passing Fields To Workers
7272
### Adding Values
7373
Fields can be passed via the workers object. Be sure as with any concurrency in Golang that your variables are concurrent safe. Most often the golang documentation will state the package or parts of it are concurrent safe. If it does not state so there is a good chance it isn't. Use the sync package to lock and unlock for writes on unsafe variables. (It is good practice NOT to defer in the work function.)
7474

7575
<img src="https://raw.githubusercontent.com/catmullet/go-workers/assets/constworker2.png" alt="worker" width="35"/> **ONLY** use the `Send()` method to get data into your worker. It is not shared memory unlike the worker objects values.
76+
7677
```go
7778
type MyWorker struct {
7879
message string
7980
}
8081

81-
func NewMyWorker(message string) *MyWorker {
82+
func NewMyWorker(message string) Worker {
8283
return &MyWorker{message}
8384
}
8485

85-
func (my *MyWorker) Work(w *goworker.Worker, in interface{}) error {
86+
func (my *MyWorker) Work(in interface{}, out chan<- interface{}) error {
8687
fmt.Println(my.message)
8788
}
8889

89-
worker := worker.NewWorker(ctx, NewMyWorker(), 100).Work()
90+
runner := workers.NewRunner(ctx, NewMyWorker(), 100).Work()
9091
```
9192

9293
### Setting Timeouts or Deadlines
9394
If your workers needs to stop at a deadline or you just need to have a timeout use the SetTimeout or SetDeadline methods. (These must be in place before setting the workers off to work.)
9495
```go
9596
// Setting a timeout of 2 seconds
96-
timeoutWorker.SetTimeout(2 * time.Second)
97+
timeoutRunner.SetTimeout(2 * time.Second)
9798

9899
// Setting a deadline of 4 hours from now
99-
deadlineWorker.SetDeadline(time.Now().Add(4 * time.Hour))
100+
deadlineRunner.SetDeadline(time.Now().Add(4 * time.Hour))
100101

101-
func workerFunction(w *worker.Worker, in interface{}) error {
102+
func workerFunction(in interface{}, out chan<- interface{} error {
102103
fmt.Println(in)
103104
time.Sleep(1 * time.Second)
104105
}
@@ -109,9 +110,9 @@ func workerFunction(w *worker.Worker, in interface{}) error {
109110
### Buffered Writer
110111
If you want to write out to a file or just stdout you can use SetWriterOut(writer io.Writer). The worker will have the following methods available
111112
```go
112-
worker.Println()
113-
worker.Printf()
114-
worker.Print()
113+
runner.Println()
114+
runner.Printf()
115+
runner.Print()
115116
```
116117
The workers use a buffered writer for output and can be up to 3 times faster than the fmt package. Just be mindful it won't write out to the console as quickly as an unbuffered writer. It will sync and eventually flush everything at the end, making it ideal for writing out to a file.
117118

0 commit comments

Comments
 (0)