-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resource-manager: supports redirect the http request to primary server (
#6050) close #6051 resource-manager: supports redirect the HTTP request to primary server Signed-off-by: nolouch <nolouch@gmail.com> Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
- Loading branch information
1 parent
002d6ca
commit 10eeab1
Showing
13 changed files
with
230 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package multiservicesapi | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pingcap/log" | ||
bs "github.com/tikv/pd/pkg/basicserver" | ||
"github.com/tikv/pd/pkg/errs" | ||
"github.com/tikv/pd/pkg/utils/apiutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// HTTP headers. | ||
const ( | ||
ServiceAllowDirectHandle = "service-allow-direct-handle" | ||
ServiceRedirectorHeader = "service-redirector" | ||
) | ||
|
||
// ServiceRedirector is a middleware to redirect the request to the right place. | ||
func ServiceRedirector() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
svr := c.MustGet("service").(bs.Server) | ||
allowDirectHandle := len(c.Request.Header.Get(ServiceAllowDirectHandle)) > 0 | ||
isServing := svr.IsServing() | ||
if allowDirectHandle || isServing { | ||
c.Next() | ||
return | ||
} | ||
|
||
// Prevent more than one redirection. | ||
if name := c.Request.Header.Get(ServiceRedirectorHeader); len(name) != 0 { | ||
log.Error("redirect but server is not primary", zap.String("from", name), zap.String("server", svr.Name()), errs.ZapError(errs.ErrRedirect)) | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, errs.ErrRedirect.FastGenByArgs().Error()) | ||
return | ||
} | ||
|
||
c.Request.Header.Set(ServiceRedirectorHeader, svr.Name()) | ||
|
||
primary := svr.GetPrimary() | ||
if primary == nil { | ||
c.AbortWithStatusJSON(http.StatusServiceUnavailable, errs.ErrLeaderNil.FastGenByArgs().Error()) | ||
return | ||
} | ||
clientUrls := primary.GetClientUrls() | ||
urls := make([]url.URL, 0, len(clientUrls)) | ||
for _, item := range clientUrls { | ||
u, err := url.Parse(item) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, errs.ErrURLParse.Wrap(err).GenWithStackByCause().Error()) | ||
return | ||
} | ||
|
||
urls = append(urls, *u) | ||
} | ||
|
||
client := svr.GetHTTPClient() | ||
apiutil.NewCustomReverseProxies(client, urls).ServeHTTP(c.Writer, c.Request) | ||
c.Abort() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.