netpoll icon indicating copy to clipboard operation
netpoll copied to clipboard

connection的Write与Flush方法返回的error不正确的问题

Open JHue58 opened this issue 10 months ago • 2 comments

这两个方法在lock不成功时也会返回ErrConnClosed,可能是由使用者不小心并发调用导致的,但连接并没有closed,这对debug会有一定的误导性

func (c *connection) Flush() error {
	if !c.IsActive() || !c.lock(flushing) {
		return Exception(ErrConnClosed, "when flush")
	}
	defer c.unlock(flushing)
	c.outputBuffer.Flush()
	return c.flush()
}

func (c *connection) Write(p []byte) (n int, err error) {
	if !c.IsActive() || !c.lock(flushing) {
		return 0, Exception(ErrConnClosed, "when write")
	}
	defer c.unlock(flushing)

	dst, _ := c.outputBuffer.Malloc(len(p))
	n = copy(dst, p)
	c.outputBuffer.Flush()
	err = c.flush()
	return n, err
}

JHue58 avatar Apr 08 '24 01:04 JHue58

@JHue58 记录了,会把两个if条件分开处理

joway avatar Apr 08 '24 05:04 joway

这个issue看起来好像还没有提PR,我想给这个issue提个PR,所以可以把这个PR分配给我么

TremblingV5 avatar May 21 '24 03:05 TremblingV5

@TremblingV5 Hi,你可以提pr,我标志了这个issue 是 good first issue。

joway avatar May 27 '24 03:05 joway

此外,你现在的commit有两个地方可以改下:

  1. ErrFailedLockFlushing 命名上不需要标注 failed , Err 前缀已经暗示了失败的意思。可以叫做:ErrConcurrentAccess = "concurrent connection access"
  2. 不需要抽象成函数,因为 lock unlock 最好写在一个函数里

joway avatar May 27 '24 03:05 joway

此外,你现在的commit有两个地方可以改下:

  1. ErrFailedLockFlushing 命名上不需要标注 failed , Err 前缀已经暗示了失败的意思。可以叫做:ErrConcurrentAccess = "concurrent connection access"
  2. 不需要抽象成函数,因为 lock unlock 最好写在一个函数里

好嘞,已知晓

TremblingV5 avatar May 27 '24 08:05 TremblingV5