private void removeEndpoint(Endpoint endpoint, boolean crashed) {
    endpoint.close();
    boolean removedEndpoint = false;
    synchronized (this) {
      Map<ServerLocationAndMemberId, Endpoint> endpointMapTemp = new HashMap<>(endpointMap);
      endpoint = endpointMapTemp.remove(new ServerLocationAndMemberId(endpoint.getLocation(),
          endpoint.getMemberId().getUniqueId()));
      if (endpoint != null) {
        endpointMap = Collections.unmodifiableMap(endpointMapTemp);
        removedEndpoint = true;
      }
      poolStats.setServerCount(endpointMap.size());
    }

    if (removedEndpoint) {
      PoolImpl pool = (PoolImpl) PoolManager.find(poolName);
      ServerLocation location = endpoint.getLocation();
      if (pool != null && pool.getMultiuserAuthentication()) {
        int size = 0;
        ArrayList<ProxyCache> proxyCaches = pool.getProxyCacheList();
        synchronized (proxyCaches) {
          for (ProxyCache proxyCache : proxyCaches) {
            try {
              Long userId =
                  proxyCache.getUserAttributes().getServerToId().remove(location);
              if (userId != null) {
                ++size;
              }
            } catch (CacheClosedException cce) {
              // If this call is triggered by a Cache.close(), then this can be
              // expected.
            }
          }
          secureLogger.debug(
              "EndpointManagerImpl.removeEndpoint() Removed server {} from {} user's ProxyCache",
              location, size);
        }
        UserAttributes ua = UserAttributes.userAttributes.get();
        if (ua != null) {
          Long userId = ua.getServerToId().remove(location);
          if (userId != null) {
            secureLogger.debug(
                "EndpointManagerImpl.removeEndpoint() Removed server {} from thread local variable",
                location);
          }
        }
      } else if (pool != null && !pool.getMultiuserAuthentication()) {
        secureLogger.debug("set the userId of {} to -1", location);
        location.setUserId(-1);
      }

      if (crashed) {
        listener.endpointCrashed(endpoint);
      } else {
        listener.endpointNoLongerInUse(endpoint);
      }
    }
  }
Used by Endpoint only, when the reference count for this endpoint reaches 0
geode-core/src/main/java/org/apache/geode/cache/client/internal/EndpointManagerImpl.java