Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polaris.ConfigAPI 不能并行监听多个配置文件 #183

Closed
nxsre opened this issue Oct 18, 2023 · 0 comments · Fixed by #212
Closed

polaris.ConfigAPI 不能并行监听多个配置文件 #183

nxsre opened this issue Oct 18, 2023 · 0 comments · Fixed by #212
Labels
bug Something isn't working

Comments

@nxsre
Copy link

nxsre commented Oct 18, 2023

Describe the bug
使用 polaris.ConfigAPI 监听配置文件存在不能串行等待的问题,每个配置需要等待1s导致程序启动超时
To Reproduce


package main

import (
	"fmt"
	"github.com/polarismesh/polaris-go/pkg/config"
	"log"
	"time"

	"github.com/polarismesh/polaris-go"
	"github.com/polarismesh/polaris-go/pkg/model"
)

func main() {
	cfg := config.ConfigurationImpl{}
	cfg.Init()
	cfg.SetDefault()
	cfg.Global.ServerConnector.SetAddresses([]string{"10.211.55.16:8091"})
	cfg.Config.ConfigConnectorConfig.SetAddresses([]string{"10.211.55.16:8093"})
	configAPI, err := polaris.NewConfigAPIByConfig(&cfg)
	if err != nil {
		fmt.Println("fail to start example.", err)
		return
	}
	for _, f := range []string{"blacklist-44444.json", "blacklist-51934.json", "blacklist-64532.json"} {
		go watchfile(f, configAPI)
	}
	time.Sleep(10 * time.Second)
}

func watchfile(fileName string, configAPI polaris.ConfigAPI) {
	t1 := time.Now()

	//configAPI, err := polaris.NewConfigAPI()

	t2 := time.Now()
	// 获取远程的配置文件
	namespace := "default"
	fileGroup := "tcpwall"
	//fileName := "example.yaml"

	configFile, err := configAPI.GetConfigFile(namespace, fileGroup, fileName)
	if err != nil {
		log.Println("fail to get config.", err)
		return
	}
	t3 := time.Now()
	// 打印配置文件内容
	log.Println(configFile.GetContent())

	// 方式一:添加监听器
	configFile.AddChangeListener(changeListener)

	// 方式二:添加监听器
	//changeChan := configFile.AddChangeListenerWithChannel()
	t4 := time.Now()
	fmt.Println(t4.Sub(t3), t3.Sub(t2), t2.Sub(t1))
	//for {
	//	select {
	//	case event := <-changeChan:
	//		log.Printf("received change event by channel. %+v", event)
	//	}
	//}
}

func changeListener(event model.ConfigFileChangeEvent) {
	log.Printf("received change event. %+v", event)
}

Expected behavior
预期为 watchfile 可以同时监听多个配置文件

Environment
Linux / macOS 均可复现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant