github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/protocol/timer.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package protocol
     4  
     5  // Timing observe Timers and
     6  type Timing interface {
     7  	AddTimer(t Timer)
     8  }
     9  
    10  // Timer is the interface that must implement by any timer.
    11  type Timer interface {
    12  	Start(d Duration) (err Error)
    13  
    14  	Reset(d Duration) (alreadyActivated bool)
    15  
    16  	// Client must call Stop(), otherwise **"leaks"** occur, specially in Tick()
    17  	Stop() (alreadyStopped bool)
    18  
    19  	Stringer
    20  }
    21  
    22  // Timer_Async is the interface that must implement by any timer.
    23  type Timer_Async interface {
    24  	// Init initialize the Timer with given callback function.
    25  	// - **NOTE**: each time calling callback() in the timer goroutine, so callback must be
    26  	// a well-behaved function and not block. If callback need blocking operation it must do its logic in new thread(goroutine).
    27  	// - Be aware that given function must not be closure and must not block the caller.
    28  	Init(t Timing, callback TimerListener)
    29  
    30  	Timer
    31  }
    32  
    33  type Timer_Sync interface {
    34  	// Init initialize the Timer and make ready the timer channel and send signal on it
    35  	// client use Signal() to block until timeout occur.
    36  	Init(t Timing)
    37  
    38  	// Suggest to implement not force here to get the timer channel to receive timer-out signal
    39  	Signal() <-chan struct{}
    40  
    41  	Timer
    42  }
    43  
    44  // Ticker is the interface that must implement by any ticker.
    45  // Both Timer_Async and Timer_Sync can also be a ticker, just
    46  // Start method of timer is same as Tick(d, 0)
    47  // Reset just change the interval not first tick duration.
    48  type Ticker interface {
    49  	Tick(first, interval Duration) (err Error)
    50  }
    51  
    52  // TimerListener or TimerCallBack
    53  type TimerListener interface {
    54  	// Non-Blocking, means It must not block the caller in any ways.
    55  	TimerHandler()
    56  }