Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel.
- https://github.com/duke-git/lancet/blob/main/concurrency/channel.go
- https://github.com/duke-git/lancet/blob/main/concurrency/keyed_locker.go
import ( "github.com/duke-git/lancet/v2/concurrency" )
Create a Channel pointer instance.
Signature:
type Channel[T any] struct func NewChannel[T any]() *Channel[T]
Example: Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { c := concurrency.NewChannel[int]() }
Link multiple channels into one channel until cancel the context.
Signature:
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T
Example: Run
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() c := concurrency.NewChannel[int]() genVals := func() <-chan <-chan int { out := make(chan (<-chan int)) go func() { defer close(out) for i := 1; i <= 5; i++ { stream := make(chan int, 1) stream <- i close(stream) out <- stream } }() return out } for v := range c.Bridge(ctx, genVals()) { fmt.Println(v) } // Output: // 1 // 2 // 3 // 4 // 5 }
Merge multiple channels into one channel until cancel the context.
Signature:
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T
Example: Run
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() c := concurrency.NewChannel[int]() channels := make([]<-chan int, 2) for i := 0; i < 2; i++ { channels[i] = c.Take(ctx, c.Repeat(ctx, i), 2) } chs := c.FanIn(ctx, channels...) for v := range chs { fmt.Println(v) //1 1 0 0 or 0 0 1 1 } }
Create channel, put values into the channel repeatly until cancel the context.
Signature: Run
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T
Example:
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() c := concurrency.NewChannel[int]() intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4) for v := range intStream { fmt.Println(v) } // Output: // 1 // 2 // 1 // 2 }
Creates a channel, then put values into the channel.
Signature:
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
Example: Run
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() c := concurrency.NewChannel[int]() intStream := c.Generate(ctx, 1, 2, 3) fmt.Println(<-intStream) fmt.Println(<-intStream) fmt.Println(<-intStream) // Output: // 1 // 2 // 3 }
Create a channel, excutes fn repeatly, and put the result into the channel, until close context.
Signature: Run
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T
Example:
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() fn := func() string { return "hello" } c := concurrency.NewChannel[string]() intStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3) for v := range intStream { fmt.Println(v) } // Output: // hello // hello // hello }
Read one or more channels into one channel, will close when any readin channel is closed.
Signature: Run
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T
Example:
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { sig := func(after time.Duration) <-chan any { c := make(chan any) go func() { defer close(c) time.Sleep(after) }() return c } start := time.Now() c := concurrency.NewChannel[any]() <-c.Or( sig(1*time.Second), sig(2*time.Second), sig(3*time.Second), ) fmt.Println("done after %v", time.Since(start)) //1.003s }
Read a channel into another channel, will close until cancel context.
Signature: Run
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T
Example:
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() c := concurrency.NewChannel[int]() intStream := c.Take(ctx, c.Repeat(ctx, 1), 3) for v := range c.OrDone(ctx, intStream) { fmt.Println(v) } // Output: // 1 // 1 // 1 }
Create a channel whose values are taken from another channel with limit number.
Signature: Run
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T
Example:
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() numbers := make(chan int, 5) numbers <- 1 numbers <- 2 numbers <- 3 numbers <- 4 numbers <- 5 defer close(numbers) c := concurrency.NewChannel[int]() intStream := c.Take(ctx, numbers, 3) for v := range intStream { fmt.Println(v) } // Output: // 1 // 2 // 3 }
Split one chanel into two channels, until cancel the context.
Signature: Run
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)
Example:
package main import ( "context" "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() c := concurrency.NewChannel[int]() intStream := c.Take(ctx, c.Repeat(ctx, 1), 2) ch1, ch2 := c.Tee(ctx, intStream) for v := range ch1 { fmt.Println(v) fmt.Println(<-ch2) } // Output: // 1 // 1 // 1 // 1 }
KeyedLocker is a simple implementation of a keyed locker that allows for non-blocking lock acquisition.
Signature:
func NewKeyedLocker[K comparable](ttl time.Duration) *KeyedLocker[K]
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewKeyedLocker[string](2 * time.Second) task := func() { fmt.Println("Executing task...") time.Sleep(1 * time.Second) fmt.Println("Task completed.") } ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() if err := locker.Do(ctx, "mykey", task); err != nil { log.Fatalf("Error executing task: %v\n", err) } else { fmt.Println("Task successfully executed.") } ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Second) defer cancel2() if err := locker.Do(ctx2, "mykey", task); err != nil { log.Fatalf("Error executing task: %v\n", err) } else { fmt.Println("Task successfully executed.") } // Output: // Executing task... // Task completed. // Task successfully executed. // Executing task... // Task completed. // Task successfully executed. }
Acquires a lock for the specified key and executes the provided function.
Signature:
func (l *KeyedLocker[K]) Do(ctx context.Context, key K, fn func()) error
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewKeyedLocker[string](2 * time.Second) task := func() { fmt.Println("Executing task...") time.Sleep(1 * time.Second) fmt.Println("Task completed.") } ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() if err := locker.Do(ctx, "mykey", task); err != nil { log.Fatalf("Error executing task: %v\n", err) } else { fmt.Println("Task successfully executed.") } ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Second) defer cancel2() if err := locker.Do(ctx2, "mykey", task); err != nil { log.Fatalf("Error executing task: %v\n", err) } else { fmt.Println("Task successfully executed.") } // Output: // Executing task... // Task completed. // Task successfully executed. // Executing task... // Task completed. // Task successfully executed. }
RWKeyedLocker is a read-write version of KeyedLocker.
Signature:
func NewRWKeyedLocker[K comparable](ttl time.Duration) *RWKeyedLocker[K]
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewRWKeyedLocker[string](2 * time.Second) // Simulate a key key := "resource_key" fn := func() { fmt.Println("Starting write operation...") // Simulate write operation, assuming it takes 2 seconds time.Sleep(200 * time.Millisecond) fmt.Println("Write operation completed!") } // Acquire the write lock and execute the operation ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() // Execute the lock operation with a 3-second timeout err := locker.Lock(ctx, key, fn) if err != nil { return } //output: //Starting write operation... //Write operation completed! }
Acquires a read lock for the specified key and executes the provided function.
Signature:
func (l *RWKeyedLocker[K]) RLock(ctx context.Context, key K, fn func()) error
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewRWKeyedLocker[string](2 * time.Second) // Simulate a key key := "resource_key" fn := func() { fmt.Println("Starting write operation...") // Simulate write operation, assuming it takes 2 seconds time.Sleep(200 * time.Millisecond) fmt.Println("Write operation completed!") } // Acquire the write lock and execute the operation ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() // Execute the lock operation with a 3-second timeout err := locker.RLock(ctx, key, fn) if err != nil { return } //output: //Starting write operation... //Write operation completed! }
Acquires a write lock for the specified key and executes the provided function.
Signature:
func (l *RWKeyedLocker[K]) Lock(ctx context.Context, key K, fn func()) error
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := NewRWKeyedLocker[string](2 * time.Second) // Simulate a key key := "resource_key" fn := func() { fmt.Println("Starting write operation...") // Simulate write operation, assuming it takes 2 seconds time.Sleep(200 * time.Millisecond) fmt.Println("Write operation completed!") } // Acquire the write lock and execute the operation ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() // Execute the lock operation with a 3-second timeout err := locker.Lock(ctx, key, fn) if err != nil { return } //output: //Starting write operation... //Write operation completed! }
TryKeyedLocker is a non-blocking version of KeyedLocker.
Signature:
func NewTryKeyedLocker[K comparable]() *TryKeyedLocker[K]
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewTryKeyedLocker[string]() key := "resource_key" if locker.TryLock(key) { fmt.Println("Lock acquired") time.Sleep(1 * time.Second) // Unlock after work is done locker.Unlock(key) fmt.Println("Lock released") } else { fmt.Println("Lock failed") } //output: //Lock acquired //Lock released }
TryLock tries to acquire a lock for the specified key.
Signature:
func (l *TryKeyedLocker[K]) TryLock(key K) bool
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewTryKeyedLocker[string]() key := "resource_key" if locker.TryLock(key) { fmt.Println("Lock acquired") time.Sleep(1 * time.Second) // Unlock after work is done locker.Unlock(key) fmt.Println("Lock released") } else { fmt.Println("Lock failed") } //output: //Lock acquired //Lock released }
Unlock releases the lock for the specified key.
Signature:
func (l *TryKeyedLocker[K]) Unlock(key K)
Example:Run
package main import ( "fmt" "github.com/duke-git/lancet/v2/concurrency" ) func main() { locker := concurrency.NewTryKeyedLocker[string]() key := "resource_key" if locker.TryLock(key) { fmt.Println("Lock acquired") time.Sleep(1 * time.Second) // Unlock after work is done locker.Unlock(key) fmt.Println("Lock released") } else { fmt.Println("Lock failed") } //output: //Lock acquired //Lock released }