]> Sergey Matveev's repositories - uploader.git/commitdiff
UCSPI connection closing
authorSergey Matveev <stargrave@stargrave.org>
Fri, 21 May 2021 14:36:40 +0000 (17:36 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 21 May 2021 14:36:40 +0000 (17:36 +0300)
main.go
ucspi.go

diff --git a/main.go b/main.go
index a6109acfb6345cffcf9dae244549398394ec5237..8a39053dc18444de2601b8a93039f6dde57cca8f 100644 (file)
--- a/main.go
+++ b/main.go
@@ -229,9 +229,9 @@ func main() {
                ReadHeaderTimeout: 10 * time.Second,
                MaxHeaderBytes:    10 * (1 << 10),
        }
-       s.SetKeepAlivesEnabled(false)
        http.HandleFunc("/upload/", upload)
        if *doUCSPI {
+               s.SetKeepAlivesEnabled(false)
                ln := &UCSPI{}
                s.ConnState = connStater
                err := s.Serve(ln)
index 74c5b7918afa3e8f7f709c187296c528cecf764a..105bae0e0d44131d2e0448eac3d21199a2f0a49f 100644 (file)
--- a/ucspi.go
+++ b/ucspi.go
@@ -18,12 +18,15 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "io"
        "net"
        "net/http"
        "os"
        "time"
 )
 
+var aLongTimeAgo = time.Unix(1, 0)
+
 type UCSPIAddr struct {
        ip   string
        port string
@@ -33,9 +36,28 @@ func (addr *UCSPIAddr) Network() string { return "tcp" }
 
 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
 
-type UCSPIConn struct{}
+type UCSPIConn struct {
+       eof chan struct{}
+}
 
-func (conn *UCSPIConn) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
+type ReadResult struct {
+       n   int
+       err error
+}
+
+func (conn *UCSPIConn) Read(b []byte) (int, error) {
+       c := make(chan ReadResult)
+       go func() {
+               n, err := os.Stdin.Read(b)
+               c <- ReadResult{n, err}
+       }()
+       select {
+       case res := <-c:
+               return res.n, res.err
+       case <-conn.eof:
+               return 0, io.EOF
+       }
+}
 
 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
 
@@ -62,6 +84,12 @@ func (conn *UCSPIConn) SetDeadline(t time.Time) error {
 }
 
 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
+       // An ugly hack to forcefully terminate pending read.
+       // net/http calls SetReadDeadline(aLongTimeAgo), but file
+       // descriptors are not capable to exit immediately that way.
+       if t.Equal(aLongTimeAgo) {
+               conn.eof <- struct{}{}
+       }
        return os.Stdin.SetReadDeadline(t)
 }
 
@@ -82,7 +110,8 @@ func (ucspi *UCSPI) Accept() (net.Conn, error) {
                return nil, UCSPIAlreadyAccepted{}
        }
        ucspi.accepted = true
-       return &UCSPIConn{}, nil
+       conn := UCSPIConn{eof: make(chan struct{}, 1)}
+       return &conn, nil
 }
 
 func (ucspi *UCSPI) Close() error {