Skip to content

Commit a29aa00

Browse files
committed
doc: fix README
1 parent 00b3786 commit a29aa00

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

README.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,126 @@ Then import the package into your own code.
3939
- [repetition](./_examples/repetition/main.go)
4040
- [sample](./_examples/sample/main.go)
4141

42-
use global `timer` instance.
42+
#### monitor
43+
44+
[embedmd]:# (_examples/monitor/main.go go)
45+
```go
46+
package main
47+
48+
import (
49+
"log"
50+
"math"
51+
"math/rand/v2"
52+
"net/http"
53+
"sync/atomic"
54+
"time"
55+
56+
_ "net/http/pprof"
57+
58+
"github.com/thinkgos/timer"
59+
)
60+
61+
// almost 1,000,000 task
62+
func main() {
63+
go func() {
64+
sum := &atomic.Int64{}
65+
t := time.NewTicker(time.Second)
66+
for {
67+
<-t.C
68+
added := 0
69+
ranv := rand.IntN(10)
70+
max := int(rand.Uint32N(math.MaxUint16 << 2))
71+
for i := 100; i < max; i += 200 {
72+
added++
73+
ii := i + ranv
74+
75+
timer.Go(func() {
76+
sum.Add(1)
77+
delayms := int64(ii) * 20
78+
task := timer.NewTask(time.Duration(delayms) * time.Millisecond).WithJob(&job{
79+
sum: sum,
80+
expirationMs: time.Now().UnixMilli() + delayms,
81+
})
82+
timer.AddTask(task)
83+
84+
// for test race
85+
// if ii%0x03 == 0x00 {
86+
// timer.Go(func() {
87+
// task.Cancel()
88+
// })
89+
// }
90+
})
91+
}
92+
log.Printf("task: %v - %v added: %d", timer.TaskCounter(), sum.Load(), added)
93+
}
94+
}()
95+
96+
addr := ":9990"
97+
log.Printf("http stated '%v'\n", addr)
98+
log.Println(http.ListenAndServe(addr, nil))
99+
}
100+
101+
type job struct {
102+
sum *atomic.Int64
103+
expirationMs int64
104+
}
105+
106+
func (j *job) Run() {
107+
j.sum.Add(-1)
108+
now := time.Now().UnixMilli()
109+
if diff := now - j.expirationMs; diff > 1 {
110+
log.Printf("this task no equal, diff: %d %d %d\n", now, j.expirationMs, diff)
111+
}
112+
}
113+
```
114+
115+
#### repetition
116+
117+
[embedmd]:# (_examples/repetition/main.go go)
118+
```go
119+
package main
120+
121+
import (
122+
"fmt"
123+
"time"
124+
125+
"github.com/thinkgos/timer"
126+
)
127+
128+
// one or two second delay repetition example
129+
func main() {
130+
job := NewRepetitionJob()
131+
_ = timer.AddDerefTask(job)
132+
select {}
133+
}
134+
135+
type RepetitionJob struct {
136+
task *timer.Task
137+
i int
138+
}
139+
140+
var _ timer.TaskContainer = (*RepetitionJob)(nil)
141+
142+
func NewRepetitionJob() *RepetitionJob {
143+
j := &RepetitionJob{
144+
task: timer.NewTask(time.Second),
145+
i: 1,
146+
}
147+
j.task.WithJob(j)
148+
return j
149+
}
150+
151+
func (j *RepetitionJob) Run() {
152+
now := time.Now().String()
153+
j.i++
154+
_ = timer.AddTask(j.task.SetDelay(time.Second * time.Duration((j.i%2 + 1))))
155+
fmt.Printf("%s: repetition executed,\n", now)
156+
}
157+
158+
func (j *RepetitionJob) DerefTask() *timer.Task { return j.task }
159+
```
160+
161+
#### sample
43162

44163
[embedmd]:# (_examples/sample/main.go go)
45164
```go
@@ -67,6 +186,8 @@ func main() {
67186
}
68187
```
69188

189+
190+
70191
## How it works
71192

72193
- [How it works](./how_it_works.md)

0 commit comments

Comments
 (0)