go-sync-utils, v1.2
Posted on

The library that provides utility entities for syncing.
Wrapper for an abstract handler allowed to call it concurrently.
Change Log
- wrapper for an abstract handler allowed to call it concurrently:
- starting:
- start in the caller goroutine;
- start in a goroutine pool;
- stopping:
- can be called after both kinds of the starting;
- blocks the execution flow until the stopping will be completed.
- starting:
Features
- utility entities for the
sync.WaitGrouptype:- interface of the
sync.WaitGrouptype; - operating with a set of such interfaces as a whole;
- interface of the
- sending to a channel without blocking even if the channel is busy;
- wrapper for an abstract handler allowed to call it concurrently:
- starting:
- start in the caller goroutine;
- start in a goroutine pool;
- stopping:
- can be called after both kinds of the starting;
- blocks the execution flow until the stopping will be completed.
- starting:
Examples
syncutils.ConcurrentHandler:
package main
import (
"context"
"fmt"
"runtime"
"sync"
syncutils "github.com/thewizardplusplus/go-sync-utils"
)
type Handler struct {
Locker sync.Mutex
DataGroup []interface{}
}
func (handler *Handler) Handle(ctx context.Context, data interface{}) {
handler.Locker.Lock()
defer handler.Locker.Unlock()
handler.DataGroup = append(handler.DataGroup, data)
}
func main() {
// start the data handling
var innerHandler Handler
concurrentHandler := syncutils.NewConcurrentHandler(1000, &innerHandler)
go concurrentHandler.StartConcurrently(context.Background(), runtime.NumCPU())
// handle the data
for index := 0; index < 10; index++ {
data := fmt.Sprintf("data #%d", index)
concurrentHandler.Handle(data)
}
concurrentHandler.Stop()
// print the handled data
for _, data := range innerHandler.DataGroup {
fmt.Println(data)
}
// Unordered output:
// data #0
// data #1
// data #2
// data #3
// data #4
// data #5
// data #6
// data #7
// data #8
// data #9
}
Repository
Link: https://github.com/thewizardplusplus/go-sync-utils/tree/v1.2.
Content: code.
License: MIT.