Skip to content

o1egl/syncx

Repository files navigation

License GoDoc Build Status Coverage Status

SyncX

SyncX is a GO library that extends standard sync package.

Install

$ go get -u github.com/o1egl/syncx

Usage

AdwancedWaitGroup

AWG - advanced version of wait group

Added features:

  • thread safe
  • integrated context.Context
  • execution timeout
  • ability to return errors
  • panic handling from goroutines
    wg := awg.AdvancedWaitGroup{}

    // Add first task
    wg.Add(func() error {
    	//Logic
    	return nil
    })

    // Add second task
    wg.Add(func() error {
    	//Another Logic
    	return nil
    })

    wg.Start()

    var err error
    // Taking one error make sense if you use *.SetStopOnError(true)* option - see below
    err = wg.SetStopOnError(true).Start().GetLastError()

    // Taking all errors
    var errs []error
    errs = wg.Start().GetAllErrors()    

Integrated with context.Context. It gives you ability to set timeouts and register cancel functions

    // SetTimeout defines timeout for all tasks
    SetTimeout(t time.Duration)

    // SetContext defines Context
    SetContext(t context.Context)

    // SetStopOnError stops waitgroup if any task returns error
    SetStopOnError(b bool)

    // Add adds new tasks into waitgroup
    Add(funcs ...WaitgroupFunc)

    // // Start runs tasks in separate goroutines and waits for their completion
    Start()

    // Reset performs cleanup task queue and reset state
    Reset()

    // // LastError returns last error that caught by execution process
    LastError()

    // AllErrors returns all errors that caught by execution process
    AllErrors()

    // Status returns result state
    Status()

Semaphore

    // NewSemaphore returns new Semaphore instance
    NewSemaphore(10)

    //Acquire acquires one permit, if its not available the goroutine will block till its available or Context.Done() occurs.
    //You can pass context.WithTimeout() to support timeoutable acquire.
    Acquire(ctx context.Context)

    //AcquireMany is similar to Acquire() but for many permits
    //Returns successfully acquired permits.
    AcquireMany(ctx context.Context, n int) (int, error)

    //Release releases one permit
    Release()

    //ReleaseMany releases many permits
    ReleaseMany(n int) error

    //AvailablePermits returns number of available unacquired permits
    AvailablePermits() int

    //DrainPermits acquires all available permits and return the number of permits acquired
    DrainPermits() (int, error)

Copyright, License & Contributors

Submitting a Pull Request

  1. Fork it.
  2. Open a Pull Request
  3. Enjoy a refreshing Diet Coke and wait

SyncX is released under the MIT license. See LICENSE