Pārlūkot izejas kodu

修复darwin获取机器码出问题

sugar 3 gadi atpakaļ
vecāks
revīzija
418ed96554
3 mainītis faili ar 48 papildinājumiem un 10 dzēšanām
  1. 3 0
      helper/machineid/id_darwin.go
  2. 31 0
      reporter_test.go
  3. 14 10
      service.go

+ 3 - 0
helper/machineid/id_darwin.go

@@ -1,9 +1,12 @@
+//go:build darwin
 // +build darwin
 
 package machineid
 
 import (
+	"fmt"
 	"os/exec"
+	"strings"
 )
 
 func getMachineID() (s string, err error) {

+ 31 - 0
reporter_test.go

@@ -0,0 +1,31 @@
+package micro
+
+import (
+	"reflect"
+	"testing"
+)
+
+func TestReporter_getMachineCode(t *testing.T) {
+	type fields struct {
+		ServerAddr  string
+		machineCode []byte
+	}
+	tests := []struct {
+		name   string
+		fields fields
+		want   []byte
+	}{
+		// TODO: Add test cases.
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			r := &Reporter{
+				ServerAddr:  tt.fields.ServerAddr,
+				machineCode: tt.fields.machineCode,
+			}
+			if got := r.getMachineCode(); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("Reporter.getMachineCode() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}

+ 14 - 10
service.go

@@ -4,12 +4,6 @@ import (
 	"context"
 	"crypto/md5"
 	"encoding/hex"
-	"git.nspix.com/golang/micro/broker"
-	"git.nspix.com/golang/micro/gateway/cli"
-	"git.nspix.com/golang/micro/helper/machineid"
-	"git.nspix.com/golang/micro/helper/rbtree"
-	"git.nspix.com/golang/micro/stats/prometheusbackend"
-	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"math"
 	"math/rand"
 	"net"
@@ -23,6 +17,13 @@ import (
 	"syscall"
 	"time"
 
+	"git.nspix.com/golang/micro/broker"
+	"git.nspix.com/golang/micro/gateway/cli"
+	"git.nspix.com/golang/micro/helper/machineid"
+	"git.nspix.com/golang/micro/helper/rbtree"
+	"git.nspix.com/golang/micro/stats/prometheusbackend"
+	"github.com/prometheus/client_golang/prometheus/promhttp"
+
 	"git.nspix.com/golang/micro/gateway"
 	"git.nspix.com/golang/micro/gateway/http"
 	"git.nspix.com/golang/micro/gateway/rpc"
@@ -54,6 +55,7 @@ type Service struct {
 	timer       *time.Timer
 	tickTree    *rbtree.Tree
 	environment string
+	exitFlag    int32
 }
 
 func (svr *Service) async(f func()) {
@@ -261,7 +263,7 @@ func (svr *Service) startHTTPServe() (err error) {
 	l := gateway.NewListener(svr.listener.Addr())
 	if err = svr.gateway.Attaches([][]byte{[]byte("GET"), []byte("POST"), []byte("PUT"), []byte("DELETE"), []byte("OPTIONS")}, l); err == nil {
 		svr.async(func() {
-			if err = svr.httpSvr.Serve(l); err != nil {
+			if err = svr.httpSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
 				log.Warnf("http serve error: %s", err.Error())
 			}
 			log.Infof("http server stopped")
@@ -301,7 +303,7 @@ func (svr *Service) startRPCServe() (err error) {
 	l := gateway.NewListener(svr.listener.Addr())
 	if err = svr.gateway.Attach([]byte("RPC"), l); err == nil {
 		svr.async(func() {
-			if err = svr.rpcSvr.Serve(l); err != nil {
+			if err = svr.rpcSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
 				log.Warnf("rpc serve start error: %s", err.Error())
 			}
 			log.Infof("rpc server stopped")
@@ -317,7 +319,7 @@ func (svr *Service) startCliServe() (err error) {
 	l := gateway.NewListener(svr.listener.Addr())
 	if err = svr.gateway.Attach([]byte("CLI"), l); err == nil {
 		svr.async(func() {
-			if err = svr.cliSvr.Serve(l); err != nil {
+			if err = svr.cliSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
 				log.Warnf("cli serve start error: %s", err.Error())
 			}
 			log.Infof("cli server stopped")
@@ -391,6 +393,9 @@ func (svr *Service) prepare() (err error) {
 }
 
 func (svr *Service) destroy() (err error) {
+	if !atomic.CompareAndSwapInt32(&svr.exitFlag, 0, 1) {
+		return
+	}
 	log.Infof("service stopping")
 	svr.cancelFunc()
 	if !svr.opts.DisableRegister {
@@ -412,7 +417,6 @@ func (svr *Service) destroy() (err error) {
 	}
 	svr.timer.Stop()
 	if svr.tickTree.Size() > 0 {
-		log.Warnf("num of %d defer tick dropped", svr.tickTree.Size())
 		node := svr.tickTree.Iterator()
 		for node != nil {
 			tick := node.Value.(*tickPtr)