github.com/network-quality/goresponsiveness@v0.0.0-20240129151524-343954285090/timeoutat/timeoutat.go (about) 1 /* 2 * This file is part of Go Responsiveness. 3 * 4 * Go Responsiveness is free software: you can redistribute it and/or modify it under 5 * the terms of the GNU General Public License as published by the Free Software Foundation, 6 * either version 2 of the License, or (at your option) any later version. 7 * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY 8 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 9 * PARTICULAR PURPOSE. See the GNU General Public License for more details. 10 * 11 * You should have received a copy of the GNU General Public License along 12 * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>. 13 */ 14 15 package timeoutat 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "github.com/network-quality/goresponsiveness/debug" 23 ) 24 25 func TimeoutAt( 26 ctx context.Context, 27 when time.Time, 28 debugLevel debug.DebugLevel, 29 ) (response chan interface{}) { 30 response = make(chan interface{}) 31 go func(ctx context.Context) { 32 if debug.IsDebug(debugLevel) { 33 fmt.Printf("Timeout expected to end at %v\n", when) 34 } 35 select { 36 case <-time.After(when.Sub(time.Now())): 37 case <-ctx.Done(): 38 } 39 response <- struct{}{} 40 if debug.IsDebug(debugLevel) { 41 fmt.Printf("Timeout ended at %v\n", time.Now()) 42 } 43 }(ctx) 44 return 45 }