forked from lonng/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
104 lines (88 loc) · 2.49 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) nano Authors. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package scheduler
import (
"runtime/debug"
"sync/atomic"
"time"
"github.com/aura-studio/nano/env"
"github.com/aura-studio/nano/log"
"github.com/aura-studio/nano/session"
)
const (
messageQueueBacklog = 1 << 10
sessionCloseBacklog = 1 << 8
)
// Task is the unit to be scheduled
type Task func()
// SchedFunc is the Func type of schedule
type SchedFunc func(session *session.Session, v interface{}, task Task)
var (
chDie = make(chan struct{})
chExit = make(chan struct{})
chTasks = make(chan Task, 1<<8)
started int32
closed int32
)
func try(f func()) {
defer func() {
if err := recover(); err != nil {
log.Errorf("Handle message panic: %+v\n%s", err, debug.Stack())
}
}()
f()
}
// Digest pops tasks from task channel, and handle them.
func Digest() {
if atomic.AddInt32(&started, 1) != 1 {
return
}
ticker := time.NewTicker(env.TimerPrecision)
defer func() {
ticker.Stop()
close(chExit)
}()
for {
select {
case <-ticker.C:
timerManager.Cron()
case f := <-chTasks:
try(f)
case <-chDie:
return
}
}
}
// Close closes scheduler.
func Close() {
if atomic.AddInt32(&closed, 1) != 1 {
return
}
close(chDie)
<-chExit
}
// Schedule is to fill the default func for Service.Schedule
func Schedule(_ *session.Session, _ interface{}, task Task) {
PushTask(task)
}
// PushTask pushes task in task channel
func PushTask(task Task) {
chTasks <- task
}