[Hystrix authoritative guide four] Hystrix isolation strategy source code analysis two

Signal isolation    
          The TryableSemaphore  interface defines the behavior of signal isolation, and internally allocates resources with the help of the AtomicInteger class. HystrixProperty<Integer> numberOfPermits stores allocable resources, and AtomicInteger count stores allocated resources. numberOfPermits needs to be assigned when the class is initialized, so it is defined as a final type.
        protected final HystrixProperty<Integer> numberOfPermits;
        private final AtomicInteger count = new AtomicInteger(0);  
Application Resources
      tryAcquire() is responsible for resource allocation. When there is a resource application request, execute the incrementAndGet() operation on count, if the return value is greater than the value of numberOfPermits, execute decrementAndGet to roll back the previous increment operation, and return false, indicating that the application for resources failed; if the return value is not greater than numberOfPermits value, it indicates that the resource application is successful, and returns true. See the code for details:
        public boolean tryAcquire() {
            int currentCount = count.incrementAndGet();
            if (currentCount > numberOfPermits.get()) {
                return false;
            } else {
                return true;
release resources
    After the logic execution is completed, the resources should be released so that other threads can obtain the resources. release() is responsible for the release of resources. When there is a resource release request, perform the decrementAndGet() operation on the count.
      When the same service has different resource dependencies, semaphore isolation can be selected to reduce the performance consumption caused by thread scheduling.
At this point, the application and release of semaphore resources are finished.

Guess you like