https://github.com/ktsivkov/servantgo
ServantGo provides a simple and idiomatic way to merge tasks of the same type that happens to run simultaneously.
https://github.com/ktsivkov/servantgo
go golang performance-optimization performance-optimizations task-manager-golang task-scheduler task-scheduling
Last synced: 2 months ago
JSON representation
ServantGo provides a simple and idiomatic way to merge tasks of the same type that happens to run simultaneously.
- Host: GitHub
- URL: https://github.com/ktsivkov/servantgo
- Owner: ktsivkov
- License: mit
- Created: 2023-03-16T14:07:10.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-04-08T16:26:06.000Z (about 2 years ago)
- Last Synced: 2024-06-20T10:13:52.972Z (about 1 year ago)
- Topics: go, golang, performance-optimization, performance-optimizations, task-manager-golang, task-scheduler, task-scheduling
- Language: Go
- Homepage: https://pkg.go.dev/github.com/ktsivkov/servantgo
- Size: 5.86 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ServantGo
Task execution and merging library**ServantGo** provides a simple and idiomatic way to merge tasks of the same type that happens to run simultaneously.
## Installation + Example
Run `go get github.com/ktsivkov/servantgo`
Create a struct for your task, and ensure that it implements the `Task` interface.
```go
package mainimport (
"fmt"
"sync"
"time""github.com/ktsivkov/servantgo"
)type taskMock struct {
result time.Time
}func (t *taskMock) Hash() servantgo.Hash {
return "test"
}func (t *taskMock) Exec() {
time.Sleep(time.Second * 5)
t.result = time.Now()
}func main() {
wg := sync.WaitGroup{}
wg.Add(2)
var r1, r2 *taskMockgo func() {
defer wg.Done()
r1 = servantgo.Run(&taskMock{})
}()
go func() {
defer wg.Done()
time.Sleep(time.Second * 2)
r2 = servantgo.Run(&taskMock{})
}()wg.Wait()
fmt.Println(r1.result.String(), r2.result.String())
}
```In this example we request two different instances of the same task to be run.
The task will take ~5 seconds to execute, and the second task will be sent for execution ~2 seconds after the first one.
Both tasks produce the same `Hash`, and therefore they are both considered identical by the library. As a result only the first one will execute, but its result will be returned to both.
Because of this the total execution time will be ~5 seconds.
**NOTE:** The result returned to all _subscribed_ parties is the same one (same pointer).