Initial import
This commit is contained in:
37
internal/netutil/conn.go
Normal file
37
internal/netutil/conn.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"git.maze.io/maze/conduit/logger"
|
||||
)
|
||||
|
||||
type ConnCloser struct {
|
||||
net.Conn
|
||||
Closer func() error
|
||||
}
|
||||
|
||||
func (c *ConnCloser) Close() error {
|
||||
if c.Closer == nil {
|
||||
return c.Conn.Close()
|
||||
}
|
||||
return c.Closer()
|
||||
}
|
||||
|
||||
// IsClosing checks if the error is because the connection was closed.
|
||||
func IsClosing(err error) bool {
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, syscall.ECONNRESET) {
|
||||
return true
|
||||
}
|
||||
if err, ok := err.(net.Error); ok && err.Timeout() {
|
||||
return true
|
||||
}
|
||||
if err, ok := err.(*net.OpError); ok && err.Op == "close" {
|
||||
return true
|
||||
}
|
||||
logger.Debugf("not a closing error %T: %#+v", err, err)
|
||||
return false
|
||||
}
|
23
internal/stringutil/iter.go
Normal file
23
internal/stringutil/iter.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package stringutil
|
||||
|
||||
import "sort"
|
||||
|
||||
func MapKeys(m map[string]string) <-chan string {
|
||||
ch := make(chan string)
|
||||
if m == nil {
|
||||
close(ch)
|
||||
} else {
|
||||
go func(ch chan<- string) {
|
||||
defer close(ch)
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
ch <- k
|
||||
}
|
||||
}(ch)
|
||||
}
|
||||
return ch
|
||||
}
|
Reference in New Issue
Block a user