mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	配置更新时立即向集群节点发消息
This commit is contained in:
		@@ -32,6 +32,7 @@ function build() {
 | 
			
		||||
 | 
			
		||||
	cp configs/api.template.yaml $DIST/configs
 | 
			
		||||
	cp -R www $DIST/
 | 
			
		||||
	cp -R pages $DIST/
 | 
			
		||||
 | 
			
		||||
	echo "building ..."
 | 
			
		||||
	env GOOS=${1} GOARCH=${2} go build -o $DIST/bin/${NAME} -ldflags="-s -w" ../cmd/edge-node/main.go
 | 
			
		||||
 
 | 
			
		||||
@@ -70,6 +70,8 @@ func (this *APIStream) loop() error {
 | 
			
		||||
			err = this.handlePurgeCache(message)
 | 
			
		||||
		case messageconfigs.MessageCodePreheatCache: // 预热缓存
 | 
			
		||||
			err = this.handlePreheatCache(message)
 | 
			
		||||
		case messageconfigs.MessageCodeConfigChanged: // 配置变化
 | 
			
		||||
			err = this.handleConfigChanged(message)
 | 
			
		||||
		default:
 | 
			
		||||
			err = this.handleUnknownMessage(message)
 | 
			
		||||
		}
 | 
			
		||||
@@ -400,6 +402,17 @@ func (this *APIStream) handlePreheatCache(message *pb.NodeStreamMessage) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 处理配置变化
 | 
			
		||||
func (this *APIStream) handleConfigChanged(message *pb.NodeStreamMessage) error {
 | 
			
		||||
	select {
 | 
			
		||||
	case changeNotify <- true:
 | 
			
		||||
	default:
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	this.replyOk(message.RequestId, "ok")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 处理未知消息
 | 
			
		||||
func (this *APIStream) handleUnknownMessage(message *pb.NodeStreamMessage) error {
 | 
			
		||||
	this.replyFail(message.RequestId, "unknown message code '"+message.Code+"'")
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,7 @@ import (
 | 
			
		||||
 | 
			
		||||
var lastVersion = int64(-1)
 | 
			
		||||
var sharedNodeConfig *nodeconfigs.NodeConfig
 | 
			
		||||
var changeNotify = make(chan bool, 8)
 | 
			
		||||
 | 
			
		||||
// 节点
 | 
			
		||||
type Node struct {
 | 
			
		||||
@@ -117,13 +118,22 @@ func (this *Node) syncConfig(isFirstTime bool) error {
 | 
			
		||||
// 启动同步计时器
 | 
			
		||||
func (this *Node) startSyncTimer() {
 | 
			
		||||
	// TODO 这个时间间隔可以自行设置
 | 
			
		||||
	ticker := time.NewTicker(30 * time.Second)
 | 
			
		||||
	ticker := time.NewTicker(60 * time.Second)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for range ticker.C {
 | 
			
		||||
			err := this.syncConfig(false)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				logs.Error("NODE", "sync config error: "+err.Error())
 | 
			
		||||
				continue
 | 
			
		||||
		for {
 | 
			
		||||
			select {
 | 
			
		||||
			case <-ticker.C:
 | 
			
		||||
				err := this.syncConfig(false)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					logs.Error("NODE", "sync config error: "+err.Error())
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
			case <-changeNotify:
 | 
			
		||||
				err := this.syncConfig(false)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					logs.Error("NODE", "sync config error: "+err.Error())
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user