Type-safe batch task processing for Kratos with *errkratos.Erk error handling.
Built on egobatch generic foundation.
🎯 Kratos Integration: Specialized with *errkratos.Erk error type
⚡ Batch Processing: Concurrent task execution with type-safe errors
🔄 Flexible Modes: Glide mode and fast-exit mode
🌍 Context Support: Complete context propagation and timeout handling
📋 Result Filtering: OkTasks/WaTasks methods in result aggregation
go get github.com/yylego/kratos-ego/egokratospackage main
import (
"context"
"fmt"
"time"
"github.com/yylego/kratos-ego/erkgroup"
"github.com/yylego/kratos-errors"
)
func main() {
ctx := context.Background()
ego := erkgroup.NewGroup(ctx)
// Add task 1: takes 100ms to finish
ego.Go(func(ctx context.Context) *errkratos.Erk {
time.Sleep(100 * time.Millisecond)
fmt.Println("Task 1 finished OK")
return nil
})
// Add task 2: takes 50ms to finish
ego.Go(func(ctx context.Context) *errkratos.Erk {
time.Sleep(50 * time.Millisecond)
fmt.Println("Task 2 finished OK")
return nil
})
// Add task 3: takes 80ms to finish
ego.Go(func(ctx context.Context) *errkratos.Erk {
time.Sleep(80 * time.Millisecond)
fmt.Println("Task 3 finished OK")
return nil
})
// Wait until tasks finish and get the first error
if erk := ego.Wait(); erk != nil {
fmt.Printf("Got error: %s\n", erk.Error())
} else {
fmt.Println("Tasks finished OK")
}
}⬆️ Source: Source
package main
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v3/errors"
"github.com/yylego/kratos-ego"
"github.com/yylego/kratos-ego/erkgroup"
"github.com/yylego/kratos-errors/must/erkmust"
)
func main() {
// Create batch with arguments
args := []int{1, 2, 3, 4, 5}
batch := egokratos.NewTaskBatch[int, string](args)
// Configure glide mode - keep going even when errors happen
batch.SetGlide(true)
// Execute batch tasks
ctx := context.Background()
ego := erkgroup.NewGroup(ctx)
batch.EgoRun(ego, func(ctx context.Context, num int) (string, *errors.Error) {
if num%2 == 0 {
// Even numbers finish OK
return fmt.Sprintf("even-%d", num), nil
}
// Odd numbers have errors
return "", errors.BadRequest("ODD_NUMBER", "odd number")
})
// In glide mode, ego.Wait() returns nil because errors are captured in tasks
erkmust.Done(ego.Wait())
// Get and handle task results
okTasks := batch.Tasks.OkTasks()
waTasks := batch.Tasks.WaTasks()
fmt.Printf("Success: %d, Failed: %d\n", len(okTasks), len(waTasks))
// Show OK results
for _, task := range okTasks {
fmt.Printf("Arg: %d -> Result: %s\n", task.Arg, task.Res)
}
// Show failed results
for _, task := range waTasks {
fmt.Printf("Arg: %d -> Error: %s\n", task.Arg, task.Erx.Error())
}
}⬆️ Source: Source
Type-safe errgroup for Kratos:
type Group = erxgroup.Group[*errkratos.Erk]
func NewGroup(ctx context.Context) *GroupBatch task execution:
type TaskBatch[A, R] = egobatch.TaskBatch[A, R, *errkratos.Erk]
func NewTaskBatch[A, R](args []A) *TaskBatch[A, R]Methods:
SetGlide(bool)- Configure execution modeSetWaCtx(func(error) *errkratos.Erk)- Handle context errorsEgoRun(ego, func)- Run batch with errgroup
Task collection with filtering:
type Tasks[A, R] = egobatch.Tasks[A, R, *errkratos.Erk]Methods:
OkTasks()- Get success tasksWaTasks()- Get failed tasksFlatten(func)- Transform results
See examples for complete demos:
- example1 - Guest order processing
- example2 - Student score processing
- example3 - Multi-step pipeline
egokratos is built on top of egobatch using type aliases:
// egokratos provides Kratos-specific types
type Task[A, R] = egobatch.Task[A, R, *errkratos.Erk]
type Tasks[A, R] = egobatch.Tasks[A, R, *errkratos.Erk]
type TaskBatch[A, R] = egobatch.TaskBatch[A, R, *errkratos.Erk]This approach:
- ✅ Reduces code duplication
- ✅ Maintains type-safe operations
- ✅ Provides Kratos-optimized API
- ✅ Benefits from egobatch improvements
MIT License - see LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- 🐛 Mistake reports? Open an issue on GitHub with reproduction steps
- 💡 Fresh ideas? Create an issue to discuss
- 📖 Documentation confusing? Report it so we can improve
- 🚀 Need new features? Share the use cases to help us understand requirements
- ⚡ Performance issue? Help us optimize through reporting slow operations
- 🔧 Configuration problem? Ask questions about complex setups
- 📢 Follow project progress? Watch the repo to get new releases and features
- 🌟 Success stories? Share how this package improved the workflow
- 💬 Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- ⭐ Give GitHub stars if this project helps you
- 🤝 Share with teammates and (golang) programming friends
- 📝 Write tech blogs about development tools and workflows - we provide content writing support
- 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! 🎉🎉🎉