Skip to content

Commit

Permalink
Better doc file with shortcuts & rate limiter demo
Browse files Browse the repository at this point in the history
  • Loading branch information
theHamdiz committed Feb 4, 2025
1 parent 11bca53 commit feb076d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,71 @@ if err := sm_.Wait(); err != nil {
}
```

```go
// Or just use the global shortcut (for the pragmatists)
import "github.com/theHamdiz/it"

it.GracefulShutdown(ctx, server, 30*time.Second, done, func() {
// Your last words here
})

// Need a phoenix-like rebirth?
it.GracefulRestart(ctx, server, 30*time.Second, done, func() {
// Rise from the ashes
})
```

Handles shutdown signals (SIGINT, SIGTERM by default), manages cleanup tasks with timeouts, and ensures your program dies with dignity instead of just crashing.

Critical actions fail the whole shutdown if they fail, non-critical ones just log and continue, because some things aren't worth dying twice over.

Perfect for when you need your program to clean up after itself instead of leaving a mess for the OS to deal with.

### Rate Limiter - Traffic Control for Functions

```go
// The proper way (for when you need full control)
import "github.com/theHamdiz/it/rl"

limiter := rl.NewRateLimiter(time.Second, 10)
defer limiter.Close()

err := limiter.Execute(ctx, func() error {
return DoSomethingFast() // But not too fast
})

// Or better yet, with actual returns
result, err := rl.ExecuteRateLimited(limiter, ctx, func() (string, error) {
return GetSomethingQuickly() // Responsibly quick
})
```

```go
// The shortcut (for the rest of us)
import "github.com/theHamdiz/it"

// Turn any function into a well-behaved citizen
chillVersion := it.RateLimiter(time.Second, func() string {
return IWouldSpamThisIfICould()
}).(func() string)

// Usage:
result := chillVersion() // Now properly paced

// Waiting (but not forever)
success := it.WaitFor(5*time.Second, func() bool {
return systemIsReady() // Are we there yet?
})

if !success {
// Time to give up and go home
}
```

Perfect for when your functions need to learn some self-control, without all the ceremony.

Now go forth and rate limit responsibly. Your servers will thank you.

### Time Keeper - Time Tracking for the Obsessed

Because if you're not measuring it, you're just guessing.
Expand Down Expand Up @@ -254,6 +313,37 @@ for task := range tasks {
durations := atk.Wait()
```

```go
// The dirty shortcut approach!!
import "github.com/theHamdiz/it"

result := it.TimeFunction("critical-operation", func() string {
return DoSomethingWorthTiming()
})

// Block timing with defer
defer it.TimeBlock("expensive-stuff")()

// Custom timing callback
it.TimeFunctionWithCallback("important-task",
func() string {
return ExpensiveOperation()
},
func(d time.Duration) {
if d > time.Second {
alertSomeone() // Someone's being slow
},
)

// Parallel timing (for the impatient)
durations := it.TimeParallel("batch-process",
func() { task1() },
func() { task2() },
func() { task3() },
)
// Now you know which one to blame
```
Times your operations, logs the results, and lets you obsess over performance with minimal effort. Supports both synchronous and async operations, because sometimes you need to time multiple failures simultaneously.
Perfect for when you need to prove that it's not your code that's slow, it's everyone else's.
Expand Down
4 changes: 2 additions & 2 deletions it.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,9 @@ func GenerateSecret(numBytes int) string {
return hex.EncodeToString(bytes)
}

// ===================================================
// =======================================================
// Configuration - Making Things Configurable Since 2025
// ===================================================
// =======================================================

// InitFromEnv initializes logger settings from environment variables
func InitFromEnv() {
Expand Down

0 comments on commit feb076d

Please sign in to comment.