多次读取配置文件或使用 gen_server 状态?

发布于 2024-11-09 13:19:27 字数 429 浏览 0 评论 0原文

我使用 gen_server 和 gen_fsm 实现了一个 otp 系统。有一个配置文件需要读取软件运行所需的某些值,一个示例可能是:

{values, [value1, value2, value3]}.

我使用宏来提取这些值之一

define(VALUES, my_utility:get_conf_value(values)). 

问题如下:由于 ?VALUES 可能会经常被调用,因此配置文件被解析很多次,我是否应该将 ?VALUES 嵌入到 gen_fsm 的 gen_server 状态中,并在需要时通过调用提取它?

事实上,我真的很欣赏以前的实现,因为只需更改配置文件内的值即可更改软件的行为,而无需任何 #state{} 更改或调用。

您更喜欢哪种解决方案?

I implemented an otp system using gen_server and gen_fsm. There is a configuration file to be read for some values that the software needs in order to run, an example may be:

{values, [value1, value2, value3]}.

I used a macro to extract one of these values

define(VALUES, my_utility:get_conf_value(values)). 

The question is the following: since ?VALUES may be called very often, and therefore the configuration file is parsed many times, should I embed ?VALUES inside the state of my gen_server of gen_fsm and extract it with a call any time I need it?

In fact I really appreciated the previous implementation because one could change the behaviour of the software just by changing the values inside the configuration file, without any #state{} change or call.

Which solution do you prefer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2024-11-16 13:19:27

解决方案将取决于您的要求。性能与“正确性”。

一种可能的解决方案是将配置保持在进程状态并定期重新读取(检查文件修改时间是否已更改)。这可能是两个世界之间的一个很好的妥协。

摘要:

  • 每次从文件中重新读取:始终是最新的,但由于不必要的 IO 速度较慢
  • 间隔重新读取,存储在进程状态中:读取速度快,但落后
  • 手动重新读取:读取速度快,但落后需要手动触发
  • 读取一次并存储在进程状态中:读取速度快,需要重启才能更新

未考虑的要求:安全性、稳定性(配置文件损坏?)

The solution will depend upon your requirements. Performance vs "correctness".

A possible solution would be to keep the configuration in a process state and re-read it regularly (checking if the file modification time has changed). This might be a good compromise between the two worlds.

Summary:

  • Re-read from file everytime: always up to date, but slow with unnecessary IO
  • Re-read on interval, store in process state: fast to read, but lagging behind
  • Manual re-read: fast to read, but lagging behind and needs manual trigger
  • Read once and store in process state: fast to read, needs restart to update

Requirements not considered: security, stability (corrupt config file?)

冷︶言冷语的世界 2024-11-16 13:19:27

这实际上是一个比人们最初想象的要困难得多的主题。例如:

  • 配置如何更改?
  • 如果有变化,是否有任何流程需要通知?
  • 您是否将配置存储在某个中间进程/状态中?这个必须要清除吗?
  • 如何确保每个人在所有不同节点上的所有不同进程中看到相同的配置?
  • 或者即使配置不正确,您的系统也能正常运行吗?
  • 访问配置的频率如何?
  • 你在那里存储什么数据?

因此,分析您当前的解决方案:

  • 如果您关心这些问题,每次需要值时解析配置文件会太慢并产生太多垃圾
  • 向 gen_server 询问值也可能太慢,具体取决于您的用例。
  • 如果您需要进行多次查找,gen_server 也可能会过载
  • 如果数据很大,则 gen_server 和您的进程之间的复制可能会发生
  • 如果您的应用程序在多台计算机上运行并且您不能容忍配置不一致,则必须协调更新尽可能接近同一时间发生(如果您关心一致性,您可以使用我能想到的任何解决方案)

另一个解决方案是将配置存储为代码。例如,您可以有一个特殊的配置模块,您可以使用解析转换或生成实际的源文件来创建该模块。使用此解决方案,配置值驻留在虚拟机的常量池中,获取它们会产生尽可能少的垃圾(取决于您稍后如何处理数据)。

This is actually a topic that is much harder than what one would first think. For example :

  • How is the configuration changed?
  • Are there any process that should be notified if it changes?
  • Do you store the configuration in some intermediate process/state? Would this have to be cleared?
  • How do you ensure everybody is seeing the same configuration in all different processes on all different nodes?
  • Or is your system able to function even with incorrect configuration?
  • How often is the configuration accessed?
  • What data do you store there?

So, to analyse your current solutions:

  • Parsing config files every time you need a value will be too slow and create too much garbage, if you care about these matters
  • Asking the gen_server for the value might also be too slow, depending on your use case.
  • If you need to do many lookups, the gen_server might also be overloaded
  • If the data is big, copying between the gen_server and your process might
  • If your application is running on multiple machines and you cannot tolerate inconsistency in configuration, you have to orchestrate the update to happen as close as posssible to the same time (this you would have with any solution I can think of, if you care about consistency)

Another solution would be to store the configuration as code. You could for example have a special configuration module, which you create using a parse transform or by generating the actual source file. With this solution, the configuration values resides in the constant pool of the VM and fetching them creates as little garbage as possible (depending on what do you with the data later).

醉殇 2024-11-16 13:19:27

我个人认为最好的解决方案是将配置作为模块进行。好处是:

  • 真的非常快
  • 可以通过热代码更改进行更改
  • 可以从任何形式的配置文件格式机器生成
  • 编译和热代码交换仅通过“正确”配置即可免费为您提供原子更新

P.S.:请参阅网络模块加载(又名 nl /1 in shell) 用于集群范围的配置更改。

Personally I think best solution is make configuration as module. Benefits are:

  • Really very fast
  • Can by changed by hot code change
  • Can be machine generated from any form of config file formats
  • Compiling and hot code swapping gives you atomic update by only "correct" configuration for free

P.S.: See network module load (aka nl/1 in shell) for cluster wide config change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文