Giter VIP home page Giter VIP logo

pspolly's Introduction

PSPolly

PSPolly is a PowerShell wrapper around Polly.

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.

Installation

Install-Module PSPolly

Usage

Retry

Retry a number of times.

$Policy = New-PollyPolicy -Retry -RetryCount 10
Invoke-PollyCommand -Policy $Policy -ScriptBlock {
    Write-Host "Trying.."
    throw "Failed"
}

Retry a number of times with a delay.

$Policy = New-PollyPolicy -Retry -RetryCount 3 -RetryWait @(
    New-TimeSpan -Seconds 3
    New-TimeSpan -Seconds 6
    New-TimeSpan -Seconds 9
)
Invoke-PollyCommand -Policy $Policy -ScriptBlock {
    Write-Host "Trying.."
    throw "Failed"
}

Retry a number of times with a configurable delay.

$Policy = New-PollyPolicy -Retry -RetryCount 3 -SleepDuration {
    $Random = Get-Random -Min 1 -Max 10
    New-TimeSpan -Seconds $Random
}
Invoke-PollyCommand -Policy $Policy -ScriptBlock {
    Write-Host "Trying.."
    throw "Failed"
}

Retry forever.

$Policy = New-PollyPolicy -RetryForever -SleepDuration {
    $Random = Get-Random -Min 1 -Max 10
    New-TimeSpan -Seconds $Random
}
Invoke-PollyCommand -Policy $Policy -ScriptBlock {
    Write-Host "Trying.."
    throw "Failed"
}

Circuit Breaker

Pause execution after a certain amount of exceptions occur. Circuit breakers do not catch exceptions but will stop executing the action once the circuit breaker has opened. The initial state of the circuit breaker is closed.

$Policy = New-PollyPolicy -CircuitBreaker -ExceptionsAllowedBeforeBreaking 3 -DurationOfBreak (New-TimeSpan -Seconds 5)
1..10 | ForEach-Object {
    try 
    {
        Invoke-PollyCommand -Policy $Policy -ScriptBlock {
            Write-Host "Trying.."
            throw "Failed"
        }
    }
    catch 
    {
        $_
    }
}

Cache

Cache data for the specified time frame.

An absolute expiration invalidates the cache after a specific amount of time.

$Policy = New-PollyPolicy -AbsoluteExpiration (Get-Date).AddHours(1)
Invoke-PollyCommand -Policy $Policy -ScriptBlock {
    Get-Process; Start-Sleep 10
} -OperationKey 'Absolute'

A sliding expiration invalidates the cache after it hasn't be accessed for the specified time frame.

$Policy = New-PollyPolicy -SlidingExpiration (New-TimeSpan -Seconds 20)
Invoke-PollyCommand -Policy $Policy -ScriptBlock {
    Get-Process; Start-Sleep 10
} -OperationKey 'Sliding'

Rate Limit

Prevent a particular command from being called too frequently.

$Policy = New-PollyPolicy -Executions 5 -PerTimeSpan (New-TimeSpan -Seconds 10)
1..5 | ForEach-Object {
    Invoke-PollyCommand -Policy $Policy -ScriptBlock {
        "Hello"
    } -OperationKey 'RateLimit'
}

Combine Policies

Combine policies to create more powerful handling.

$CircuitBreaker = New-PollyPolicy -CircuitBreaker -ExceptionsAllowedBeforeBreaking 3 -DurationOfBreak (New-TimeSpan -Seconds 5)
$Retry = New-PollyPolicy -Retry -RetryCount 10
$Policy = Join-PollyPolicy -Policy @($CircuitBreaker, $Retry)
1..10 | ForEach-Object {
    Invoke-PollyCommand -Policy $Policy -ScriptBlock {
        Write-Host "Trying.."
        throw "Failed"
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.