-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
175 lines (155 loc) · 3.1 KB
/
util.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package util
import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
)
var ApplicationLock sync.Mutex
func Must(err error) {
if err != nil {
panic(err)
}
}
var initQuitChan = make(chan int)
var quitChan = unsafe.Pointer(&initQuitChan)
func resetQuitChan() chan int {
cnew := make(chan int)
cold := atomic.SwapPointer(&quitChan, unsafe.Pointer(&cnew))
return *(*chan int)(cold)
}
func QuitChan() chan int {
c := atomic.LoadPointer(&quitChan)
return *(*chan int)(c)
}
var SignalChan = make(chan os.Signal)
var LeaveMessage = "Bye."
var Debug = false
var DebugOutput = os.Stdout
var Stdout io.Writer = os.Stdout
func Println(args ...interface{}) (n int, err error) {
return fmt.Fprintln(Stdout, args...)
}
func Printf(format string, args ...interface{}) (n int, err error) {
return fmt.Fprintf(Stdout, format, args...)
}
func Print(args ...interface{}) (n int, err error) {
return fmt.Fprint(Stdout, args...)
}
func DebugPrintln(args ...interface{}) {
if Debug || ForceDebug {
fmt.Fprintln(DebugOutput, args...)
}
}
func DebugPrintf(format string, args ...interface{}) {
if Debug || ForceDebug {
fmt.Fprintf(DebugOutput, format, args...)
}
}
func DebugPrint(args ...interface{}) {
if Debug || ForceDebug {
fmt.Fprint(DebugOutput, args...)
}
}
var QuitDelayDuration = 500 * time.Microsecond
func DelayAndQuit() {
Println("Delay for quit.")
time.Sleep(QuitDelayDuration)
Println("Quiting ...")
}
func WaitingQuit() {
signal.Notify(SignalChan, os.Interrupt, os.Kill)
select {
case <-SignalChan:
Quit()
case <-QuitChan():
}
}
func OnQuit(handlers ...func()) {
for k := range handlers {
handler := handlers[k]
go func() {
<-QuitChan()
handler()
}()
}
}
func OnQuitAndLogError(handlers ...func() error) {
for k := range handlers {
handler := handlers[k]
go func() {
<-QuitChan()
err := handler()
if err != nil {
LogError(err)
}
}()
}
}
func OnQuitAndIgnoreError(handlers ...func() error) {
for k := range handlers {
handler := handlers[k]
go func() {
<-QuitChan()
handler()
}()
}
}
func Bye() {
if LeaveMessage != "" {
Println(LeaveMessage)
}
}
func Quit() {
defer func() {
recover()
}()
c := resetQuitChan()
if c != nil {
close(c)
}
}
var LoggerMaxLength = 5
var LoggerIgnoredErrorsChecker = []func(error) bool{
func(err error) bool {
if err == http.ErrHandlerTimeout || err == http.ErrAbortHandler {
return true
}
if err == context.Canceled {
return true
}
oe, ok := err.(*net.OpError)
if ok {
if oe.Err == syscall.EPIPE || oe.Err == syscall.ECONNRESET {
return true
}
se, ok := oe.Err.(*os.SyscallError)
if ok && (se.Err == syscall.EPIPE || se.Err == syscall.ECONNRESET) {
return true
}
}
return false
},
}
var IsErrorIgnored = func(err error) bool {
for k := range LoggerIgnoredErrorsChecker {
if LoggerIgnoredErrorsChecker[k](err) {
return true
}
}
return false
}
var RegisterLoggerIgnoredErrorsChecker = func(f func(error) bool) {
LoggerIgnoredErrorsChecker = append(LoggerIgnoredErrorsChecker, f)
}
func init() {
resetQuitChan()
}