标签: fail-fast

优雅地处理损坏的状态异常

此问题相关,我想强制CLR让我的.NET 4.5.2应用程序捕获损坏的状态异常,其唯一目的是记录它们然后终止应用程序.如果我catch (Exception ex)在应用程序周围的几个地方,这样做的正确方法是什么?

因此,在我指定<legacyCorruptedStateExceptionsPolicy>属性后,如果我理解正确,所有catch (Exception ex)处理程序将捕获异常,AccessViolationException并愉快地继续.

是的,我知道catch (Exception ex)是一个坏主意™,但如果CLR至少将正确的堆栈跟踪放入事件日志中,我将非常乐意向客户解释他的服务器应用程序在凌晨1点快速失败并且离线晚上是件好事.但不幸的是,CLR将一个不相关的异常记录到事件日志中,然后关闭该过程,以便我无法找出实际发生的情况.

问题是,如何实现这一目标,流程广泛:

if the exception thrown is a Corrupted State Exception:
    - write the message to the log file
    - end the process 
Run Code Online (Sandbox Code Playgroud)

(更新)

换句话说,这可能适用于简单应用中的大多数例外:

[HandleProcessCorruptedStateExceptions] 
[SecurityCritical]
static void Main() // main entry point
{
    try 
    {

    }
    catch (Exception ex)
    {
        // this will catch CSEs
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于:

  • 未处理的应用程序域异常(即在非前台线程上抛出)
  • Windows服务应用程序(没有实际Main入口点)

所以这似乎<legacyCorruptedStateExceptionsPolicy>是使这项工作成功的唯一方法,在这种情况下,我不知道在记录CSE后如何失败?

.net c# access-violation fail-fast corrupted-state-exception

23
推荐指数
2
解决办法
6837
查看次数

如何将防御性编程技术结合在一起?

我想问你的问题很广泛但同时它非常具体.首先,我必须说,我最感兴趣的是适用于.net环境的答案.

好吧,我想提高我生成的代码级别.现在我主要使用TDD和静态代码分析来确保我的代码是正确的.最近我听了Dino Esposito关于代码合同的演讲,现在我想将它与其他技术结合使用.在听Dino的同时我也回忆起了Debug.Assert()Trace.Assert().

具体来说,我会问几个问题:

  • 我应该如何编写合同和单元测试以相互补充?
  • 我应该只在每种方法或公共方法中使用代码合同吗?
  • 我应该阻止使用Debug.Assert()吗?什么时候可以使用它们?(例如,请注意.net中的不变量仅在公共方法/属性出口处进行检查.那么,通过简单方法在方法中间进行一些检查是否可以Assert()?)
  • 你能否推荐我一个开源项目,所有这些技术都被正确使用,因为一张图片描绘了千言万语?

c# defensive-programming assert code-contracts fail-fast

21
推荐指数
1
解决办法
1133
查看次数

C# 12 中主构造函数的 Null 检查

我使用 C# 12。在 C# 12 中我可以使用主构造函数:

public class UserService(IUnitOfWork uow) : IUserService
{
}
Run Code Online (Sandbox Code Playgroud)

在 C# 12 之前,我对构造函数中注入的项使用 null 检查:

public class UserService : IUserService
{
    private readonly IUnitOfWork _uow;

    public UserService(IUnitOfWork uow)
    {
        ArgumentNullException.ThrowIfNull(uow);
        _uow = uow;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我如何在 C# 12 中进行 null 检查?
是否需要对主构造函数使用快速失败?

c# fail-fast primary-constructor .net-8.0 c#-12.0

19
推荐指数
1
解决办法
1919
查看次数

如果数据库关闭,如何使Spring服务器启动?

我正在使用Spring Boot(1.4.7)和MyBatis.

spring.main1.datasource.url=jdbc:mariadb://192.168.0.11:3306/testdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&socketTimeout=5000&connectTimeout=3000
spring.main1.datasource.username=username
spring.main1.datasource.password=password
spring.main1.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.main1.datasource.tomcat.test-on-borrow=true
spring.main1.datasource.tomcat.test-while-idle=true
spring.main1.datasource.tomcat.validation-query=SELECT 1
spring.main1.datasource.tomcat.validation-query-timeout=5000
spring.main1.datasource.tomcat.validation-interval=5000
spring.main1.datasource.tomcat.max-wait=5000
spring.main1.datasource.continue-on-error=true
Run Code Online (Sandbox Code Playgroud)

在Eclipse或Linux服务器上断开数据库时,我无法启动程序错误.(数据库不在localhost上.)

当我尝试使用断开连接的数据库启动程序时,打印此文件.

java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Stopping service [Tomcat]
Application startup failed
Run Code Online (Sandbox Code Playgroud)

有什么办法吗?

谢谢

java fail-fast spring-boot

14
推荐指数
2
解决办法
5991
查看次数

Hystrix配置

我正在尝试使用hystrix-javanica为我的应用程序实现hystrix.

我已经配置了hystrix-configuration.properties,如下所示

hystrix.command.default.execution.isolation.strategy=SEMAPHORE
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 
hystrix.command.default.fallback.enabled=true
hystrix.command.default.circuitBreaker.enabled=true
hystrix.command.default.circuitBreaker.requestVolumeThreshold=3 
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=50000
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
Run Code Online (Sandbox Code Playgroud)

短路模式工作正常,但我对此有疑问 hystrix.command.default.circuitBreaker.requestVolumeThreshold=3

  1. 是否在3次故障或之后打开电路
  2. 3次并发故障后打开电路.

通过文档链接

有人可以回答吗?

 

 

netflix short-circuiting circuit-breaker fail-fast hystrix

13
推荐指数
1
解决办法
1万
查看次数

如何在 Jenkins 的动态管道中使用 failFast

我有具有动态并行阶段的管道,如果任何阶段失败,我希望我的管道快速失败。我试图添加 failFast: true 但我的管道卡在“在阶段 ABC 失败”。

  stage("Deploy") {             
        steps {
            script {
                def stages = createStages("Name", "Project")
                fastFail: true
                for (stage in stages) {                        
                    parallel stage                      
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

fail-fast jenkins jenkins-pipeline

8
推荐指数
1
解决办法
9767
查看次数

为什么失败的快速风格节目比防守风格节目更短?

我已经读过关于像Erlang这样的语言编程失败的程序如何最终使用比大多数其他语言中的防御风格更短的程序.这对所有类型的程序都是正确的,这是什么原因?

erlang defensive-programming fail-fast

6
推荐指数
2
解决办法
1111
查看次数

失败快速迭代器

我得到了这个定义:正如名称所暗示的那样,一旦他们意识到自迭代开始以来已经改变了Collection的结构,那么失败快速迭代器就会失败.

迭代开始以来意味着什么?这意味着Iterator it = set.iterator()这行代码?

public static void customize(BufferedReader br) throws IOException{  
    Set<String> set=new HashSet<String>(); // Actual type parameter added  
    **Iterator it=set.iterator();**
Run Code Online (Sandbox Code Playgroud)

java collections iterator fail-fast

4
推荐指数
1
解决办法
3967
查看次数

Java容器是否提供故障安全迭代器

这是我的问题:

这段代码抛出一个java.util.ConcurrentModificationException,因为在Vector listeners存在Iterator这个数据结构时会被修改.java-doc说这个容器只提供一个快速失败的迭代器.

如果在"生命" 期间删除了一个元素,是否有可能获得Iterator像Java VectorListJava那样的标准容器Iterator,它不会失效(不是快速失败)Iterator

我应该像std::list在C++中一样具有相同的行为.即使删除了当前的迭代器,迭代器也始终有效.比迭代器设置为列表中的下一个元素.

public class ClientHandle {
private final Vector<ClientHandleListener> listeners = new Vector<ClientHandleListener>();


public synchronized void  addListener(ClientHandleListener chl) {
    listeners.add(chl);
}

public synchronized void  removeListener(ClientHandleListener chl) {
    listeners.remove(chl); 
}

private void fireConnectionClosed() {
    final ClientHandle c = this;

    final Iterator<ClientHandleListener> it = listeners.iterator();
    new Thread(){
        @Override
        public void run() {
            while (it.hasNext()) {
                it.next().connectionClosed(c); //FIXME the iterator gets …
Run Code Online (Sandbox Code Playgroud)

java iterator fail-fast

1
推荐指数
2
解决办法
2756
查看次数