优化httpserver

This commit is contained in:
boyce
2019-01-28 11:31:17 +08:00
parent 5bac2dc314
commit 0b97b5f68a
4 changed files with 42 additions and 18 deletions

View File

@@ -78,10 +78,10 @@ type CTestData struct {
}
func (slf *CCluster) AcceptRpc(tpcListen *net.TCPListener) error {
slf.reader, slf.writer = net.Pipe()
/*slf.reader, slf.writer = net.Pipe()
go rpc.ServeConn(slf.reader)
slf.LocalRpcClient = rpc.NewClient(slf.writer)
*/
for {
conn, err := tpcListen.Accept()
if err != nil {
@@ -113,6 +113,10 @@ func (slf *CCluster) ListenService() error {
return err2
}
slf.reader, slf.writer = net.Pipe()
go rpc.ServeConn(slf.reader)
slf.LocalRpcClient = rpc.NewClient(slf.writer)
go slf.AcceptRpc(tcplisten)
return nil
}

View File

@@ -3,14 +3,24 @@ package network
import (
"fmt"
"net/http"
"time"
)
type HttpServer struct {
port uint16
handler http.Handler
readtimeout time.Duration
writetimeout time.Duration
httpserver *http.Server
}
func (slf *HttpServer) Init(port uint16) {
func (slf *HttpServer) Init(port uint16, handler http.Handler, readtimeout time.Duration, writetimeout time.Duration) {
slf.port = port
slf.handler = handler
slf.readtimeout = readtimeout
slf.writetimeout = writetimeout
}
func (slf *HttpServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
@@ -23,7 +33,16 @@ func (slf *HttpServer) Start() {
func (slf *HttpServer) startListen() error {
listenPort := fmt.Sprintf(":%d", slf.port)
err := http.ListenAndServe(listenPort, nil)
slf.httpserver = &http.Server{
Addr: listenPort,
Handler: slf.handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
err := slf.httpserver.ListenAndServe()
if err != nil {
fmt.Printf("http.ListenAndServe(%d, nil) error\n", slf.port)
}

View File

@@ -293,7 +293,7 @@ func suitableMethods(prefix string, typ reflect.Type, reportErr bool) map[string
mname := method.Name
if prefix != "" {
if len(mname) < 4 || mname[:4] != prefix {
if strings.Contains(mname, prefix) == false {
continue
}
}

View File

@@ -6,8 +6,11 @@ import (
"net/http"
"reflect"
"strings"
"time"
"github.com/duanhf2012/origin/rpc"
"github.com/gorilla/mux"
"github.com/gotoxu/cors"
"github.com/duanhf2012/origin/cluster"
"github.com/duanhf2012/origin/network"
@@ -33,12 +36,19 @@ type HttpServerService struct {
}
func (slf *HttpServerService) OnInit() error {
slf.httpserver.Init(slf.port)
slf.httpserver.HandleFunc("/{server:[a-zA-Z0-9]+}/{method:[a-zA-Z0-9]+}", func(w http.ResponseWriter, r *http.Request) {
slf.httpserver.Init(slf.port, slf.initRouterHandler(), 10*time.Second, 10*time.Second)
return nil
}
func (slf *HttpServerService) initRouterHandler() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/{server:[a-zA-Z0-9]+}/{method:[a-zA-Z0-9]+}", func(w http.ResponseWriter, r *http.Request) {
slf.httpHandler(w, r)
})
return nil
cors := cors.AllowAll()
//return cors.Handler(gziphandler.GzipHandler(r))
return cors.Handler(r)
}
func (slf *HttpServerService) OnRun() error {
@@ -93,17 +103,8 @@ func (slf *HttpServerService) httpHandler(w http.ResponseWriter, r *http.Request
writeError(http.StatusBadRequest, "rpc: ioutil.ReadAll "+err.Error())
return
}
strCallPath := "_" + vstr[1] + "." + vstr[2]
/*
method, err := slf.GetMethod(strCallPath)
var respone string
if err != nil {
respone = fmt.Sprintf("http respone => %v\n", err)
strCallPath := "_" + vstr[1] + ".HTTP_" + vstr[2]
} else {
cluster.InstanceClusterMgr().Call(NodeServiceMethod, args, reply)
}
*/
request := HttpRequest{string(msg)}
var resp HttpRespone