经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
配置文件动态刷新
来源:cnblogs  作者:喜欢兰花山丘  时间:2022/1/17 11:10:27  对本文有异议

目录

 1. 背景

2. 相关知识储备

思路一: 民科 mtime 文件最后修改时间

思路二: 科班 操作系统通知特性, 例如 linux 的 inotify

3. 相关代码设计

3.1 简单实用版

3.2 尝试多线程

3.3 多线程版本

4. 总结

 

正文

 1. 背景

配置文件动态刷新这个业务场景非常常见. 存在两个主要使用场景, 客户端和服务器. 

客户端需求很直白, 我本地配置变更, 程序能及时和非及时的重刷到系统中. 

服务器相比客户端做法要多些环节, 服务器本地会有一份配置兜底, 配置中心中配置发生改变会推送给触发给服务器触发内部更新操作.

我们这里主要聊场景偏向于客户端, 本地配置发生改变, 我们如何来更新内存中配置

 

文章承接于: C中级 - 文件辅助操作

 

2. 相关知识储备

首先思考一个问题我们如何判断一个文件发生了更新 ?

 这里提供两种思路. 

思路一: 民科 mtime 文件最后修改时间

  1. struct stat {
  2. unsigned long st_dev; /* Device. */
  3. unsigned long st_ino; /* File serial number. */
  4. unsigned int st_mode; /* File mode. */
  5. unsigned int st_nlink; /* Link count. */
  6. unsigned int st_uid; /* User ID of the file's owner. */
  7. unsigned int st_gid; /* Group ID of the file's group. */
  8. unsigned long st_rdev; /* Device number, if device. */
  9. unsigned long __pad1;
  10. long st_size; /* Size of file, in bytes. */
  11. int st_blksize; /* Optimal block size for I/O. */
  12. int __pad2;
  13. long st_blocks; /* Number 512-byte blocks allocated. */
  14. long st_atime; /* Time of last access. */
  15. unsigned long st_atime_nsec;
  16. long st_mtime; /* Time of last modification. */
  17. unsigned long st_mtime_nsec;
  18. long st_ctime; /* Time of last status change. */
  19. unsigned long st_ctime_nsec;
  20. unsigned int __unused4;
  21. unsigned int __unused5;
  22. };

在结构体中 st_atime, st_mtime, st_ctime 字段可以知道, Linux 文件有三个时间属性:

1. mtime: 文件内容最后修改时间

2. ctime: 文件状态改变时间, 如权限, 属性被更改

3. atime: 文件内容被访问时间

如 cat, less 等 在默认情况下, ls 显示出来的是该文件的 mtime, 即文件内容最后修改时间.

如果你需要查看另外两个时间, 可以使用 ls -l --time ctime 命令.

思路二: 科班 操作系统通知特性, 例如 linux 的 inotify

man inotify

inotify_init1 -> inotify_add_watch IN_MODIFY / inotify_rm_watch -> poll 监控机制 -> close

  1. IN_MODIFY (+)
  2. File was modified (e.g., write(2), truncate(2)).

流程去注册关注修改时间, 当文件发生修改时候操作系统会通知上层应用具体修改详情.

linux inotify 是一种文件变化通知机制, 它是一个内核用于通知用户空间程序文件系统变化的机制,

以便用户态能够及时地得知内核或底层硬件设备发生了什么.

 

我们这里采用民科思路. linux inotify 对于我们场景有点大材小用了. 欢迎感兴趣人参照官方例子去尝试.

3. 相关代码设计

3.1 简单实用版

素材: 

https://github.com/wangzhione/structc/blob/9de5200229845c4c7acf921ca63c794918b28fe5/modular/system/file.h

https://github.com/wangzhione/structc/blob/9de5200229845c4c7acf921ca63c794918b28fe5/modular/system/file.c

业务能力设计 file_set 注册和删除, file_update 触发检查和更新操作

  1. #pragma once
  2. #include "struct.h"
  3. #include "strext.h"
  4.  
  5. //
  6. // file_f - 文件更新行为
  7. //
  8. typedef void (* file_f)(FILE * c, void * arg);
  9. //
  10. // file_set - 文件注册更新行为
  11. // path : 文件路径
  12. // func : NULL 标记清除, 正常 update -> func(path -> FILE, arg)
  13. // arg : func 额外参数
  14. // return : void
  15. //
  16. extern void file_set(const char * path, file_f func, void * arg);
  17. //
  18. // file_update - 配置文件刷新操作
  19. // return : void
  20. //
  21. extern void file_update(void);

具体思路是利用 list + mtime , 可以观察 struct 设计部分

  1. #include "file.h"
  2.  
  3. struct file {
  4. time_t last; // 文件最后修改时间点
  5. char * path; // 文件全路径
  6. unsigned hash; // 文件路径 hash 值
  7. file_f func; // 执行行为
  8. void * arg; // 行为参数
  9.  
  10. struct file * next; // 文件下一个结点
  11. };
  12. static struct file * file_create(const char * path, unsigned h, file_f func, void * arg) {
  13. assert(path && func);
  14. if (fmtime(path) == -1) {
  15. RETURN(NULL, "mtime error p = %s", path);
  16. }
  17. struct file * fu = malloc(sizeof(struct file));
  18. if (NULL == fu) {
  19. return NULL;
  20. }
  21. fu->last = -1;
  22. fu->path = strdup(path);
  23. if (NULL == fu->path) {
  24. free(fu);
  25. return NULL;
  26. }
  27. fu->hash = h;
  28. fu->func = func;
  29. fu->arg = arg;
  30. // fu->next = NULL;
  31.  
  32. return fu;
  33. }
  34. inline void file_delete(struct file * fu) {
  35. free(fu->path);
  36. free(fu);
  37. }
  38. static struct files {
  39. struct file * list; // 当前文件对象集
  40. } f_s;
  41. // files add
  42. static void f_s_add(const char * path, unsigned hash, file_f func, void * arg) {
  43. struct file * fu = file_create(path, hash, func, arg);
  44. if (fu == NULL) {
  45. return;
  46. }
  47. // 直接插入到头结点部分
  48. fu->next = f_s.list;
  49. f_s.list = fu;
  50. }

struct file 存储文件操作对象, struct files 是 struct file list 集合. 

3.2 尝试多线程

我们知道 file_set 和 file_update 不是线程安全的. 依赖业务系统启动时候统一调用 file_set 无法运行时修改相关设置.

不知道是否有同学会采用如下设计

  1. static struct files {
  2. atomic_flag lock;
  3. struct file * list;
  4. } f_s;

通过 lock 来保证线程安全

这种思路确实能解决线程安全问题, 存在很多缺陷, file_update 业务上面很耗时, 他会阻塞 file_set 操作, 特殊情况会引发业务雪崩.

所以我们需要更针对性锁.

3.3 多线程版本

为了适配多线程情况. 首先我们明确下简单业务, 同步的 file list 就够用了.

我们这里单纯为了没事要吃蛋炒饭态度, 构造 file dict hash + atomic lock 来没事找事. 

素材:

https://github.com/wangzhione/structc/blob/8c040f0cb3507fc4563bc18f48104f9cc20c5da5/modular/system/file.h

https://github.com/wangzhione/structc/blob/8c040f0cb3507fc4563bc18f48104f9cc20c5da5/modular/system/file.c

总体设计思路

  1. #include "file.h"
  2.  
  3. struct file {
  4. time_t last; // 文件最后修改时间点
  5. file_f func; // 执行行为
  6. void * arg; // 行为参数
  7. };
  8. static struct file * file_create(const char * path, file_f func, void * arg) {
  9. assert(path && func);
  10. if (fmtime(path) == -1) {
  11. RETURN(NULL, "mtime error p = %s", path);
  12. }
  13. struct file * fu = malloc(sizeof(struct file));
  14. if (NULL == fu) {
  15. return NULL;
  16. }
  17. fu->last = -1;
  18. fu->func = func;
  19. fu->arg = arg;
  20. return fu;
  21. }
  22. static inline void file_delete(struct file * fu) {
  23. free(fu);
  24. }
  25. struct files {
  26. atomic_flag data_lock;
  27. // const char * path key -> value struct file
  28. // 用于 update 数据
  29. volatile dict_t data;
  30. atomic_flag backup_lock;
  31. // const char * path key -> value struct file
  32. // 在 update 兜底备份数据
  33. volatile dict_t backup;
  34. };
  35. static struct files F = {
  36. .data_lock = ATOMIC_FLAG_INIT,
  37. .backup_lock = ATOMIC_FLAG_INIT,
  38. };
  39. extern void file_init() {
  40. F.data = dict_create(file_delete);
  41. F.backup = dict_create(file_delete);
  42. }

我们先在 data 中添加数据, 如果 data 被 update 占用, 我们把数据放入 backup 中再去处理. 

  1. //
  2. // file_set - 文件注册更新行为
  3. // path : 文件路径
  4. // func : NULL 标识清除, 正常 update -> func(path -> FILE, arg)
  5. // arg : func 额外参数
  6. // return : void
  7. //
  8. void
  9. file_set(const char * path, file_f func, void * arg) {
  10. struct file * fu = NULL;
  11. assert(path && *path);
  12. // step 1 : 尝试竞争 data lock
  13. if (atomic_flag_trylock(&F.data_lock)) {
  14. if (NULL != func) {
  15. fu = file_create(path, func, arg);
  16. }
  17. dict_set(F.data, path, fu);
  18. return atomic_flag_unlock(&F.data_lock);
  19. }
  20. // step 2 : data lock 没有竞争到, 直接竞争 backup lock
  21. atomic_flag_lock(&F.backup_lock);
  22. fu = file_create(path, func, arg);
  23. dict_set(F.backup, path, fu);
  24. atomic_flag_unlock(&F.backup_lock);
  25. }

4. 总结

去感受其中思路. 我用C写代码很顺手. 但有时候觉得 C 在现在阶段, 不是专业吃这个饭的,

可以尝试用其它更加高级语言来轻松快捷表达自己的想法和工程版本.

对于开发生涯我花了很多年找到自己定位, 我的底层核心是一名软件工程师. 然后语言和技术以及商业工程问题陆续通顺起来了. 

(因为我的单元测试不充分, 错误可能很多, 欢迎在 github 给我提 commit or issure. 时间愉快)

 

原文链接:http://www.cnblogs.com/life2refuel/p/15775053.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号