// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . package configloaders import ( "bytes" "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/userconfigs" "github.com/TeaOSLab/EdgeUser/internal/remotelogs" "github.com/TeaOSLab/EdgeUser/internal/rpc" "time" ) var sharedUserPriceConfig *userconfigs.UserPriceConfig var sharedUserPriceJSON []byte func init() { var ticker = time.NewTicker(1 * time.Minute) go func() { for range ticker.C { _, err := LoadUserPriceConfig() if err != nil { if rpc.IsConnError(err) { remotelogs.Debug("LoadUserPriceConfig", err.Error()) } else { remotelogs.Error("LoadUserPriceConfig", err.Error()) } } } }() } // LoadUserPriceConfig 加载用户计费设置 // 在没有error的情况下,需要保证一定会返回一个不为空的配置 func LoadUserPriceConfig() (*userconfigs.UserPriceConfig, error) { rpcClient, err := rpc.SharedRPC() if err != nil { return nil, err } resp, err := rpcClient.SysSettingRPC().ReadSysSetting(rpcClient.Context(0), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeUserPriceConfig}) if err != nil { return nil, err } // 如果是相同,则不更新 if bytes.Equal(resp.ValueJSON, sharedUserPriceJSON) { return sharedUserPriceConfig, nil } var config = userconfigs.DefaultUserPriceConfig() if len(resp.ValueJSON) == 0 { return config, nil } err = json.Unmarshal(resp.ValueJSON, config) if err != nil { return nil, err } sharedUserPriceConfig = config sharedUserPriceJSON = resp.ValueJSON return config, nil } func LoadCacheableUserPriceConfig() (*userconfigs.UserPriceConfig, error) { if sharedUserPriceConfig != nil { // clone是防止被修改 return sharedUserPriceConfig.Clone() } return LoadUserPriceConfig() }