Skip to content

Commit

Permalink
Revert "close tikv#4373 : add framework"
Browse files Browse the repository at this point in the history
This reverts commit 01fb757.

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
  • Loading branch information
CabinfeverB committed Dec 28, 2021
1 parent 47e6984 commit f4b62fe
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 150 deletions.
4 changes: 3 additions & 1 deletion pkg/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"strconv"

"github.com/gorilla/mux"
"github.com/pingcap/errcode"
"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -136,6 +135,7 @@ func ErrorResp(rd *render.Render, w http.ResponseWriter, err error) {
rd.JSON(w, http.StatusInternalServerError, err.Error())
}
}
<<<<<<< HEAD

<<<<<<< HEAD
// GetComponentNameOnHTTP returns component name from Request Header
Expand Down Expand Up @@ -179,3 +179,5 @@ func GetHTTPRouteName(req *http.Request) (string, bool) {
return "", false
>>>>>>> d6daf973 (close #4490)
}
=======
>>>>>>> 45b95d82 (Revert "close #4373 : add framework")
18 changes: 0 additions & 18 deletions pkg/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,6 @@ func IsServiceAllowed(s *server.Server, group server.ServiceGroup) bool {
return false
}

type selfProtector struct {
s *server.Server
}

// NewSelfProtector handle self-protection
func NewSelfProtector(s *server.Server) negroni.Handler {
return &selfProtector{s: s}
}

func (protector *selfProtector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
handler := protector.s.GetSelfProtectionHandler()
if handler == nil || handler.HandleHTTPSelfProtection(r) {
next(w, r)
} else {
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
}
}

type redirector struct {
s *server.Server
}
Expand Down
1 change: 0 additions & 1 deletion server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewHandler(ctx context.Context, svr *server.Server) (http.Handler, server.S
router.PathPrefix(apiPrefix).Handler(negroni.New(
serverapi.NewRuntimeServiceValidator(svr, group),
serverapi.NewRedirector(svr),
serverapi.NewSelfProtector(svr),
negroni.Wrap(r)),
)

Expand Down
80 changes: 0 additions & 80 deletions server/self_protection.go

This file was deleted.

8 changes: 0 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ type Server struct {
// tsoDispatcher is used to dispatch different TSO requests to
// the corresponding forwarding TSO channel.
tsoDispatcher sync.Map /* Store as map[string]chan *tsoRequest */
// SelfProtectionHandler is used for PD self-pretection
selfProtectionHandler *SelfProtectionHandler
}

// HandlerBuilder builds a server HTTP handler.
Expand Down Expand Up @@ -240,7 +238,6 @@ func CreateServer(ctx context.Context, cfg *config.Config, serviceBuilders ...Ha
}

s.handler = newHandler(s)
s.selfProtectionHandler = NewSelfProtectionHandler(s)

// Adjust etcd config.
etcdCfg, err := s.cfg.GenEmbedEtcdConfig()
Expand Down Expand Up @@ -671,11 +668,6 @@ func (s *Server) stopRaftCluster() {
s.cluster.Stop()
}

// GetAddr returns the server urls for clients.
func (s *Server) GetSelfProtectionHandler() *SelfProtectionHandler {
return s.selfProtectionHandler
}

// GetAddr returns the server urls for clients.
func (s *Server) GetAddr() string {
return s.cfg.AdvertiseClientUrls
Expand Down
42 changes: 0 additions & 42 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ import (
"net/http"
"testing"

"github.com/gorilla/mux"
. "github.com/pingcap/check"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/pkg/assertutil"
"github.com/tikv/pd/pkg/etcdutil"
"github.com/tikv/pd/pkg/testutil"
"github.com/tikv/pd/server/config"
"github.com/urfave/negroni"
"go.etcd.io/etcd/embed"
"go.etcd.io/etcd/pkg/types"
"go.uber.org/goleak"
Expand Down Expand Up @@ -238,42 +235,3 @@ func (s *testServerHandlerSuite) TestRegisterServerHandler(c *C) {
bodyString := string(bodyBytes)
c.Assert(bodyString, Equals, "Hello World\n")
}

func (s *testServerHandlerSuite) TestMuxRouterName(c *C) {
handler := func(ctx context.Context, s *Server) (http.Handler, ServiceGroup, error) {
r := mux.NewRouter()
r.HandleFunc("/pd/apis/mok/v1/router", func(w http.ResponseWriter, r *http.Request) {
RouterName, _ := apiutil.GetHTTPRouteName(r)
fmt.Fprintln(w, RouterName)
}).Name("Mux Router")
info := ServiceGroup{
Name: "mok",
Version: "v1",
}
router := mux.NewRouter()
router.PathPrefix("/pd").Handler(negroni.New(
negroni.Wrap(r)),
)
return router, info, nil
}
cfg := NewTestSingleConfig(checkerWithNilAssert(c))
ctx, cancel := context.WithCancel(context.Background())
svr, err := CreateServer(ctx, cfg, handler)
c.Assert(err, IsNil)
defer func() {
cancel()
svr.Close()
testutil.CleanServer(svr.cfg.DataDir)
}()
err = svr.Run()
c.Assert(err, IsNil)
resp, err := http.Get(fmt.Sprintf("%s/pd/apis/mok/v1/router", svr.GetAddr()))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(err, IsNil)
bodyBytes, err := io.ReadAll(resp.Body)
resp.Body.Close()
c.Assert(err, IsNil)
bodyString := string(bodyBytes)
c.Assert(bodyString, Equals, "Mux Router\n")
}

0 comments on commit f4b62fe

Please sign in to comment.