背景
很多时候我们希望程序可以单例运行
怎么去做
- 监听端口形式的http或tcp
此方式需要占用系统端口,有“洁癖”的小伙伴可能不太能接受
- 用unix形式即sock方式监听
实现
package single
import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
)
var socketFile string
// Start start single
func Start(socketFile string) (started bool) {
c, _ := net.Dial("unix", socketFile)
if c != nil {
return true
}
go func() {
os.Remove(socketFile)
l, err := net.Listen("unix", socketFile)
if err != nil {
panic(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
} else {
conn.Close()
}
}
}()
return false
}
// StartAuto auto start
func StartAuto() bool {
h := md5.New()
execName := os.Args[0]
dir := filepath.Dir(execName)
dirAbs, err := filepath.Abs(dir)
if err == nil && dirAbs != dir {
execName = filepath.Join(dirAbs, execName)
}
h.Write([]byte(execName))
filename := hex.EncodeToString(h.Sum(nil))
// 这里没有使用临时目录,考虑到临时目录可能随时会被清理
p, e := home()
if e != nil {
p = "./"
}
filepath := filepath.Join(p, "."+filepath.Base(execName)+"-"+filename+".sock")
return Start(filepath)
}
func home() (string, error) {
user, err := user.Current()
if nil == err {
return user.HomeDir, nil
}
// cross compile support
if "windows" == runtime.GOOS {
return homeWindows()
}
// Unix-like system, so just assume Unix
return homeUnix()
}
func homeUnix() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
// If that fails, try the shell
var stdout bytes.Buffer
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
result := strings.TrimSpace(stdout.String())
if result == "" {
return "", errors.New("blank output when reading home directory")
}
return result, nil
}
func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home := drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}
return home, nil
}
用法如下
package main
import (
"fmt"
"goutils/single"
)
func main() {
// 自动使用sock文件
// started := single.StartAuto()
// 指定sock文件
started := single.Start("./1.sock")
if started {
fmt.Println("已经启动了一个实例")
} else {
fmt.Println("第一次运行")
select {}
}
}