github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/kcppp/time.go (about)

     1  package kcppp
     2  
     3  import "time"
     4  
     5  func currentMS() uint32 {
     6  	return uint32(time.Now().UnixNano() / 1000000)
     7  }
     8  
     9  type rtAction struct {
    10  	dline  time.Time
    11  	action func()
    12  }
    13  
    14  type resettableTimer struct {
    15  	actions chan rtAction
    16  }
    17  
    18  func newTimer() *resettableTimer {
    19  	rt := &resettableTimer{
    20  		actions: make(chan rtAction, 32),
    21  	}
    22  	go func() {
    23  		var dline time.Time
    24  		var action func()
    25  		for {
    26  			select {
    27  			case <-time.After(dline.Sub(time.Now())):
    28  				if action != nil {
    29  					action()
    30  				}
    31  				dline = time.Now().Add(time.Hour * 200000)
    32  			case act, ok := <-rt.actions:
    33  				if !ok {
    34  					return
    35  				}
    36  				dline = act.dline
    37  				action = act.action
    38  			}
    39  		}
    40  	}()
    41  	return rt
    42  }