github.com/mailgun/holster/v4@v4.20.0/ctxutil/context.go (about) 1 package ctxutil 2 3 import ( 4 "context" 5 "fmt" 6 "runtime" 7 "time" 8 9 "github.com/sirupsen/logrus" 10 ) 11 12 // WithTimeout calls context.WithTimeout and logs details of the 13 // deadline origin. 14 func WithDeadline(ctx context.Context, deadline time.Time) (context.Context, context.CancelFunc) { 15 _, fn, line, _ := runtime.Caller(1) 16 17 logrus.WithContext(ctx). 18 WithFields(logrus.Fields{ 19 "deadline": deadline.Format(time.RFC3339), 20 "source": fmt.Sprintf("%s:%d", fn, line), 21 }). 22 Debug("Set context deadline") 23 24 return context.WithDeadline(ctx, deadline) 25 } 26 27 // WithTimeout calls context.WithTimeout and logs details of the 28 // deadline origin. 29 func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { 30 deadline := time.Now().Add(timeout) 31 _, fn, line, _ := runtime.Caller(1) 32 33 logrus.WithContext(ctx). 34 WithFields(logrus.Fields{ 35 "deadline": deadline.Format(time.RFC3339), 36 "source": fmt.Sprintf("%s:%d", fn, line), 37 }). 38 Debug("Set context deadline") 39 40 return context.WithTimeout(ctx, timeout) 41 }