File size: 666 Bytes
857a91b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package security

import (
	"fmt"
	"time"
)

// ClampTTL clamps a requested TTL (in seconds) to the [1, max] range. A
// non-positive request falls back to max.
func ClampTTL(requested, max int) time.Duration {
	if max <= 0 {
		max = 600
	}
	if requested <= 0 || requested > max {
		requested = max
	}
	return time.Duration(requested) * time.Second
}

// CheckTTL validates a requested TTL against the configured maximum.
func CheckTTL(requested, max int) error {
	if requested < 0 {
		return fmt.Errorf("ttl_seconds must not be negative")
	}
	if max > 0 && requested > max {
		return fmt.Errorf("ttl_seconds %d exceeds maximum %d", requested, max)
	}
	return nil
}