From 6ea4d5423ec041b73c78bcd010081be00608c954 Mon Sep 17 00:00:00 2001 From: qydysky Date: Wed, 19 Mar 2025 23:00:33 +0800 Subject: [PATCH] 1 (#35) --- web/Web.go | 41 ++++++----------------------------- web/Web_test.go | 57 ++----------------------------------------------- 2 files changed, 8 insertions(+), 90 deletions(-) diff --git a/web/Web.go b/web/Web.go index 87c6b2e..96b3289 100644 --- a/web/Web.go +++ b/web/Web.go @@ -6,7 +6,6 @@ import ( "errors" "net" "net/http" - "os" "strconv" "strings" "sync" @@ -54,28 +53,19 @@ func (t *Web) Shutdown(ctx ...context.Context) { type WebSync struct { Server *http.Server - FD uintptr mux *http.ServeMux wrs *WebPath } func NewSyncMap(conf *http.Server, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync) { - if o, e := NewSyncMapNoPanic(conf, 0, m, matchFunc...); e != nil { + if o, e := NewSyncMapNoPanic(conf, m, matchFunc...); e != nil { panic(e) } else { return o } } -func NewSyncMapFD(conf *http.Server, fd uintptr, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync) { - if o, e := NewSyncMapNoPanic(conf, fd, m, matchFunc...); e != nil { - panic(e) - } else { - return o - } -} - -func NewSyncMapNoPanic(conf *http.Server, fd uintptr, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync, err error) { +func NewSyncMapNoPanic(conf *http.Server, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync, err error) { o = new(WebSync) @@ -88,34 +78,15 @@ func NewSyncMapNoPanic(conf *http.Server, fd uintptr, m *WebPath, matchFunc ...f matchFunc = append(matchFunc, o.wrs.Load) var ln net.Listener - if fd != 0 { - if tmp, err := net.FileListener(os.NewFile(fd, "")); err != nil { - return nil, err - } else { - ln = tmp - } + if tmp, err := net.Listen("tcp", conf.Addr); err != nil { + return nil, err } else { - if tmp, err := net.Listen("tcp", conf.Addr); err != nil { - return nil, err - } else { - ln = tmp - } + ln = tmp } if conf.TLSConfig != nil { ln = tls.NewListener(ln, conf.TLSConfig) } - switch t := ln.(type) { - case *net.TCPListener: - c, _ := t.SyscallConn() - c.Control(func(fd uintptr) { - o.FD = fd - }) - case *net.UnixListener: - c, _ := t.SyscallConn() - c.Control(func(fd uintptr) { - o.FD = fd - }) - } + go o.Server.Serve(ln) if o.Server.Handler == nil { diff --git a/web/Web_test.go b/web/Web_test.go index 5840f61..634cc11 100644 --- a/web/Web_test.go +++ b/web/Web_test.go @@ -17,66 +17,13 @@ import ( reqf "github.com/qydysky/part/reqf" ) -func TestMain1(t *testing.T) { - var fd uintptr - r := reqf.New() - var stop func(ctx ...context.Context) - var stop2 func(ctx ...context.Context) - { - ser := &http.Server{Addr: "127.0.0.1:18081"} - wp := &WebPath{} - wp.Store("/test/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("1")) - }) - o, e := NewSyncMapNoPanic(ser, 0, wp, wp.Load) - if e != nil { - t.Fatal(e) - } - fd = o.FD - stop = o.Shutdown - } - time.Sleep(time.Second) - { - r.Reqf(reqf.Rval{ - Url: "http://127.0.0.1:18081/test/", - }) - if !bytes.Equal(r.Respon, []byte("1")) { - t.Fatal(r.Respon) - } - } - { - ser := &http.Server{Addr: "127.0.0.1:18082"} - wp := &WebPath{} - wp.Store("/test/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("2")) - }) - o, e := NewSyncMapNoPanic(ser, fd, wp, wp.Load) - if e != nil { - t.Fatal(e) - } - fd = o.FD - stop2 = o.Shutdown - } - stop() - time.Sleep(time.Second) - { - r.Reqf(reqf.Rval{ - Url: "http://127.0.0.1:18081/test/", - }) - if !bytes.Equal(r.Respon, []byte("2")) { - t.Fatal(r.Respon) - } - } - stop2() -} - func TestMain(t *testing.T) { ser := &http.Server{ Addr: "127.0.0.1:18081", } wp := &WebPath{} - t.Log(NewSyncMapNoPanic(ser, 0, wp, wp.Load)) - t.Log(NewSyncMapNoPanic(ser, 0, wp, wp.Load)) + t.Log(NewSyncMapNoPanic(ser, wp, wp.Load)) + t.Log(NewSyncMapNoPanic(ser, wp, wp.Load)) } func Test_Exprier(t *testing.T) { -- 2.39.2