https://github.com/copyleftdev/randexc
A Go library for executing actions at random times within specified durations. Perfect for load testing, simulating real-world events, and implementing jittered backoff strategies.
https://github.com/copyleftdev/randexc
backoff execution go jitter random testing
Last synced: 3 months ago
JSON representation
A Go library for executing actions at random times within specified durations. Perfect for load testing, simulating real-world events, and implementing jittered backoff strategies.
- Host: GitHub
- URL: https://github.com/copyleftdev/randexc
- Owner: copyleftdev
- License: mit
- Created: 2024-08-25T09:50:52.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-08-25T10:01:33.000Z (9 months ago)
- Last Synced: 2024-08-26T11:14:18.086Z (9 months ago)
- Topics: backoff, execution, go, jitter, random, testing
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# randexc: Random Execution Library for Go
[](https://goreportcard.com/report/github.com/copyleftdev/randexc)
[](https://godoc.org/github.com/copyleftdev/randexc)
[](https://opensource.org/licenses/MIT)`randexc` is a powerful and flexible Go library for executing actions at random times within a specified duration. It provides both synchronous and asynchronous execution options, making it suitable for a wide range of applications including load testing, simulating real-world events, and implementing sophisticated retry mechanisms.
## Features
- ๐ Execute actions within a random time frame
- ๐ Support for both synchronous and asynchronous execution
- ๐ Configurable through functional options
- ๐งช Easy to test with custom random sources
- ๐ฆ Lightweight with no external dependencies## Installation
```bash
go get github.com/copyleftdev/randexc
```## Quick Start
```go
package mainimport (
"context"
"fmt"
"log""github.com/copyleftdev/randexc/pkg/randexc"
)func main() {
executor, err := randexc.New("1m")
if err != nil {
log.Fatalf("Failed to create executor: %v", err)
}err = executor.Execute(context.Background(), func() error {
fmt.Println("Action executed at a random time within 1 minute!")
return nil
})if err != nil {
log.Printf("Execution failed: %v", err)
}
}
```## Use Cases
### Load Testing
Simulate realistic user behavior in load tests by executing actions at random intervals:
```go
func performRequest() error {
// Simulate HTTP request
time.Sleep(100 * time.Millisecond)
return nil
}executor, _ := randexc.New("5s")
for i := 0; i < 100; i++ {
go func() {
err := executor.Execute(context.Background(), performRequest)
if err != nil {
log.Printf("Request failed: %v", err)
}
}()
}
```### Jittered Exponential Backoff
Implement a jittered exponential backoff strategy for retrying operations:
```go
func retryWithBackoff(operation func() error) error {
baseDelay := 100 * time.Millisecond
maxDelay := 10 * time.Second
maxAttempts := 5for attempt := 0; attempt < maxAttempts; attempt++ {
jitteredDelay := time.Duration(float64(baseDelay) * (1 + rand.Float64()))
if jitteredDelay > maxDelay {
jitteredDelay = maxDelay
}executor, _ := randexc.New(jitteredDelay.String())
err := executor.Execute(context.Background(), operation)
if err == nil {
return nil
}baseDelay *= 2
}return fmt.Errorf("operation failed after %d attempts", maxAttempts)
}
```### Simulating Real-world Events
Create more realistic simulations by introducing randomness in event timing:
```go
func simulateIoTDevice(deviceID string) {
executor, _ := randexc.New("1h")for {
executor.Execute(context.Background(), func() error {
temperature := 20 + rand.Float64()*10
humidity := 30 + rand.Float64()*20
fmt.Printf("Device %s: Temp: %.2fยฐC, Humidity: %.2f%%\n", deviceID, temperature, humidity)
return nil
})
}
}// Simulate multiple IoT devices
for i := 0; i < 5; i++ {
go simulateIoTDevice(fmt.Sprintf("device_%d", i))
}
```## Documentation
For more detailed documentation and advanced usage examples, please see the [usage guide](docs/usage.md).
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.