initial commit
This commit is contained in:
66
examples/basic/commands.go
Normal file
66
examples/basic/commands.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Entity struct {
|
||||
Name string
|
||||
Address string
|
||||
}
|
||||
|
||||
type GetName struct {
|
||||
UID string
|
||||
Sleep time.Duration
|
||||
|
||||
Result Entity
|
||||
}
|
||||
|
||||
func (b *GetName) CorrelationID() string {
|
||||
return b.UID
|
||||
}
|
||||
|
||||
func (b *GetName) Execute(context.Context) error {
|
||||
b.Result = Entity{Name: "Bob"}
|
||||
|
||||
if b.Sleep > 0 {
|
||||
time.Sleep(b.Sleep)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *GetName) Rollback(context.Context) error {
|
||||
b.Result = Entity{}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type GetAddress struct {
|
||||
Input *Entity
|
||||
|
||||
Result Entity
|
||||
}
|
||||
|
||||
func (b *GetAddress) CorrelationID() string {
|
||||
return b.Input.Name
|
||||
}
|
||||
|
||||
func (b *GetAddress) Execute(context.Context) error {
|
||||
b.Result = *b.Input
|
||||
if b.Result.Address != "" {
|
||||
return errors.New("already set")
|
||||
}
|
||||
|
||||
b.Result.Address = "London"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *GetAddress) Rollback(context.Context) error {
|
||||
b.Result = Entity{}
|
||||
|
||||
return nil
|
||||
}
|
||||
25
examples/basic/go.mod
Normal file
25
examples/basic/go.mod
Normal file
@@ -0,0 +1,25 @@
|
||||
module git.derfenix.pro/fenix/commander/examples/basic
|
||||
|
||||
go 1.22
|
||||
|
||||
replace (
|
||||
git.derfenix.pro/fenix/commander => ../../
|
||||
git.derfenix.pro/fenix/commander/inmemorycache => ../../inmemorycache
|
||||
)
|
||||
|
||||
require (
|
||||
git.derfenix.pro/fenix/commander v0.0.0-00010101000000-000000000000
|
||||
git.derfenix.pro/fenix/commander/inmemorycache v0.0.0-00010101000000-000000000000
|
||||
github.com/google/uuid v1.6.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
go.opentelemetry.io/otel v1.28.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.28.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.28.0 // indirect
|
||||
)
|
||||
29
examples/basic/go.sum
Normal file
29
examples/basic/go.sum
Normal file
@@ -0,0 +1,29 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
|
||||
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
|
||||
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
|
||||
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
|
||||
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
|
||||
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
25
examples/basic/service.go
Normal file
25
examples/basic/service.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.derfenix.pro/fenix/commander"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
c1 := GetName{UID: uuid.NewString()}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(c2.Result)
|
||||
}
|
||||
134
examples/basic/service_test.go
Normal file
134
examples/basic/service_test.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.derfenix.pro/fenix/commander"
|
||||
"git.derfenix.pro/fenix/commander/inmemorycache"
|
||||
)
|
||||
|
||||
func BenchmarkServiceNoop(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
c1 := GetName{UID: uuid.NewString()}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
_ = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServiceWithDelay(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
c1 := GetName{UID: uuid.NewString(), Sleep: time.Microsecond * 100}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
_ = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServiceWithRollbackNoop(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
c1 := GetName{UID: uuid.NewString()}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
commands = append(commands, &GetAddress{Input: &c2.Result})
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
_ = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServiceWithRollbackWithDelay(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
c1 := GetName{UID: uuid.NewString(), Sleep: time.Microsecond * 100}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
commands = append(commands, &GetAddress{Input: &c2.Result})
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
_ = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServiceWithCacheNoop(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
cmd = cmd.WithCache(inmemorycache.NewInMemoryCache())
|
||||
|
||||
c1 := GetName{UID: uuid.NewString()}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
_ = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServiceWithCacheWithDelay(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := commander.New(10)
|
||||
|
||||
cmd = cmd.WithCache(inmemorycache.NewInMemoryCache())
|
||||
|
||||
c1 := GetName{UID: uuid.NewString(), Sleep: time.Microsecond * 100}
|
||||
c2 := GetAddress{Input: &c1.Result}
|
||||
commands := []commander.Command{&c1, &c2}
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := cmd.Execute(ctx, "", commands...); err != nil {
|
||||
_ = err
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user