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]: Validate the approach of implementing persistent TCP connections. #568

Closed
3 tasks done
SarthakMakhija opened this issue Apr 4, 2024 · 1 comment
Closed
3 tasks done
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@SarthakMakhija
Copy link

SarthakMakhija commented Apr 4, 2024

Actions I've taken before I'm here

  • I've thoroughly read the documentations about this problem but still have no answer.
  • I've searched the Github Issues/Discussions but didn't find any similar problems that have been solved.
  • I've searched the internet for this problem but didn't find anything helpful.

Questions with details

I have to implement persistent TCP connections from a client to a server for a usecase. The messages received on the connections will be protobuf encoded messages.

Would you have any example of the same? Or, would you be able to provide feedback on the following code?

The example is created using a gnet example.

Code snippets (optional)

func (s *simpleServer) OnBoot(eng gnet.Engine) (action gnet.Action) {
	logging.Infof("running server on %s with multi-core=%t",
		fmt.Sprintf("%s://%s", s.network, s.addr), s.multicore)
	s.eng = eng
	return
}

func (s *simpleServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
	c.SetContext(new(protocol.SimpleCodec)) //some form of codec.
	atomic.AddInt32(&s.connected, 1)
	return
}

func (s *simpleServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
	if err != nil {
		logging.Infof("error occurred on connection=%s, %v\n", c.RemoteAddr().String(), err)
	}
	disconnected := atomic.AddInt32(&s.disconnected, 1)
	connected := atomic.AddInt32(&s.connected, -1)
	if connected == 0 {
		logging.Infof("all %d connections are closed, shut it down", disconnected)
		action = gnet.Shutdown
	}
	return
}

func (s *simpleServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
	codec := c.Context().(*protocol.SimpleCodec) //same codec that was set in `OnOpen`.
	var packets []Message //Assume Message is decoded from the byte slice received from gnet.Conn.
	for {
		data, err := codec.Decode(c)
		if err == protocol.ErrIncompletePacket {
			break
		}
		if err != nil {
			logging.Errorf("invalid packet: %v", err)
			return gnet.Close
		}
		packets = append(packets, data)
	}
        
        //launch goroutines to process the incoming packets, using a workerpool.
        //the goroutine(s) will write the response to the connection using c.AsyncWrite().
	return
}
@SarthakMakhija SarthakMakhija added help wanted Extra attention is needed question Further information is requested labels Apr 4, 2024
@panjf2000
Copy link
Owner

I think you should go ahead and finish your program, then run some tests on it. If you find some issues during testing, then you can open GitHub issues here. It makes more sense to diagnose a program with more concrete details or issues. If you only want to ask some general questions, please go to our discord channel.

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

No branches or pull requests

2 participants