001/* 002 * Copyright 2022-2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.soklet; 018 019import org.jspecify.annotations.NonNull; 020 021import javax.annotation.concurrent.ThreadSafe; 022import java.time.Duration; 023import java.util.function.BooleanSupplier; 024import java.util.function.LongSupplier; 025 026import static java.util.Objects.requireNonNull; 027 028/** 029 * Framework-created advisory timing and cancelation information borrowed by a 030 * transport during attachment and startup. Soklet owns the context and its 031 * deadlines. A transport may read it from the attaching or startup thread but 032 * must not attempt to mutate or extend either boundary. 033 * 034 * @author <a href="https://www.revetkn.com">Mark Allen</a> 035 */ 036@ThreadSafe 037public final class StartupContext { 038 @NonNull 039 private final NanoClock clock; 040 private final long normalDeadlineNanos; 041 @NonNull 042 private final LongSupplier cancelationDeadlineNanos; 043 @NonNull 044 private final BooleanSupplier cancelationRequested; 045 046 StartupContext(@NonNull NanoClock clock, 047 long normalDeadlineNanos, 048 long cancelationDeadlineNanos, 049 @NonNull BooleanSupplier cancelationRequested) { 050 this(clock, normalDeadlineNanos, () -> cancelationDeadlineNanos, 051 cancelationRequested); 052 } 053 054 StartupContext(@NonNull NanoClock clock, 055 long normalDeadlineNanos, 056 @NonNull LongSupplier cancelationDeadlineNanos, 057 @NonNull BooleanSupplier cancelationRequested) { 058 this.clock = requireNonNull(clock); 059 this.normalDeadlineNanos = normalDeadlineNanos; 060 this.cancelationDeadlineNanos = requireNonNull(cancelationDeadlineNanos); 061 this.cancelationRequested = requireNonNull(cancelationRequested); 062 } 063 064 /** 065 * Acquires the time remaining before the active startup boundary. 066 * 067 * @return the remaining time, clamped to zero once the boundary is reached 068 */ 069 @NonNull 070 public Duration getRemainingTime() { 071 if (isCancelationRequested()) 072 return LifecycleDeadlines.remaining( 073 this.cancelationDeadlineNanos.getAsLong(), this.clock.nanoTime()); 074 return LifecycleDeadlines.remaining( 075 this.normalDeadlineNanos, this.clock.nanoTime()); 076 } 077 078 /** 079 * Is startup cancelation currently requested? 080 * 081 * @return {@code true} if cancelation is requested, {@code false} otherwise 082 */ 083 @NonNull 084 public Boolean isCancelationRequested() { 085 return this.cancelationRequested.getAsBoolean(); 086 } 087 088 long activeDeadlineNanos() { 089 if (isCancelationRequested()) 090 return this.cancelationDeadlineNanos.getAsLong(); 091 return this.normalDeadlineNanos; 092 } 093}