Browse Source

添加一些工具类

lxg 3 years ago
parent
commit
24ba46aaf1
5 changed files with 129 additions and 10 deletions
  1. 11 0
      helper/random/int.go
  2. 8 0
      helper/random/ip.go
  3. 28 0
      helper/random/string.go
  4. 48 0
      helper/utils/directory.go
  5. 34 10
      service.go

+ 11 - 0
helper/random/int.go

@@ -0,0 +1,11 @@
+package random
+
+import (
+	"math/rand"
+	"time"
+)
+
+func Int(min, max int64) int64 {
+	rand.Seed(time.Now().UnixNano())
+	return min + rand.Int63n(max-min)
+}

File diff suppressed because it is too large
+ 8 - 0
helper/random/ip.go


+ 28 - 0
helper/random/string.go

@@ -0,0 +1,28 @@
+package random
+
+import (
+	"math/rand"
+	"strings"
+)
+
+const (
+	Uppercase    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+	Lowercase    = "abcdefghijklmnopqrstuvwxyz"
+	Alphabetic   = Uppercase + Lowercase
+	Numeric      = "0123456789"
+	Alphanumeric = Alphabetic + Numeric
+	Symbols      = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
+	Hex          = Numeric + "abcdef"
+)
+
+func String(length uint8, charsets ...string) string {
+	charset := strings.Join(charsets, "")
+	if charset == "" {
+		charset = Alphanumeric
+	}
+	b := make([]byte, length)
+	for i := range b {
+		b[i] = charset[rand.Int63()%int64(len(charset))]
+	}
+	return string(b)
+}

+ 48 - 0
helper/utils/directory.go

@@ -0,0 +1,48 @@
+package utils
+
+import (
+	"os"
+	"path/filepath"
+	"runtime"
+)
+
+//HomeDir return user home directory
+func HomeDir() string {
+	if runtime.GOOS == "windows" {
+		return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
+	}
+	if h := os.Getenv("HOME"); h != "" {
+		return h
+	}
+	return "/"
+}
+
+//HiddenFilePrefix get hidden file prefix
+func HiddenFilePrefix() string {
+	switch runtime.GOOS {
+	case "windows":
+		return "~"
+	default:
+		return "."
+	}
+}
+
+//CacheDir return user cache directory
+func CacheDir() string {
+	switch runtime.GOOS {
+	case "darwin":
+		return filepath.Join(HomeDir(), "Library", "Caches")
+	case "windows":
+		for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
+			if v := os.Getenv(ev); v != "" {
+				return v
+			}
+		}
+		// Worst case:
+		return HomeDir()
+	}
+	if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
+		return xdg
+	}
+	return filepath.Join(HomeDir(), ".cache")
+}

+ 34 - 10
service.go

@@ -4,6 +4,9 @@ import (
 	"context"
 	"crypto/md5"
 	"encoding/hex"
+	"git.nspix.com/golang/micro/helper/random"
+	"git.nspix.com/golang/micro/helper/utils"
+	"io/ioutil"
 	"math"
 	"math/rand"
 	"net"
@@ -11,6 +14,7 @@ import (
 	"net/http/pprof"
 	"os"
 	"os/signal"
+	"path"
 	"strings"
 	"sync"
 	"sync/atomic"
@@ -208,15 +212,36 @@ func (svr *Service) Environment() string {
 	return svr.environment
 }
 
+func (svr *Service) getMachineID() (machineID string) {
+	var (
+		buf []byte
+		err error
+	)
+	cacheFile := path.Join(utils.CacheDir(), utils.HiddenFilePrefix()+svr.opts.ShortName())
+	if utils.FileExists(cacheFile) {
+		if buf, err = ioutil.ReadFile(cacheFile); err == nil {
+			machineID = string(buf)
+			return
+		}
+	}
+	if machineID == "" {
+		if machineID, err = machineid.Code(); err != nil {
+			machineID = random.String(64)
+		}
+	}
+	_ = os.MkdirAll(path.Dir(cacheFile), 0644)
+	_ = ioutil.WriteFile(cacheFile, []byte(machineID), 0755)
+	return machineID
+}
+
 func (svr *Service) instance() *registry.ServiceNode {
 	var (
-		err         error
-		id          string
-		dockerID    string
-		tcpAddr     *net.TCPAddr
-		ipLocal     string
-		machineCode string
-		node        *registry.ServiceNode
+		err      error
+		id       string
+		dockerID string
+		tcpAddr  *net.TCPAddr
+		ipLocal  string
+		node     *registry.ServiceNode
 	)
 	if id, err = docker.SelfContainerID(); err != nil {
 		//生成唯一ID
@@ -249,9 +274,7 @@ func (svr *Service) instance() *registry.ServiceNode {
 		node.Port = svr.opts.Port
 	}
 	//上报机器码
-	if machineCode, err = machineid.Code(); err == nil {
-		node.Metadata["machine-code"] = machineCode
-	}
+	node.Metadata["machine-code"] = svr.getMachineID()
 	node.Metadata["docker-id"] = dockerID
 	if svr.opts.EnableHttp {
 		node.Metadata["enable-http"] = "true"
@@ -403,6 +426,7 @@ func (svr *Service) prepare() (err error) {
 	return
 }
 
+//destroy stop and destroy service
 func (svr *Service) destroy() (err error) {
 	if !atomic.CompareAndSwapInt32(&svr.exitFlag, 0, 1) {
 		return

Some files were not shown because too many files changed in this diff