-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
71 lines (61 loc) · 1.56 KB
/
utils.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
package gbase
import (
"net"
"os"
"sync"
"time"
"github.com/cjey/gbase/utils"
)
// ResolveTCPAddr use 0.0.0.0 as default ip
// to completion the given raw ip
// "" => 0.0.0.0:defaultPort
// 80 => 0.0.0.0:80
// 1.1.1.1 => 1.1.1.1:defaultPort
func ResolveTCPAddr(ctx Context, raw string, defaultPort uint16) (*net.TCPAddr, error) {
return utils.ResolveTCPAddr(raw, nil, defaultPort)
}
// WriteMyPID write current process pid to /var/run/<appname>.pid
// If write failed, but the file content is equal to current process id,
// that's also ok. Any error will be ignored.
func WriteMyPID(ctx Context, appname string) {
utils.WritePID(ctx, "/var/run/"+appname+".pid", os.Getpid())
}
var _LiveProcessName = struct {
sync.Mutex
stop chan struct{}
}{}
// LiveProcessName change current process name every interval duration.
// You can use this for exposing your core runtime information to maintainer.
func LiveProcessName(ctx Context, interval time.Duration, liveName func(int) string) chan struct{} {
_LiveProcessName.Lock()
defer _LiveProcessName.Unlock()
if _LiveProcessName.stop != nil {
// stop last one
close(_LiveProcessName.stop)
}
var (
cnt int
last string
stop = make(chan struct{})
)
go func() {
for {
if liveName != nil {
var name = liveName(utils.MaxProcessNameLength())
if cnt == 0 || name != last {
utils.RenameMyProcess(ctx, name)
cnt++
last = name
}
}
select {
case <-stop:
return
case <-time.After(interval):
}
}
}()
// record this one
_LiveProcessName.stop = stop
return stop
}