Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Apr 22, 2024
1 parent a73f682 commit ea09a69
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
19 changes: 15 additions & 4 deletions rxlib-x/src/main/java/org/rx/redis/RedisUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.rx.redis;

import com.google.common.util.concurrent.RateLimiter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.rx.core.Cache;
import org.rx.core.Strings;
import org.rx.core.Sys;
Expand All @@ -14,6 +16,15 @@

@Slf4j
public class RedisUtil {
public static Lock wrapLock(@NonNull RedissonClient redissonClient, @NonNull String lockName) {
return Sys.fallbackProxy(Lock.class, new Lazy<>(() -> redissonClient.getLock(lockName)), new Lazy<>(ReentrantLock::new), e -> {
if (Strings.hashEquals(e.getMethod().getName(), "unlock")) {
return null;
}
throw e;
});
}

public static Lock wrapLock(RLock rLock) {
return Sys.fallbackProxy(Lock.class, rLock, new Lazy<>(ReentrantLock::new), e -> {
if (Strings.hashEquals(e.getMethod().getName(), "unlock")) {
Expand All @@ -24,14 +35,14 @@ public static Lock wrapLock(RLock rLock) {
});
}

public static <TK, TV> Cache<TK, TV> wrapCache(RedisCache<TK, TV> rCache) {
return Sys.fallbackProxy(Cache.class, rCache, new Lazy<>(() -> Cache.getInstance(MemoryCache.class)));
}

public static RateLimiterAdapter wrapRateLimiter(RedisRateLimiter rRateLimiter) {
return Sys.fallbackProxy(RateLimiterAdapter.class, rRateLimiter, new Lazy<>(() -> {
RateLimiter limiter = RateLimiter.create(rRateLimiter.getPermitsPerSecond());
return () -> limiter.tryAcquire();
}));
}

public static <TK, TV> Cache<TK, TV> wrapCache(RedisCache<TK, TV> rCache) {
return Sys.fallbackProxy(Cache.class, rCache, new Lazy<>(() -> Cache.getInstance(MemoryCache.class)));
}
}
42 changes: 26 additions & 16 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,39 @@ public static File getJarFile(Class<?> klass) {
return new File(path);
}

public static <T> T fallbackProxy(Class<T> targetType, Lazy<T> target, Lazy<T> fallbackTarget) {
return fallbackProxy(targetType, target, fallbackTarget, null);
}

public static <T> T fallbackProxy(@NonNull Class<T> targetType, Lazy<T> target, Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
return proxy(targetType, (m, p) -> innerFallbackProxy(m, p, target.getValue(), fallbackTarget, onError));
}

public static <T> T fallbackProxy(Class<T> targetType, T target, Lazy<T> fallbackTarget) {
return fallbackProxy(targetType, target, fallbackTarget, null);
}

public static <T> T fallbackProxy(@NonNull Class<T> targetType, @NonNull T target, @NonNull Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
return proxy(targetType, (m, p) -> {
public static <T> T fallbackProxy(@NonNull Class<T> targetType, T target, Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
return proxy(targetType, (m, p) -> innerFallbackProxy(m, p, target, fallbackTarget, onError));
}

static <T> Object innerFallbackProxy(Method m, DynamicProxyBean p, @NonNull T target, @NonNull Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
try {
return p.fastInvoke(target);
} catch (Throwable e) {
T fallbackTargetValue = fallbackTarget.getValue();
try {
return p.fastInvoke(target);
} catch (Throwable e) {
T value = fallbackTarget.getValue();
try {
Object r = p.fastInvoke(value);
log.warn("fallbackProxy", e);
return r;
} catch (Throwable fe) {
FallbackException fb = new FallbackException(m, p, target, value, e, fe);
if (onError == null) {
throw fb;
}
return onError.invoke(fb);
Object r = p.fastInvoke(fallbackTargetValue);
log.warn("fallbackProxy", e);
return r;
} catch (Throwable fe) {
FallbackException fb = new FallbackException(m, p, target, fallbackTargetValue, e, fe);
if (onError == null) {
throw fb;
}
return onError.apply(fb);
}
});
}
}

public static <T> T targetObject(Object proxyObject) {
Expand Down

0 comments on commit ea09a69

Please sign in to comment.