github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/process/endpoint.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://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,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package process
    18  
    19  import (
    20  	"sync"
    21  	"time"
    22  )
    23  
    24  const (
    25  	endpointStopGracePeriod = time.Duration(5) * time.Minute
    26  )
    27  
    28  type StopControl struct {
    29  	stopTime time.Time
    30  	sync.RWMutex
    31  }
    32  
    33  func (sc *StopControl) Stop() {
    34  	if sc == nil {
    35  		return
    36  	}
    37  
    38  	sc.Lock()
    39  	defer sc.Unlock()
    40  	sc.stopTime = time.Now()
    41  }
    42  
    43  func (sc *StopControl) IsStopped() bool {
    44  	if sc == nil {
    45  		return true
    46  	}
    47  
    48  	sc.RLock()
    49  	defer sc.RUnlock()
    50  	return !sc.stopTime.IsZero()
    51  }
    52  
    53  func (sc *StopControl) StopGracePeriodExpired() bool {
    54  	if sc == nil {
    55  		return true
    56  	}
    57  
    58  	sc.RLock()
    59  	defer sc.RUnlock()
    60  	return !sc.stopTime.IsZero() && time.Since(sc.stopTime) > endpointStopGracePeriod
    61  }
    62  
    63  func NewStopControl(stopTime time.Time) *StopControl {
    64  	return &StopControl{
    65  		stopTime: stopTime,
    66  	}
    67  }