question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fail fast capability

See original GitHub issue

Hi,

I’m a big fan of this marvel of engineering. Recently integrating with a 3d party we detected that some requests run to long. The first go to was to see the documentation for FailSafe. Unfortunately no luck. Below is an example of use case we want to have.


    @Test
    public void the_duration_of_the_test_is_10ms() throws Exception {
         CircuitBreaker breaker = new CircuitBreaker()
                .withTimeout(10, TimeUnit.MILLISECONDS);

        Failsafe.with(breaker)
        .get(() -> {
            Thread.sleep(20000); // emulate the long running task
            return 0;
        });
    }

I expected withTimeout to help me here to cut the execution with a failure at 10ms. Is this something you guys have on the radar ? Or any work around will be appreciated

Thank you

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
ruslandercommented, Jul 20, 2017

@jhalterman thank you for quick reply. Here is what came up for now. Sharing it here maybe other people can find it useful or take it from here and push it forward.


public class FailFastPolicyTest {

    ExecutorService executorService = Executors.newCachedThreadPool();

    @Test
    public void fail_fast_policy_applied() throws Throwable {
        CircuitBreaker breaker = new CircuitBreaker();

        try {
            Failsafe.with(breaker)
                    .get(failFastPolicy(10, () -> {
                        Thread.sleep(20000);   // this is dangerous long running task we want to protect ourselfs from
                        return 0;
                    }));
        } catch (InternalServerErrorException e) {
        }

        assertTrue(breaker.isOpen());
    }

    @Test
    public void fast_succeeding() throws Throwable {
        Object result = failFastPolicy(3000, () -> new Object());
        assertNotNull(result);
    }

    @Test
    public void propagates_internal_exception() throws Throwable {

        Callable<Object> throwsEx = () -> {
            throw new InternalServerErrorException("This is an emulated exception");
        };

        try {
            failFastPolicy(1000, throwsEx);
        } catch (InternalServerErrorException e) {
            assertThat(e)
                    .isInstanceOf(InternalServerErrorException.class)
                    .hasMessage("This is an emulated exception");
        }
    }

    @Test
    public void when_threshold_exceeded_throws_internal_error() throws Throwable {

        Callable<Object> longRunningExecution = () -> {
            Thread.sleep(10000);
            return new Object();
        };

        try {
            failFastPolicy(100, longRunningExecution);
        } catch (InternalServerErrorException e) {
            assertThat(e)
                    .isInstanceOf(InternalServerErrorException.class)
                    .hasMessage("Execution aborted, exceeded allowed 100 threshold");
        }
    }
    
    private Callable<Object> failFastPolicy(long thresholdMilliseconds, Callable<Object> callable) throws Throwable{
        return ()-> {
            try {
                Future<Object> task = executorService.submit(callable);
                return task.get(thresholdMilliseconds, TimeUnit.MILLISECONDS);
            } catch (ExecutionException e) {
                throw new InternalServerErrorException("Internal exception ", e.getCause());
            } catch (TimeoutException e) {
                throw new InternalServerErrorException("Execution aborted, exceeded allowed " + thresholdMilliseconds + " threshold");
            }
        };
    }
}

0reactions
jhaltermancommented, Aug 12, 2019

A new Timeout policy has been merged which supports failing an execution, and optionally interrupting or cancelling it:

https://github.com/jhalterman/failsafe#timeouts

This was just released in 2.2.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Fail Faster -- And Why You Should - Forbes
Facilitate moderate (not too hard or too easy) challenges employees need to work through. Tolerate—indeed, embrace—failure as a necessary input ...
Read more >
Understanding the need to "Fail Fast". - LinkedIn
Failing fast does mean simply “going faster” or “working harder” as much as “understanding risks and being iterative”. Generally speaking, ...
Read more >
Fail-fast - Wikipedia
In systems design, a fail-fast system is one which immediately reports at its interface any condition that is likely to indicate a failure....
Read more >
Fail Fast - Martin Fowler
Failing fast is a nonintuitive technique: “failing immediately and visibly” sounds like it would make your software more fragile, but it actually makes...
Read more >
Fail-fast Is Failing... Fast! - ACM Queue
For more than 40 years, fail-fast has been the dominant way of achieving fault tolerance. In this approach, some mechanism is responsible ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found