| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 | package signalimport (	"context"	"time")type ActivityTimer struct {	updated chan bool	timeout time.Duration	ctx     context.Context	cancel  context.CancelFunc}func (t *ActivityTimer) UpdateActivity() {	select {	case t.updated <- true:	default:	}}func (t *ActivityTimer) run() {	for {		select {		case <-time.After(t.timeout):			t.cancel()			return		case <-t.ctx.Done():			return		default:			select {			case <-time.After(t.timeout):				t.cancel()				return			case <-t.ctx.Done():				return			case <-t.updated:			}		}	}}func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {	timer := &ActivityTimer{		ctx:     ctx,		cancel:  cancel,		timeout: timeout,		updated: make(chan bool, 1),	}	go timer.run()	return timer}
 |