Code Monkey home page Code Monkey logo

Comments (3)

Choubeihai avatar Choubeihai commented on June 3, 2024
 // Send 用来在非 loop 协程发送
func (c *Connection) Send(data interface{}, opts ...ConnectionOption) error {
  if !c.connected.Get() {
  	return ErrConnectionClosed
  }

  opt := ConnectionOptions{}
  for _, o := range opts {
  	o(&opt)
  }

  c.loop.QueueInLoop(func() {
  	if c.connected.Get() {
  		bytesAfterPackets := c.protocol.Packet(c, data)
  		c.sendInLoop(bytesAfterPackets)

  		if opt.sendInLoopFinish != nil {
  			opt.sendInLoopFinish(data)
  		}
  	}
  })
  return nil
}

还有这个,Send这个API我理解是异步发送消息,触发的是eventfd,这个eventfd仍然是被epoll管理的,最终也是在epoll_wait中(gev里面的poll)中被触发,这不也是在eventloop协程中发送吗?但是您的注释是非loop协程发送。

from gev.

Allenxuxu avatar Allenxuxu commented on June 3, 2024
// Close 关闭连接
func (c *Connection) Close() error {
  if !c.connected.Get() {
  	return ErrConnectionClosed
  }

  c.loop.QueueInLoop(func() {
  	c.handleClose(c.fd)
  })
  return nil
}

关闭连接时,为什么不直接关闭连接,而是将【关闭连接】这个任务放到异步队列里面等待执行?

为了线程安全

from gev.

Allenxuxu avatar Allenxuxu commented on June 3, 2024
 // Send 用来在非 loop 协程发送
func (c *Connection) Send(data interface{}, opts ...ConnectionOption) error {
  if !c.connected.Get() {
  	return ErrConnectionClosed
  }

  opt := ConnectionOptions{}
  for _, o := range opts {
  	o(&opt)
  }

  c.loop.QueueInLoop(func() {
  	if c.connected.Get() {
  		bytesAfterPackets := c.protocol.Packet(c, data)
  		c.sendInLoop(bytesAfterPackets)

  		if opt.sendInLoopFinish != nil {
  			opt.sendInLoopFinish(data)
  		}
  	}
  })
  return nil
}

还有这个,Send这个API我理解是异步发送消息,触发的是eventfd,这个eventfd仍然是被epoll管理的,最终也是在epoll_wait中(gev里面的poll)中被触发,这不也是在eventloop协程中发送吗?但是您的注释是非loop协程发送。

注释不太准确,意思是这个方法可以不在 loop 中执行

from gev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.