spf13 / viper

Go configuration with fangs
MIT License
26.77k stars 2.01k forks source link

update configure from remote configure center cause panic #1637

Open shifengbin opened 11 months ago

shifengbin commented 11 months ago

Preflight Checklist

Viper Version

1.16.0

Go Version

1.20.7

Config Source

Remove K/V stores

Format

JSON

Repl.it link

No response

Code reproducing the issue

type remoteConfigProvider struct{}

func (rc remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
    log.Println("Getting config from remote", rp)
    r := bytes.NewReader([]byte("{\"a\":1}"))
    time.Sleep(time.Second)
    return r, nil
}

func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {

    log.Println("Watching config from remote", rp)
    s := fmt.Sprintf(`{"app":{"name":"test","version":%d}}`, time.Now().Unix())
    r := bytes.NewReader([]byte(s))
    time.Sleep(time.Second)
    return r, nil
}

func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
    log.Println("Watching  Channel config from remote", rp)
    ch := make(chan *viper.RemoteResponse)
    go func() {
        defer close(ch)

        for i := 0; ; i++ {
            ch <- &viper.RemoteResponse{
                Value: []byte(fmt.Sprintf(`{"app":{"name":"test","version":%d}}`, time.Now().Unix())),
            }
            time.Sleep(time.Second)
        }

    }()
    return ch, nil
}
func init() {
    viper.RemoteConfig = remoteConfigProvider{}
    viper.SupportedRemoteProviders = []string{"app"}
}
viper.SetConfigType("json")
    viper.AddRemoteProvider("app", ":8080", "/aabb/cc")
    viper.ReadRemoteConfig()
    fmt.Println("-----------", viper.GetInt64("b"))

    viper.RegisterAlias("a", "b")
    // viper.WatchRemoteConfig()
    viper.GetViper().WatchRemoteConfigOnChannel()
    // viper.WatchConfig()

    fmt.Println("-----------")
    go func() {
        time.Sleep(time.Second)
        for i := 0; i < 10000; i++ {
            fmt.Println(viper.GetInt64("A.b"), viper.Get("app"), viper.AllKeys())
            // if i%1000 == 0 {
            //  time.Sleep(time.Second)
            // }
            time.Sleep(time.Second)
        }
    }()

Expected Behavior

update config

Actual Behavior

panic

Steps To Reproduce

use "Code reproducing the issue" content

Additional Information

I commit a pr #1635

github-actions[bot] commented 11 months ago

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news, either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️