diff options
author | Valery Piashchynski <[email protected]> | 2020-11-25 12:33:50 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-25 12:33:50 +0300 |
commit | 2918cfca6d9579125257bfc9f5655537a63ec82a (patch) | |
tree | f0f4221eca331a27032d923b512df2a567c4ebe6 | |
parent | 4f54d3a9d4b9ac65ad47509372f63653b7b973a0 (diff) |
Update CreateListener function, simplifications
-rw-r--r-- | plugins/http/plugin.go | 12 | ||||
-rw-r--r-- | plugins/http/tests/plugin_test_old.go | 40 | ||||
-rwxr-xr-x | util/network.go | 22 |
3 files changed, 22 insertions, 52 deletions
diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go index b37a9700..e58f9359 100644 --- a/plugins/http/plugin.go +++ b/plugins/http/plugin.go @@ -268,12 +268,18 @@ func (s *Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) { } r = attributes.Init(r) + // protect the case, when user send Reset and we are replacing handler with pool + s.Lock() + f := s.handler.ServeHTTP + s.Unlock() // chaining middleware - f := s.handler.ServeHTTP - for _, m := range s.mdwr { - f = m(f) + if len(s.mdwr) > 0 { + for i := 0; i < len(s.mdwr); i++ { + f = s.mdwr[i](f) + } } + f(w, r) } diff --git a/plugins/http/tests/plugin_test_old.go b/plugins/http/tests/plugin_test_old.go index 852e5545..1ef7002e 100644 --- a/plugins/http/tests/plugin_test_old.go +++ b/plugins/http/tests/plugin_test_old.go @@ -24,45 +24,7 @@ package tests // target string //} // -//func (cfg *testCfg) Get(name string) service.Config { -// if name == ID { -// if cfg.httpCfg == "" { -// return nil -// } -// -// return &testCfg{target: cfg.httpCfg} -// } -// -// if name == rpc.ID { -// return &testCfg{target: cfg.rpcCfg} -// } -// -// if name == env.ID { -// return &testCfg{target: cfg.envCfg} -// } -// -// return nil -//} -//func (cfg *testCfg) Unmarshal(out interface{}) error { -// j := json.ConfigCompatibleWithStandardLibrary -// return j.Unmarshal([]byte(cfg.target), out) -//} -// -//func Test_Service_NoConfig(t *testing.T) { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{"Enable":true}`}) -// assert.Error(t, err) -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusInactive, st) -//} -// + //func Test_Service_Configure_Disable(t *testing.T) { // logger, _ := test.NewNullLogger() // logger.SetLevel(logrus.DebugLevel) diff --git a/util/network.go b/util/network.go index f35d842b..c2475f4a 100755 --- a/util/network.go +++ b/util/network.go @@ -24,13 +24,19 @@ func CreateListener(address string) (net.Listener, error) { return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") } - if dsn[0] == "unix" && fileExists(dsn[1]) { - err := syscall.Unlink(dsn[1]) - if err != nil { - return nil, fmt.Errorf("error during the unlink syscall: error %v", err) + // create unix listener + if dsn[0] == "unix" { + // check if the file exist + if fileExists(dsn[1]) { + err := syscall.Unlink(dsn[1]) + if err != nil { + return nil, fmt.Errorf("error during the unlink syscall: error %v", err) + } } + return net.Listen(dsn[0], dsn[1]) } + // configure and create tcp4 listener cfg := tcplisten.Config{ ReusePort: true, DeferAccept: true, @@ -38,12 +44,8 @@ func CreateListener(address string) (net.Listener, error) { Backlog: 0, } - // tcp4 is currently supported - if dsn[0] == "tcp" { - return cfg.NewListener("tcp4", dsn[1]) - } - - return net.Listen(dsn[0], dsn[1]) + // only tcp4 is currently supported + return cfg.NewListener("tcp4", dsn[1]) } // fileExists checks if a file exists and is not a directory before we |