Skip to content

Fix/viper race condition#114

Draft
ljluestc wants to merge 4 commits intoxinliangnote:masterfrom
ljluestc:fix/viper-race-condition
Draft

Fix/viper race condition#114
ljluestc wants to merge 4 commits intoxinliangnote:masterfrom
ljluestc:fix/viper-race-condition

Conversation

@ljluestc
Copy link
Copy Markdown

Fix: race condition in viper config handling

Description

This PR fixes a race condition in the configs package where viper.WatchConfig updates the configuration concurrently with read operations.
It introduces a sync.RWMutex to protect access to the global config variable.

Issue

Fixes #108

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (no functional changes, no api changes)
  • Documentation
  • Test

Checklist

  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have commented my code, particularly in hard-to-understand areas

Verification

Run the race detector test:

go test -v -race ./configs/...

Output:

=== RUN   TestRace
--- PASS: TestRace (0.05s)
PASS
ok      github.com/xinliangnote/go-gin-api/configs      1.065s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viper监听配置文件修改的时候,变量读和写,在不同的线程,这里是否要加锁

1 participant