Stop using
go func
, start usinggofuncy
!
- ctx as a first class citizen
- error return as a first class citizen
- optional: enable telemetry (metrics & traces)
gofuncy.routine.count
countergofuncy.routine.duration
histogramgofuncy.channel.sent.count
countergofuncy.channel.sent.duration
histogram
Environment variables:
OTEL_ENABLED
: enable telemetryGOFUNCY_CHANNEL_VALUE_EVENTS_ENABLED
: creates a span event for every value sent into the channelGOFUNCY_CHANNEL_VALUE_ATTRIBUTE_ENABLED
: adds the json dump of the data to the span event
From:
package main
func main() {
go func() {
numbers, err := GenerateNumbers(5)
if err != nil {
panic(err)
}
}()
}
To:
package main
import (
"github.com/foomo/gofuncy"
)
func main() {
errChan := gofuncy.Go(func(ctx context.Context) error {
numbers, err := GenerateNumbers(5)
return err
})
if err := <-errChan; err != nil {
panic(err)
}
}
Each routine can return an error that is being returned through an error channel:
errChan := gofuncy.Go(func (ctx context.Context) error {
return nil
})
if err := <- errChan; err != nil {
panic(err)
}
Each routine will receive its own base context, which can be set:
errChan := gofuncy.Go(send(msg), gofuncy.WithContext(context.Background()))
flowchart TB
subgraph root
channel[Channel]
subgraph "Routine A"
ctxA[ctx] --> senderA
senderA[Sender]
end
subgraph "Routine B"
ctxB[ctx] --> senderB
senderB[Sender]
end
senderA --> channel
senderB --> channel
channel --> receiverC
subgraph "Routine C"
ctxC[ctx] --> receiverC
receiverC[Receiver]
end
end
Using the context we will inject a name for the process so that it can always be identified:
flowchart TB
subgraph root
channel[Channel]
subgraph "Routine A"
ctxA[ctx] -- ctx: sender - a --> senderA
senderA[Sender]
end
subgraph "Routine B"
ctxB[ctx] -- ctx: sender - b --> senderB
senderB[Sender]
end
senderA --> channel
senderB --> channel
channel --> receiverC
subgraph "Routine C"
ctxC[ctx] -- ctx: receiver - b --> receiverC
receiverC[Receiver]
end
end
Metrics:
Name | Type |
---|---|
gofuncy.routine.count |
UpDownCounter |
gofuncy.routine.duration |
Histogram |
flowchart TB
subgraph root
subgraph rA ["Routine A"]
handler[Handler]
end
rA -- gofuncy . routine . count --> Metrics
rA -- gofuncy . routine . duration --> Metrics
rA -- span: routine - a --> Trace
end
Make a pull request...
Distributed under MIT License, please see license file within the code for more details.