Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor shutdown config, to prevent setting zero values #4533

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

kevwan
Copy link
Contributor

@kevwan kevwan commented Jan 1, 2025

No description provided.

Copy link

codecov bot commented Jan 1, 2025

Codecov Report

Attention: Patch coverage is 70.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 94.56%. Comparing base (8690859) to head (c37d356).
Report is 196 commits behind head on master.

Files with missing lines Patch % Lines
core/proc/shutdown.go 68.42% 6 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
core/service/serviceconf.go 90.32% <100.00%> (+0.32%) ⬆️
core/proc/shutdown.go 75.38% <68.42%> (+1.47%) ⬆️

... and 8 files with indirect coverage changes

@kevwan
Copy link
Contributor Author

kevwan commented Jan 1, 2025

Recommendations

  1. Documentation:

    • Ensure that the new ShutdownConf struct and its fields are well-documented to help future maintainers understand their purpose and usage.
  2. Constants:

    • Consider adding comments to explain the purpose of default values for defaultWrapUpTime and defaultWaitTime.
  3. Test Cases:

    • Add test cases to cover edge cases, such as setting WrapUpTime or WaitTime to zero or negative values and ensure the validation logic works as expected.
  4. Thread Safety:

    • If wrapUpTime and waitTime can be modified concurrently, consider adding mutex protection to ensure thread safety.

Example Code Improvements

  1. Adding Comments for Constants:

    const (
        // defaultWrapUpTime is the default time allowed for wrap-up listeners to complete.
        defaultWrapUpTime = time.Second
        // defaultWaitTime is the default waiting time before force quitting, used because most queues are blocking mode with a 5-second timeout.
        defaultWaitTime = 5500 * time.Millisecond
    )
  2. Mutex Protection for Global Variables:

    var (
        mu sync.RWMutex
        wrapUpListeners   = new(listenerManager)
        shutdownListeners = new(listenerManager)
        wrapUpTime        = defaultWrapUpTime
        waitTime          = defaultWaitTime
    )
    
    func Setup(conf ShutdownConf) error {
        mu.Lock()
        defer mu.Unlock()
        if conf.WrapUpTime > 0 {
            wrapUpTime = conf.WrapUpTime
        }
        if conf.WaitTime > 0 {
            waitTime = conf.WaitTime
        }
        return nil
    }
    
    func SetTimeToForceQuit(duration time.Duration) {
        mu.Lock()
        defer mu.Unlock()
        waitTime = duration
    }

These improvements would enhance the code's robustness, readability, and maintainability.

@kevwan
Copy link
Contributor Author

kevwan commented Jan 1, 2025

All recommendations finished.

@kevwan kevwan requested a review from zhoushuguang January 1, 2025 12:38
@kevwan kevwan merged commit 28a001c into zeromicro:master Jan 1, 2025
5 of 6 checks passed
@kevwan kevwan deleted the chore/shutdown branch January 1, 2025 12:49
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.

1 participant