2121import static com .google .common .base .Preconditions .checkArgument ;
2222import static java .util .concurrent .TimeUnit .SECONDS ;
2323import static org .apache .commons .lang3 .StringUtils .isBlank ;
24+ import com .github .benmanes .caffeine .cache .CacheLoader ;
25+ import com .github .benmanes .caffeine .cache .Caffeine ;
26+ import com .github .benmanes .caffeine .cache .LoadingCache ;
2427import com .google .common .collect .BoundType ;
2528import com .google .common .collect .Range ;
2629import com .google .common .collect .Sets ;
2730import java .net .MalformedURLException ;
2831import java .net .URI ;
2932import java .net .URL ;
33+ import java .time .Duration ;
3034import java .util .ArrayList ;
3135import java .util .List ;
3236import java .util .Optional ;
8690import org .apache .pulsar .common .policies .path .PolicyPath ;
8791import org .apache .pulsar .common .util .FutureUtil ;
8892import org .apache .pulsar .metadata .api .MetadataStoreException ;
93+ import org .checkerframework .checker .nullness .qual .NonNull ;
94+ import org .checkerframework .checker .nullness .qual .Nullable ;
8995import org .slf4j .Logger ;
9096import org .slf4j .LoggerFactory ;
9197
@@ -96,6 +102,17 @@ public abstract class PulsarWebResource {
96102
97103 private static final Logger log = LoggerFactory .getLogger (PulsarWebResource .class );
98104
105+ private static final LoadingCache <String , PulsarServiceNameResolver > SERVICE_NAME_RESOLVER_CACHE =
106+ Caffeine .newBuilder ().expireAfterWrite (Duration .ofMinutes (5 )).build (
107+ new CacheLoader <>() {
108+ @ Override
109+ public @ Nullable PulsarServiceNameResolver load (@ NonNull String serviceUrl ) throws Exception {
110+ PulsarServiceNameResolver serviceNameResolver = new PulsarServiceNameResolver ();
111+ serviceNameResolver .updateServiceUrl (serviceUrl );
112+ return serviceNameResolver ;
113+ }
114+ });
115+
99116 static final String ORIGINAL_PRINCIPAL_HEADER = "X-Original-Principal" ;
100117
101118 @ Context
@@ -476,17 +493,21 @@ protected void validateClusterOwnership(String cluster) throws WebApplicationExc
476493
477494 private URI getRedirectionUrl (ClusterData differentClusterData ) throws MalformedURLException {
478495 try {
479- PulsarServiceNameResolver serviceNameResolver = new PulsarServiceNameResolver () ;
496+ PulsarServiceNameResolver serviceNameResolver ;
480497 if (isRequestHttps () && pulsar .getConfiguration ().getWebServicePortTls ().isPresent ()
481498 && StringUtils .isNotBlank (differentClusterData .getServiceUrlTls ())) {
482- serviceNameResolver . updateServiceUrl (differentClusterData .getServiceUrlTls ());
499+ serviceNameResolver = SERVICE_NAME_RESOLVER_CACHE . get (differentClusterData .getServiceUrlTls ());
483500 } else {
484- serviceNameResolver . updateServiceUrl (differentClusterData .getServiceUrl ());
501+ serviceNameResolver = SERVICE_NAME_RESOLVER_CACHE . get (differentClusterData .getServiceUrl ());
485502 }
486503 URL webUrl = new URL (serviceNameResolver .resolveHostUri ().toString ());
487504 return UriBuilder .fromUri (uri .getRequestUri ()).host (webUrl .getHost ()).port (webUrl .getPort ()).build ();
488- } catch (PulsarClientException .InvalidServiceURL exception ) {
489- throw new MalformedURLException (exception .getMessage ());
505+ } catch (Exception exception ) {
506+ if (exception .getCause () != null
507+ && exception .getCause () instanceof PulsarClientException .InvalidServiceURL ) {
508+ throw new MalformedURLException (exception .getMessage ());
509+ }
510+ throw exception ;
490511 }
491512 }
492513
0 commit comments