github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/timer.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package util
     7  
     8  import (
     9  	"time"
    10  )
    11  
    12  // StopTimer is a utility function to safely stop a time.Timer and clean its channel
    13  func StopTimer(t *time.Timer) bool {
    14  	stopped := t.Stop()
    15  	if !stopped {
    16  		select {
    17  		case <-t.C:
    18  		default:
    19  		}
    20  	}
    21  	return stopped
    22  }