|
| 1 | +/* |
| 2 | + * Copyright 2024 LY Corporation |
| 3 | + * |
| 4 | + * LY Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.linecorp.armeria.server.healthcheck; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkArgument; |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | + |
| 23 | +/** |
| 24 | + * A {@link HealthChecker} that reports as unhealthy |
| 25 | + * when the target free disk space exceeds a file disk usable space. |
| 26 | + * For example: |
| 27 | + * <pre>{@code |
| 28 | + * final DiskMemoryHealthChecker diskMemoryHealthChecker = HealthChecker.ofDisk(100, new File("/tmp")); |
| 29 | + * |
| 30 | + * // Returns false if a file disk usable memory space is less than 100 bytes, |
| 31 | + * // or true if a file disk usable memory space is greater than or equal to 100 bytes. |
| 32 | + * final boolean healthy = diskMemoryHealthChecker.isHealthy(); |
| 33 | + * }</pre> |
| 34 | + */ |
| 35 | +// Forked from <a href="https://github.com/micrometer-metrics/micrometer/blob/8339d57bef8689beb8d7a18b429a166f6595f2af/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/system/DiskSpaceMetrics.java">DiskSpaceMetrics.java</a> in the micrometer core. |
| 36 | +final class DiskMemoryHealthChecker implements HealthChecker { |
| 37 | + |
| 38 | + private final double targetFreeDiskSpace; |
| 39 | + |
| 40 | + private final File path; |
| 41 | + |
| 42 | + DiskMemoryHealthChecker(double targetFreeDiskSpace, File path) { |
| 43 | + checkArgument(targetFreeDiskSpace >= 0, "freeDiskSpace: %s (expected: >= 0)", targetFreeDiskSpace); |
| 44 | + requireNonNull(path); |
| 45 | + this.targetFreeDiskSpace = targetFreeDiskSpace; |
| 46 | + this.path = path; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns true if the file usable space is greater or equal than the target space. |
| 51 | + * @return boolean |
| 52 | + */ |
| 53 | + @Override |
| 54 | + public boolean isHealthy() { |
| 55 | + return path.getUsableSpace() >= targetFreeDiskSpace; |
| 56 | + } |
| 57 | +} |
0 commit comments