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

question: trpc: Is there any way I could know whether an *server.Server is new-ed? #175

Closed
2 tasks done
Andrew-M-C opened this issue May 6, 2024 · 4 comments
Closed
2 tasks done
Labels
question Further information is requested

Comments

@Andrew-M-C
Copy link
Contributor

Preliminary Research

  • I have read the project's documentation.
  • I have searched existing issues for similar questions to see if my question has already been addressed.

Question

As we know, a trpc app should and should only invoke trpc.NewServer() once. After invoking this function, a server is initialized and all associated modules are ready to use.

I am now developing an plugin depending on other plugins, etcd for example. However, at start-up procedure, when I callled config.Get("etcd") and got nil, I am not sure of the reason. If it was because that etcd was not properly configurred? Or that etcd is confgurred but simply not initialized by trpc framework yet.

So, is there any way that I can know at least one Server is new-ed? Which indicates that trpc server is completely initialized.

Additional Information

N/A

@Andrew-M-C Andrew-M-C added the question Further information is requested label May 6, 2024
@WineChord
Copy link
Contributor

The design appearance of trpc.NewServer() implies that it is used to create a new server instance. However, the current implementation modifies a multitude of global variables found in packages such as config and client, as seen here. Encountering an error from config.Get returning nil is indicative of the plugins not being initialized, which is part of the trpc.NewServer() routine.

Another global variable could be introduced to indicate the creation of a new server instance. However, this approach might lead to further deterioration of the code and is unlikely to be a favorable solution.

A more viable strategy would be to refrain from using config.Get within init functions. Instead, it is advisable to access plugin-related information only after the execution of trpc.NewServer to ensure that all necessary initializations have been performed.

@Andrew-M-C
Copy link
Contributor Author

What I am doing now is the second solution you mentioned, to implicitly invoke the initialization function after NewServer. I just wonder if there is another elegant way.

Is this acceptable: Allow plugins declaring its precondition? The plugin's Setup() will only be called if its depending plugins are set-up?

@WineChord
Copy link
Contributor

Checkout the DependsOn and FlexDependsOn interface: https://github.com/trpc-group/trpc-go/blob/main/plugin/README.md#plugin-initialization-order

@Andrew-M-C
Copy link
Contributor Author

wow, that seems to meet my case! I will try it at once, THX!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants