Skip to content

[FIXED] Processes config file during embeded server initialization.#7333

Closed
yixianOu wants to merge 3 commits intonats-io:mainfrom
yixianOu:main
Closed

[FIXED] Processes config file during embeded server initialization.#7333
yixianOu wants to merge 3 commits intonats-io:mainfrom
yixianOu:main

Conversation

@yixianOu
Copy link
Copy Markdown
Contributor

@yixianOu yixianOu commented Sep 19, 2025

Resolves #7092

Edited the NewServer function in the server package , server.go file. Using the ProcessConfigFile method of the Options type, to enable the embeded nats-server to read the config file

Prevent configuration files from being processed twice by checking opts.configDigest before calling ProcessConfigFile in NewServer.

Signed-off-by: orician lzjzxOYX201905@gmail.com

Prevent configuration files from being processed twice by checking
opts.configDigest before calling ProcessConfigFile in NewServer.

This fixes account isolation test failures caused by duplicate
configuration processing.

Signed-off-by: orician <lzjzxOYX201905@gmail.com>
@yixianOu yixianOu requested a review from a team as a code owner September 19, 2025 03:28
@yixianOu yixianOu closed this Sep 19, 2025
@yixianOu yixianOu reopened this Sep 19, 2025
@yixianOu
Copy link
Copy Markdown
Contributor Author

This feature was discussed in #7093, but I still believe that if the embedded NATS server could provide the capability to read configuration files, it would make embedding NATS in Go much more user-friendly.

Copy link
Copy Markdown
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Principally looks good to me, but I think we need a unit test to prove this behaviour. You should be able to plagiarise a good bit of TestLargeMaxPayload in opts_test.go for example but replace the LoadConfig call in the test with a config struct.

@ripienaar
Copy link
Copy Markdown
Contributor

I am still pro NewServerFromConfig() instead of this

@yixianOu
Copy link
Copy Markdown
Contributor Author

Thank you for your suggestion. I will refer to TestLargeMaxPayload to write test cases for this change, and I will also explore whether NewServerFromConfig() can provide a better implementation.

@yixianOu
Copy link
Copy Markdown
Contributor Author

I am still pro NewServerFromConfig() instead of this

This is a good approach considering forward compatibility, but I'd like to first test whether this change might be disruptive.

Enable embedded NATS servers to automatically process configuration files
when using Options struct with ConfigFile set, addressing the limitation
noted in PR nats-io#7333 comments.

Changes:
- Add config file processing in NewServer() when configDigest is empty
- Process config files automatically for embedded servers without requiring LoadConfig
- Add proper error handling for config file processing failures

Tests added:
- TestConfigStructReplacesLoadConfig: Validates Options struct replaces LoadConfig calls
- TestConfigFileProcessingComparison: Compares LoadConfig vs Options.ProcessConfigFile behavior
- TestConfigFileHotReloadWithEmbeddedServer: Ensures hot reload works with embedded servers

The implementation ensures backward compatibility and doesn't interfere with
existing configuration reload mechanisms. Both traditional LoadConfig and
new Options struct approaches support identical functionality including
hot reload capabilities.

Resolves embedded server config processing as requested in PR review feedback.

Signed-off-by: orician <lzjzxOYX201905@gmail.com>
@yixianOu
Copy link
Copy Markdown
Contributor Author

Principally looks good to me, but I think we need a unit test to prove this behaviour. You should be able to plagiarise a good bit of TestLargeMaxPayload in opts_test.go for example but replace the LoadConfig call in the test with a config struct.

Add proper error handling for config file processing failures in NewServer
Added 3 new testcases in opts_test.go :

  • TestConfigStructReplacesLoadConfig: Validates Options struct replaces LoadConfig calls
  • TestConfigFileProcessingComparison: Compares LoadConfig vs Options.ProcessConfigFile(called in NewServer) behavior
  • TestConfigFileHotReloadWithEmbeddedServer: Ensures hot reload works with embedded servers

@yixianOu
Copy link
Copy Markdown
Contributor Author

Should I use NewServerFromConfig() to implement the feature instead of directly modifying NewServer()?

neilalexander added a commit that referenced this pull request Oct 1, 2025
…cessing (#7364)

# Add NewServerFromConfig function for embedded server config processing

## Overview

Resolves #7092

This PR introduces a new `NewServerFromConfig()` function that
automatically processes configuration files for embedded NATS servers.

## Problem

When using NATS embedded in Go applications, developers (such as me)
often pass the `ConfigFile` field in the `Options` struct, but
`NewServer(opts *Options)` doesn't automatically process this
configuration file. This leads to confusion and requires additional
manual steps to load the configuration.

## Solution

### Approach Comparison

In pr #7333, a solution was proposed to directly modify `NewServer()`:

```go
func NewServer(opts *Options) (*Server, error) {
    if opts.ConfigFile != _EMPTY_ && opts.configDigest == "" {
        if err := opts.ProcessConfigFile(opts.ConfigFile); err != nil {
            return nil, err
        }
    }
    // ...original code
}
```

However, directly modifying `NewServer()` might raise concerns about
backward compatibility. Therefore, this PR introduces a dedicated
`NewServerFromConfig()` function specifically to handle the `ConfigFile`
field of `*Options`.

### Implementation

This PR adds:

```go
func NewServerFromConfig(opts *Options) (*Server, error) {
    if opts.ConfigFile != _EMPTY_ && opts.configDigest == "" {
        if err := opts.ProcessConfigFile(opts.ConfigFile); err != nil {
            return nil, err
        }
    }
    return NewServer(opts)
}
```

## Testing

Added comprehensive test coverage:

- **`TestNewServerFromConfigFunctionality`**: Tests basic functionality
and error handling scenarios
- **`TestNewServerFromConfigVsLoadConfig`**: Validates equivalence with
traditional `LoadConfig()` approach using deep equality checks

## Acknowledgments

Thanks to @ripienaar for the suggestion to create a separate function
instead of modifying `NewServer()` directly.

## Signing Off

Signed-off-by: orician lzjzxOYX201905@gmail.com
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.

Go client sdk built-in nats-server unable to read configuration file by passing ConfigFile Options.

3 participants