move mux to separate repo
This commit is contained in:
1 parent
6e2d485b90
commit
11f91b67a8
6 files changed
+11
-374
No files matched your search
Generated
+10
-3
@@ -277,10 +277,9 @@
|
||||
version = "v2.0.2"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:d30832edf4ae7a45e863e216c89bb5f1d66c10ff5b3255b9e007598a048594d7"
|
||||
digest = "1:b6f5c19baa132c1bb51fa07430738948910b516326dc923febb8ba6e24b0f7dc"
|
||||
name = "github.com/integration-system/isp-lib"
|
||||
packages = [
|
||||
"atomic",
|
||||
"backend",
|
||||
"bootstrap",
|
||||
"config",
|
||||
@@ -328,6 +327,14 @@
|
||||
pruneopts = "UT"
|
||||
revision = "5475a8b08e62e41fa886641861ac225e7f91e4e5"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:965f8051786df58d7d62c8ffa2d90cb84000aab1a167e5c4713ff572cd7e164e"
|
||||
name = "github.com/integration-system/net-mux"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "9dc223fe46d293b528c8e2a04c43d991bc23a2de"
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:01ed62f8f4f574d8aff1d88caee113700a2b44c42351943fa73cc1808f736a50"
|
||||
name = "github.com/jinzhu/inflection"
|
||||
@@ -756,7 +763,6 @@
|
||||
"github.com/integration-system/isp-lib-test/docker",
|
||||
"github.com/integration-system/isp-lib-test/utils",
|
||||
"github.com/integration-system/isp-lib-test/utils/postgres",
|
||||
"github.com/integration-system/isp-lib/atomic",
|
||||
"github.com/integration-system/isp-lib/backend",
|
||||
"github.com/integration-system/isp-lib/bootstrap",
|
||||
"github.com/integration-system/isp-lib/config",
|
||||
@@ -766,6 +772,7 @@
|
||||
"github.com/integration-system/isp-lib/utils",
|
||||
"github.com/integration-system/isp-log",
|
||||
"github.com/integration-system/isp-log/stdcodes",
|
||||
"github.com/integration-system/net-mux",
|
||||
"github.com/json-iterator/go",
|
||||
"github.com/mohae/deepcopy",
|
||||
"github.com/pkg/errors",
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
mux "github.com/integration-system/net-mux"
|
||||
"github.com/thecodeteam/goodbye"
|
||||
"isp-config-service/cluster"
|
||||
"isp-config-service/codes"
|
||||
@@ -18,7 +19,6 @@ import (
|
||||
"isp-config-service/helper"
|
||||
"isp-config-service/holder"
|
||||
"isp-config-service/model"
|
||||
"isp-config-service/mux"
|
||||
"isp-config-service/raft"
|
||||
"isp-config-service/service"
|
||||
"isp-config-service/store"
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type Matcher func([]byte) bool
|
||||
|
||||
// Any is a Matcher that matches any connection.
|
||||
func Any() Matcher {
|
||||
return func(_ []byte) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
var httpMethods = map[string]struct{}{
|
||||
"OPTIONS": {},
|
||||
"HEAD": {},
|
||||
"GET": {},
|
||||
"POST": {},
|
||||
"PUT": {},
|
||||
"PATCH": {},
|
||||
"DELETE": {},
|
||||
"TRACE": {},
|
||||
"CONNECT": {},
|
||||
}
|
||||
|
||||
// HTTP1 only matches the methods in the HTTP request.
|
||||
func HTTP1() Matcher {
|
||||
return func(data []byte) bool {
|
||||
// max length of method
|
||||
l := 7
|
||||
if len(data) < l {
|
||||
l = len(data)
|
||||
}
|
||||
methodB := data[:l]
|
||||
words := bytes.Split(methodB, []byte(" "))
|
||||
if len(words) == 0 {
|
||||
return false
|
||||
}
|
||||
_, ok := httpMethods[string(words[0])]
|
||||
return ok
|
||||
}
|
||||
}
|
||||
-186
@@ -1,186 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/integration-system/isp-lib/atomic"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrListenerClosed = errors.New("mux: listener closed")
|
||||
ErrNotMatched = errors.New("mux: connection not matched by a matcher")
|
||||
)
|
||||
|
||||
type Mux interface {
|
||||
Match(Matcher) net.Listener
|
||||
Serve() error
|
||||
OnError(func(error))
|
||||
Close() error
|
||||
}
|
||||
|
||||
type matchersListener struct {
|
||||
matcher Matcher
|
||||
listener *muxListener
|
||||
}
|
||||
|
||||
type mux struct {
|
||||
listener net.Listener
|
||||
ml []matchersListener
|
||||
connBufLen int
|
||||
doneCh chan struct{}
|
||||
errHandler func(error)
|
||||
}
|
||||
|
||||
func (m *mux) Match(matcher Matcher) net.Listener {
|
||||
ml := newMuxListener(m.listener, m.connBufLen)
|
||||
m.ml = append(m.ml, matchersListener{matcher: matcher, listener: ml})
|
||||
return ml
|
||||
}
|
||||
|
||||
func (m *mux) Serve() error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
defer func() {
|
||||
close(m.doneCh)
|
||||
wg.Wait()
|
||||
|
||||
for _, sl := range m.ml {
|
||||
_ = sl.listener.Close()
|
||||
for c := range sl.listener.connCh {
|
||||
_ = c.Close()
|
||||
}
|
||||
close(sl.listener.connCh)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
c, err := m.listener.Accept()
|
||||
if err != nil {
|
||||
if ne, ok := err.(net.Error); ok && ne.Temporary() {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go m.serve(c, &wg)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mux) Close() error {
|
||||
return m.listener.Close()
|
||||
}
|
||||
|
||||
func (m *mux) OnError(f func(error)) {
|
||||
m.errHandler = f
|
||||
}
|
||||
|
||||
func (m *mux) serve(c net.Conn, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
buf := getBuffer()
|
||||
n, err := c.Read(buf)
|
||||
data := buf[:n]
|
||||
for _, matcher := range m.ml {
|
||||
matched := matcher.matcher(data)
|
||||
if matched {
|
||||
if matcher.listener.closed.Get() {
|
||||
break
|
||||
}
|
||||
muc := newMuxConn(c, data, err)
|
||||
select {
|
||||
case matcher.listener.connCh <- muc:
|
||||
case <-m.doneCh:
|
||||
putBuffer(buf)
|
||||
_ = c.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
putBuffer(buf)
|
||||
_ = c.Close()
|
||||
if m.errHandler != nil {
|
||||
m.errHandler(ErrNotMatched)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mux) handleErr(err error) bool {
|
||||
if ne, ok := err.(net.Error); ok {
|
||||
return ne.Temporary()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func New(l net.Listener) Mux {
|
||||
return &mux{
|
||||
listener: l,
|
||||
connBufLen: 1024,
|
||||
doneCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
type muxListener struct {
|
||||
root net.Listener
|
||||
connCh chan net.Conn
|
||||
closed *atomic.AtomicBool
|
||||
}
|
||||
|
||||
func (l *muxListener) Accept() (net.Conn, error) {
|
||||
if l.closed.Get() {
|
||||
return nil, ErrListenerClosed
|
||||
}
|
||||
c, ok := <-l.connCh
|
||||
if !ok || l.closed.Get() {
|
||||
return nil, ErrListenerClosed
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Close stops listening on address.
|
||||
// Already Accepted connections are not closed.
|
||||
func (l *muxListener) Close() error {
|
||||
l.closed.Set(true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *muxListener) Addr() net.Addr {
|
||||
return l.root.Addr()
|
||||
}
|
||||
|
||||
func newMuxListener(root net.Listener, connBufLen int) *muxListener {
|
||||
return &muxListener{
|
||||
root: root,
|
||||
connCh: make(chan net.Conn, connBufLen),
|
||||
closed: atomic.NewAtomicBool(false),
|
||||
}
|
||||
}
|
||||
|
||||
type muxConn struct {
|
||||
net.Conn
|
||||
buf []byte
|
||||
lastErr error
|
||||
}
|
||||
|
||||
func newMuxConn(c net.Conn, buf []byte, lastErr error) *muxConn {
|
||||
return &muxConn{
|
||||
Conn: c,
|
||||
buf: buf,
|
||||
lastErr: lastErr,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *muxConn) Read(p []byte) (int, error) {
|
||||
if m.buf != nil {
|
||||
n := copy(p, m.buf)
|
||||
if len(m.buf) == n {
|
||||
putBuffer(m.buf)
|
||||
m.buf = nil
|
||||
} else {
|
||||
m.buf = m.buf[n:]
|
||||
}
|
||||
return n, m.lastErr
|
||||
}
|
||||
return m.Conn.Read(p)
|
||||
}
|
||||
-118
@@ -1,118 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHTTP1(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
path := "/test/"
|
||||
method := "GET"
|
||||
mockResData := []byte("948 res")
|
||||
var responseData []byte
|
||||
|
||||
rootListener := newLocalListener()
|
||||
mux := New(rootListener)
|
||||
httpListener := mux.Match(HTTP1())
|
||||
mux.OnError(func(err error) {
|
||||
a.Fail("connection not matched by a http matcher")
|
||||
})
|
||||
|
||||
go func() {
|
||||
err := mux.Serve()
|
||||
log.Println(err)
|
||||
}()
|
||||
|
||||
handler := func(writer http.ResponseWriter, request *http.Request) {
|
||||
var err error
|
||||
a.Equal(method, request.Method)
|
||||
n, err := writer.Write(mockResData)
|
||||
a.NoError(err)
|
||||
a.Len(mockResData, n)
|
||||
}
|
||||
httpMux := http.NewServeMux()
|
||||
httpMux.HandleFunc(path, handler)
|
||||
httpServer := httptest.NewUnstartedServer(httpMux)
|
||||
httpServer.Listener = httpListener
|
||||
httpServer.Start()
|
||||
|
||||
url := httpServer.URL + path
|
||||
httpClient := httpServer.Client()
|
||||
req, _ := http.NewRequest(method, url, nil)
|
||||
res, err := httpClient.Do(req)
|
||||
a.NoError(err)
|
||||
if a.NotNil(res) {
|
||||
a.Equal(200, res.StatusCode)
|
||||
responseData, err = ioutil.ReadAll(res.Body)
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
a.Equal(mockResData, responseData)
|
||||
}
|
||||
|
||||
func TestAny(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
mockReqData := []byte("1 somre")
|
||||
requestData := make([]byte, len(mockReqData))
|
||||
|
||||
rootListener := newLocalListener()
|
||||
mux := New(rootListener)
|
||||
httpListener := mux.Match(HTTP1())
|
||||
tcpListener := mux.Match(Any())
|
||||
go func() {
|
||||
err := mux.Serve()
|
||||
log.Println(err)
|
||||
}()
|
||||
|
||||
handler := func(writer http.ResponseWriter, request *http.Request) {
|
||||
a.Fail("matched http handler")
|
||||
}
|
||||
httpMux := http.NewServeMux()
|
||||
httpMux.HandleFunc("/", handler)
|
||||
httpServer := httptest.NewUnstartedServer(httpMux)
|
||||
httpServer.Listener = httpListener
|
||||
httpServer.Start()
|
||||
|
||||
hadTcpConn := false
|
||||
go func() {
|
||||
for {
|
||||
conn, err := tcpListener.Accept()
|
||||
if hadTcpConn {
|
||||
a.Fail("more than one tcp conn")
|
||||
return
|
||||
}
|
||||
hadTcpConn = true
|
||||
a.NoError(err)
|
||||
_, err = conn.Read(requestData)
|
||||
a.NoError(err)
|
||||
a.Equal(mockReqData, requestData)
|
||||
}
|
||||
}()
|
||||
|
||||
tcpAddr := tcpListener.Addr().String()
|
||||
conn, err := net.Dial("tcp", tcpAddr)
|
||||
a.NoError(err)
|
||||
n, err := conn.Write(mockReqData)
|
||||
a.Len(mockReqData, n)
|
||||
a.NoError(err)
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
a.Equal(mockReqData, requestData)
|
||||
a.True(hadTcpConn)
|
||||
}
|
||||
|
||||
func newLocalListener() net.Listener {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to listen on a port: %v", err))
|
||||
}
|
||||
return l
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// could be increased if needed for new matchers
|
||||
const bufferSize = 10
|
||||
|
||||
var pool sync.Pool
|
||||
|
||||
func getBuffer() []byte {
|
||||
b, ok := pool.Get().([]byte)
|
||||
if !ok {
|
||||
b = make([]byte, bufferSize)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func putBuffer(b []byte) {
|
||||
pool.Put(b[:cap(b)])
|
||||
}
|
||||
Reference in new issue
Block a user