Skip to content

Commit

Permalink
Merge pull request #139 from getanteon/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
fatihbaltaci authored May 2, 2024
2 parents c2bcb6c + b961bf6 commit b45ce80
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion logstreamer/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"sync"
"time"

"github.com/ddosify/alaz/log"
)
Expand All @@ -20,6 +21,29 @@ type PoolConn struct {
tls bool
}

func (p *PoolConn) isAlive() bool {
var buf [1]byte
p.SetReadDeadline(time.Now().Add(1 * time.Millisecond)) // Set a very short deadline
_, err := p.Read(buf[:])

if err != nil {
if e, ok := err.(net.Error); ok && e.Timeout() {
// Timeout occurred, but connection is still alive
return true
}
// Real error or EOF encountered, connection likely dead
return false
}

if buf[0] == 'X' {
// close
return false
}

// Data received (unexpected in send only), process or ignore
return true
}

// Close() puts the given connects back to the pool instead of closing it.
func (p *PoolConn) Close() error {
p.mu.RLock()
Expand Down Expand Up @@ -94,7 +118,13 @@ func (c *channelPool) Get() (*PoolConn, error) {
return nil, ErrClosed
}

return conn, nil
if conn.isAlive() {
return conn, nil
} else {
conn.MarkUnusable()
conn.Close()
return nil, ErrClosed
}
default:
conn, err := factory()
if err != nil {
Expand Down

0 comments on commit b45ce80

Please sign in to comment.