All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.palantir.conjure.java.okhttp.LeakDetector Maven / Gradle / Ivy

The newest version!
/*
 * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.conjure.java.okhttp;

import com.google.common.annotations.VisibleForTesting;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeRuntimeException;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

final class LeakDetector {
    private static final SafeLogger log = SafeLoggerFactory.get(LeakDetector.class);

    private final Class resourceType;
    private final Consumer> subscriber;
    private final List> references = new ArrayList<>();

    LeakDetector(Class resourceType) {
        this(resourceType, _unused -> {});
    }

    @VisibleForTesting
    LeakDetector(Class resourceType, Consumer> subscriber) {
        this.resourceType = resourceType;
        this.subscriber = subscriber;
    }

    static Optional maybeCreateStackTrace() {
        if (log.isTraceEnabled()) {
            return Optional.of(new SafeRuntimeException("Runtime exception for stack trace"));
        }
        return Optional.empty();
    }

    synchronized void register(T objectToMonitor, Optional stackTrace) {
        references.add(new LeakDetectingReference<>(objectToMonitor, stackTrace));
        pruneAndLog();
    }

    synchronized void unregister(T objectToNoLongerMonitor) {
        for (int i = 0; i < references.size(); i++) {
            if (references.get(i).get() == objectToNoLongerMonitor) {
                int last = references.size() - 1;
                Collections.swap(references, i, last);
                references.remove(last);
                return;
            }
        }
    }

    private synchronized void pruneAndLog() {
        Iterator> iterator = references.iterator();
        while (iterator.hasNext()) {
            LeakDetectingReference reference = iterator.next();
            if (reference.get() == null) {
                subscriber.accept(reference.stackTrace);
                logLeak(reference.stackTrace);
                iterator.remove();
            }
        }
    }

    private void logLeak(Optional stackTrace) {
        if (stackTrace.isPresent()) {
            log.warn(
                    "Resource leak detected - did you forget to close a resource properly? "
                            + "This will likely hurt performance. Stack trace is of the call where "
                            + "the acquire happened.",
                    SafeArg.of("resourceType", resourceType.getName()),
                    stackTrace.get());
        } else {
            log.warn(
                    "Leak detected in Conjure call - did you forget to close a resource properly? "
                            + "This will likely hurt performance. To get a "
                            + "stack trace for the call where the acquire happened, set the log "
                            + "level for this origin to TRACE.",
                    SafeArg.of("resourceType", resourceType.getName()));
        }
    }

    private static final class LeakDetectingReference extends WeakReference {
        private final Optional stackTrace;

        LeakDetectingReference(T referent, Optional stackTrace) {
            super(referent);
            this.stackTrace = stackTrace;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy