You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.4 KiB
C#
98 lines
2.4 KiB
C#
using System.Collections.Concurrent;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace HttpClientStudy.WebApp.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Polly V8 控制器
|
|
/// </summary>
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class Polly8Controller : ControllerBase
|
|
{
|
|
private static ConcurrentDictionary<string, bool> ToggleExceptionCache = new ConcurrentDictionary<string, bool>();
|
|
|
|
private readonly ILogger<Polly8Controller> _logger;
|
|
|
|
/// <summary>
|
|
/// 构造
|
|
/// </summary>
|
|
/// <param name="logger">日志</param>
|
|
public Polly8Controller(ILogger<Polly8Controller> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Polly8Hello
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public ActionResult Hello()
|
|
{
|
|
return Ok("Polly v8");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异常接口
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public ActionResult Exception()
|
|
{
|
|
throw new HttpRequestException("服务器异常");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重试策略异常
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public ActionResult RetryException()
|
|
{
|
|
throw new HttpRequestException("服务器异常");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 随机异常
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public ActionResult RandomException()
|
|
{
|
|
var num = Random.Shared.Next(1, 100);
|
|
if (num >= 50)
|
|
{
|
|
throw new HttpRequestException("服务器随机异常");
|
|
}
|
|
else
|
|
{
|
|
return Ok(num);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换异常
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public ActionResult ToggleException(string toggleId = "")
|
|
{
|
|
var toggle = ToggleExceptionCache.GetOrAdd(toggleId, true);
|
|
//保存切换
|
|
ToggleExceptionCache[toggleId] = !toggle;
|
|
|
|
if (toggle)
|
|
{
|
|
throw new HttpRequestException("服务器随机异常");
|
|
}
|
|
else
|
|
{
|
|
return Ok($"ToggleException toggleId={toggleId}");
|
|
}
|
|
}
|
|
}
|
|
}
|