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

About setting up a connections pool #27

Closed
jkgeekJack opened this issue Oct 20, 2019 · 1 comment
Closed

About setting up a connections pool #27

jkgeekJack opened this issue Oct 20, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request proposal Proposal for this repo

Comments

@jkgeekJack
Copy link

jkgeekJack commented Oct 20, 2019

func (svr *server) acceptNewConnection(fd int) error {
	nfd, sa, err := unix.Accept(fd)
	if err != nil {
		if err == unix.EAGAIN {
			return nil
		}
		return err
	}
	if err := unix.SetNonblock(nfd, true); err != nil {
		return err
	}
	lp := svr.subLoopGroup.next()
       //可以从pool里面取出来,重置一些参数就行,不然在高并发下对象和ringbuffer的消耗还是挺大的
	c := &conn{
		fd:             nfd,
		sa:             sa,
		loop:           lp,
		inboundBuffer:  ringbuffer.New(socketRingBufferSize),
		outboundBuffer: ringbuffer.New(socketRingBufferSize),
	}
	_ = lp.loopOpen(c)
	_ = lp.poller.Trigger(func() (err error) {
		if err = lp.poller.AddRead(nfd); err == nil {
			lp.connections[nfd] = c
			return
		}
		return
	})
	return nil
}
@jkgeekJack jkgeekJack added enhancement New feature or request proposal Proposal for this repo labels Oct 20, 2019
@panjf2000
Copy link
Owner

panjf2000 commented Oct 20, 2019

你是说用一个临时对象池 sync.Pool 来 cache conn?连接关闭之后放回 pool,下次新建连接直接从 pool 取?

@panjf2000 panjf2000 changed the title New connection can be getted form pool About setting up a connections pool Oct 20, 2019
panjf2000 added a commit that referenced this issue Oct 25, 2019
0-haha pushed a commit to 0-haha/gnet that referenced this issue Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal Proposal for this repo
Projects
None yet
Development

No branches or pull requests

2 participants