Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

kratos-ego

Type-safe batch task processing for Kratos with *errkratos.Erk error handling.

Built on egobatch generic foundation.


CHINESE README

中文说明

Features

🎯 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

Installation

go get github.com/yylego/kratos-ego/egokratos

Quick Start

Basic errgroup with Kratos Errors

package 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

Batch Task Processing

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

Core Components

erkgroup.Group

Type-safe errgroup for Kratos:

type Group = erxgroup.Group[*errkratos.Erk]

func NewGroup(ctx context.Context) *Group

TaskBatch[A, R]

Batch 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 mode
  • SetWaCtx(func(error) *errkratos.Erk) - Handle context errors
  • EgoRun(ego, func) - Run batch with errgroup

Tasks[A, R]

Task collection with filtering:

type Tasks[A, R] = egobatch.Tasks[A, R, *errkratos.Erk]

Methods:

  • OkTasks() - Get success tasks
  • WaTasks() - Get failed tasks
  • Flatten(func) - Transform results

Examples

See examples for complete demos:

Relationship with egobatch

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

📄 License

MIT License - see LICENSE.


💬 Contact & Feedback

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

🔧 Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

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! 🎉🎉🎉


GitHub Stars

Stargazers

About

Concurrent task execution with Kratos mistake propagation using errgroup and batch processing

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages