go.temporal.io/server@v1.23.0/common/timer/validation.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package timer
    26  
    27  import (
    28  	"errors"
    29  	"time"
    30  
    31  	"go.temporal.io/server/common/primitives/timestamp"
    32  	"google.golang.org/protobuf/types/known/durationpb"
    33  )
    34  
    35  const (
    36  	// MaxAllowedTimer is the maximum allowed timer duration in the system
    37  	// exported for integration tests
    38  	MaxAllowedTimer = 100 * 365 * 24 * time.Hour
    39  )
    40  
    41  var (
    42  	errNegativeDuration = errors.New("negative timer duration")
    43  	// Pre-extracted for ease of use later
    44  	maxSeconds = MaxAllowedTimer.Nanoseconds() / 1e9
    45  	maxNanos   = int32(MaxAllowedTimer.Nanoseconds() - maxSeconds*1e9)
    46  )
    47  
    48  func ValidateAndCapTimer(delay *durationpb.Duration) error {
    49  	duration := timestamp.DurationValue(delay)
    50  	if duration < 0 {
    51  		return errNegativeDuration
    52  	}
    53  
    54  	// unix nano (max int64) is 2262-04-11T23:47:16.854775807Z
    55  	// allowing 100 years timer is safe until 2162
    56  	//
    57  	// NOTE: we choose to cap the timer instead of returning error so that
    58  	// existing workflows implementation using higher than allowed timer
    59  	// can continue to run.
    60  	if duration > MaxAllowedTimer {
    61  		delay.Nanos = maxNanos
    62  		delay.Seconds = maxSeconds
    63  	}
    64  	return nil
    65  }