Skip to content

Commit

Permalink
fix: return a correct error when failed to access concurrent connecti…
Browse files Browse the repository at this point in the history
…ons (#332)

Co-authored-by: TremblingV5 <[email protected]>
  • Loading branch information
TremblingV5 and TremblingV5 authored May 29, 2024
1 parent a9a224c commit 3e411b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
17 changes: 10 additions & 7 deletions connection_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
ErrEOF = syscall.Errno(0x106)
// Write I/O buffer timeout, calling by Connection.Writer
ErrWriteTimeout = syscall.Errno(0x107)
// Concurrent connection access error
ErrConcurrentAccess = syscall.Errno(0x108)
)

const ErrnoMask = 0xFF
Expand Down Expand Up @@ -110,11 +112,12 @@ func (e *exception) Temporary() bool {

// Errors defined in netpoll
var errnos = [...]string{
ErrnoMask & ErrConnClosed: "connection has been closed",
ErrnoMask & ErrReadTimeout: "connection read timeout",
ErrnoMask & ErrDialTimeout: "dial wait timeout",
ErrnoMask & ErrDialNoDeadline: "dial no deadline",
ErrnoMask & ErrUnsupported: "netpoll dose not support",
ErrnoMask & ErrEOF: "EOF",
ErrnoMask & ErrWriteTimeout: "connection write timeout",
ErrnoMask & ErrConnClosed: "connection has been closed",
ErrnoMask & ErrReadTimeout: "connection read timeout",
ErrnoMask & ErrDialTimeout: "dial wait timeout",
ErrnoMask & ErrDialNoDeadline: "dial no deadline",
ErrnoMask & ErrUnsupported: "netpoll does not support",
ErrnoMask & ErrEOF: "EOF",
ErrnoMask & ErrWriteTimeout: "connection write timeout",
ErrnoMask & ErrConcurrentAccess: "concurrent connection access",
}
13 changes: 11 additions & 2 deletions connection_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,15 @@ func (c *connection) MallocLen() (length int) {
// If empty, it will call syscall.Write to send data directly,
// otherwise the buffer will be sent asynchronously by the epoll trigger.
func (c *connection) Flush() error {
if !c.IsActive() || !c.lock(flushing) {
if !c.IsActive() {
return Exception(ErrConnClosed, "when flush")
}

if !c.lock(flushing) {
return Exception(ErrConcurrentAccess, "when flush")
}
defer c.unlock(flushing)

c.outputBuffer.Flush()
return c.flush()
}
Expand Down Expand Up @@ -282,9 +287,13 @@ func (c *connection) Read(p []byte) (n int, err error) {

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

if !c.lock(flushing) {
return 0, Exception(ErrConcurrentAccess, "when write")
}
defer c.unlock(flushing)

dst, _ := c.outputBuffer.Malloc(len(p))
Expand Down

0 comments on commit 3e411b1

Please sign in to comment.