data.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package common
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. type Rule struct {
  7. Name, Json string
  8. }
  9. /**** Timer Mock *******/
  10. /** Ticker **/
  11. type Ticker interface {
  12. GetC() <-chan time.Time
  13. Stop()
  14. Trigger(ti int64)
  15. }
  16. type DefaultTicker struct{
  17. time.Ticker
  18. }
  19. func NewDefaultTicker(d int) *DefaultTicker{
  20. return &DefaultTicker{*(time.NewTicker(time.Duration(d) * time.Millisecond))}
  21. }
  22. func (t *DefaultTicker) GetC() <-chan time.Time{
  23. return t.C
  24. }
  25. func (t *DefaultTicker) Trigger(ti int64) {
  26. Log.Fatal("ticker trigger unsupported")
  27. }
  28. type MockTicker struct {
  29. c chan time.Time
  30. duration int64
  31. lastTick int64
  32. }
  33. func NewMockTicker(d int) *MockTicker{
  34. if d <= 0 {
  35. panic(errors.New("non-positive interval for MockTicker"))
  36. }
  37. c := make(chan time.Time, 1)
  38. t := &MockTicker{
  39. c: c,
  40. duration: int64(d),
  41. lastTick: GetMockNow(),
  42. }
  43. return t
  44. }
  45. func (t *MockTicker) SetDuration(d int){
  46. t.duration = int64(d)
  47. t.lastTick = GetMockNow()
  48. }
  49. func (t *MockTicker) GetC() <-chan time.Time{
  50. return t.c
  51. }
  52. func (t *MockTicker) Stop() {
  53. //do nothing
  54. }
  55. func (t *MockTicker) Trigger(ti int64) {
  56. t.lastTick = ti
  57. t.c <- time.Unix(ti/1000, ti%1000*1e6)
  58. }
  59. func (t *MockTicker) DoTick(c int64) {
  60. Log.Infof("do tick at %d, last tick %d", c, t.lastTick)
  61. if t.lastTick == 0 {
  62. t.lastTick = c
  63. }
  64. if c >= (t.lastTick + t.duration){
  65. Log.Info("trigger tick")
  66. t.Trigger(t.lastTick + t.duration)
  67. }
  68. }
  69. /** Timer **/
  70. type Timer interface {
  71. GetC() <-chan time.Time
  72. Stop() bool
  73. Reset(d time.Duration) bool
  74. Trigger(ti int64)
  75. }
  76. type DefaultTimer struct{
  77. time.Timer
  78. }
  79. func NewDefaultTimer(d int) *DefaultTimer{
  80. return &DefaultTimer{*(time.NewTimer(time.Duration(d) * time.Millisecond))}
  81. }
  82. func (t *DefaultTimer) GetC() <-chan time.Time{
  83. return t.C
  84. }
  85. func (t *DefaultTimer) Trigger(ti int64) {
  86. Log.Fatal("timer trigger unsupported")
  87. }
  88. type MockTimer struct {
  89. c chan time.Time
  90. duration int64
  91. createdAt int64
  92. }
  93. func NewMockTimer(d int) *MockTimer{
  94. if d <= 0 {
  95. panic(errors.New("non-positive interval for MockTimer"))
  96. }
  97. c := make(chan time.Time, 1)
  98. t := &MockTimer{
  99. c: c,
  100. duration: int64(d),
  101. createdAt: GetMockNow(),
  102. }
  103. return t
  104. }
  105. func (t *MockTimer) GetC() <-chan time.Time{
  106. return t.c
  107. }
  108. func (t *MockTimer) Stop() bool{
  109. t.createdAt = 0
  110. return true
  111. }
  112. func (t *MockTimer) SetDuration(d int){
  113. t.duration = int64(d)
  114. t.createdAt = GetMockNow()
  115. Log.Infoln("reset timer created at %v", t.createdAt)
  116. }
  117. func (t *MockTimer) Reset(d time.Duration) bool{
  118. Log.Infoln("reset timer")
  119. t.SetDuration(int(d.Nanoseconds()/1e6))
  120. return true
  121. }
  122. func (t *MockTimer) Trigger(ti int64) {
  123. t.c <- time.Unix(ti/1000, ti%1000*1e6)
  124. t.createdAt = 0
  125. }
  126. func (t *MockTimer) DoTick(c int64) {
  127. Log.Infof("do tick at %d, created at", c, t.createdAt)
  128. if t.createdAt > 0 && c >= (t.createdAt + t.duration){
  129. Log.Info("trigger timer")
  130. t.Trigger(t.createdAt + t.duration)
  131. }
  132. }