nats-server icon indicating copy to clipboard operation
nats-server copied to clipboard

[refactor]: encapsulate the network logic into internal/network folder

Open xieyuschen opened this issue 2 years ago • 6 comments

Currently, nats-server provides many network protocol support such as mqtt and websocket. But the code management seems not good and it might block the further network feature such as adding quic support.

  • code coupling:
    Now all network details are in the server package which is easy confusing both developers and people who read the source code. The code management way here is more like what python does, each file handles one package. For example, in the server package, there are both mqtt and websocket and in the future, there might be quic. To clarify which the function relationship, we do this:
// this is a websocket function as it has prefix ws.
func (c *client) wsRead(r *wsReadInfo, ior io.Reader, buf []byte) ([][]byte, error) 

// this is a mqtt function.
func (c *client) mqttConnectTrace(cp *mqttConnectProto) string {

Instead, we should encapsulate those function like the following.

package websocket
func (c *client) Read(r *wsReadInfo, ior io.Reader, buf []byte) ([][]byte, error) 
package mqtt
func (c *client) ConnectTrace(cp *mqttConnectProto) string

What's worse, all unexported function are visable in server package but they should not. I think it's better to encapsulate the network into a single package to make the code cleaner.

Thanks

xieyuschen avatar Jun 10 '22 05:06 xieyuschen

For the code coupling, could also see the #3174 . To enable a global logger which binds to the server, we could decouple many functions from the server struct.

Thanks.

xieyuschen avatar Jun 10 '22 05:06 xieyuschen

Will have @kozlovic take a look once back from extended holiday.

derekcollison avatar Jun 10 '22 17:06 derekcollison

Will have @kozlovic take a look once back from extended holiday.

Got it, could I know about the approximate agenda about him? Thanks

xieyuschen avatar Jun 11 '22 07:06 xieyuschen

A few weeks.

derekcollison avatar Jun 11 '22 16:06 derekcollison

I am picking this up possibly looking forward to v2.10.0.

derekcollison avatar Nov 17 '22 19:11 derekcollison

I am picking this up possibly looking forward to v2.10.0.

Glad to here it:) Regards

xieyuschen avatar Nov 18 '22 10:11 xieyuschen