feat: add example

This commit is contained in:
sujit
2024-10-10 00:17:36 +05:45
parent 49996e7992
commit 48fdb7d67c
2 changed files with 22 additions and 7 deletions

View File

@@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/oarkflow/mq/consts"
"github.com/oarkflow/mq/examples/tasks"
"io"
"net/http"
"github.com/oarkflow/mq/consts"
"github.com/oarkflow/mq/examples/tasks"
"github.com/oarkflow/mq"
"github.com/oarkflow/mq/dag"
)
@@ -24,10 +25,8 @@ func main() {
d.AddNode("C", tasks.Node3)
d.AddNode("D", tasks.Node4)
d.AddNode("E", tasks.Node5)
err := d.AddDeferredNode("F")
if err != nil {
panic(err)
}
d.AddNode("F", tasks.Node6)
d.AddEdge("A", "B", dag.LoopEdge)
d.AddCondition("C", map[string]string{"PASS": "D", "FAIL": "E"})
d.AddEdge("B", "C")
@@ -35,7 +34,7 @@ func main() {
d.AddEdge("E", "F")
http.HandleFunc("POST /publish", requestHandler("publish"))
http.HandleFunc("POST /request", requestHandler("request"))
err = d.Start(context.TODO(), ":8083")
err := d.Start(context.TODO(), ":8083")
if err != nil {
panic(err)
}

View File

@@ -2,6 +2,7 @@ package utils
import (
"math/rand"
"reflect"
"time"
)
@@ -9,3 +10,18 @@ func CalculateJitter(baseDelay time.Duration, percent float64) time.Duration {
jitter := time.Duration(rand.Float64()*percent*float64(baseDelay)) - time.Duration(percent*float64(baseDelay)/2)
return baseDelay + jitter
}
func SizeOf(v any) uintptr {
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
switch val.Kind() {
case reflect.Slice:
return uintptr(val.Len()) * val.Type().Elem().Size()
case reflect.Map:
return uintptr(val.Len()) * (val.Type().Key().Size() + val.Type().Elem().Size())
default:
return val.Type().Size()
}
}