From 0859ff4598dcd5dccbe856f2ab962c79c49aa334 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Fri, 9 Oct 2020 12:03:53 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=9B=B4=E6=96=B0=E6=97=B6?= =?UTF-8?q?=E7=AB=8B=E5=8D=B3=E5=90=91=E9=9B=86=E7=BE=A4=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=8F=91=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/build.sh | 1 + internal/nodes/api_stream.go | 13 +++++++++++++ internal/nodes/node.go | 22 ++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/build/build.sh b/build/build.sh index ad694c5..de8bc18 100755 --- a/build/build.sh +++ b/build/build.sh @@ -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 diff --git a/internal/nodes/api_stream.go b/internal/nodes/api_stream.go index 4738a06..3b60ef4 100644 --- a/internal/nodes/api_stream.go +++ b/internal/nodes/api_stream.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+"'") diff --git a/internal/nodes/node.go b/internal/nodes/node.go index d612cfc..5ac8c3d 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -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 + } } } }()