Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mcs: fix gin race by using a global once #7778

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions pkg/mcs/resourcemanager/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (
"net/http"
"reflect"
"strings"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/pingcap/log"
rmserver "github.com/tikv/pd/pkg/mcs/resourcemanager/server"
Expand All @@ -41,7 +39,6 @@ import (
const APIPathPrefix = "/resource-manager/api/v1/"

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "resource-manager",
Version: "v1",
Expand All @@ -67,11 +64,6 @@ type Service struct {

// NewService returns a new Service.
func NewService(srv *rmserver.Service) *Service {
once.Do(func() {
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
apiHandlerEngine := gin.New()
apiHandlerEngine.Use(gin.Recovery())
apiHandlerEngine.Use(cors.Default())
Expand Down
8 changes: 0 additions & 8 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"net/url"
"strconv"
"strings"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
Expand All @@ -53,7 +51,6 @@ const APIPathPrefix = "/scheduling/api/v1"
const handlerKey = "handler"

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "scheduling",
Version: "v1",
Expand Down Expand Up @@ -94,11 +91,6 @@ func createIndentRender() *render.Render {

// NewService returns a new Service.
func NewService(srv *scheserver.Service) *Service {
once.Do(func() {
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
apiHandlerEngine := gin.New()
apiHandlerEngine.Use(gin.Recovery())
apiHandlerEngine.Use(cors.Default())
Expand Down
8 changes: 0 additions & 8 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"fmt"
"net/http"
"strconv"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
Expand All @@ -44,7 +42,6 @@ const (
)

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "tso",
Version: "v1",
Expand Down Expand Up @@ -77,11 +74,6 @@ func createIndentRender() *render.Render {

// NewService returns a new Service.
func NewService(srv *tsoserver.Service) *Service {
once.Do(func() {
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
apiHandlerEngine := gin.New()
apiHandlerEngine.Use(gin.Recovery())
apiHandlerEngine.Use(cors.Default())
Expand Down
14 changes: 14 additions & 0 deletions pkg/utils/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/pingcap/errcode"
"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -67,6 +70,17 @@ const (
chunkSize = 4096
)

var once sync.Once

func init() {
once.Do(func() {
// See https://github.com/pingcap/tidb-dashboard/blob/f8ecb64e3d63f4ed91c3dca7a04362418ade01d8/pkg/apiserver/apiserver.go#L84
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
}

// DeferClose captures the error returned from closing (if an error occurs).
// This is designed to be used in a defer statement.
func DeferClose(c io.Closer, err *error) {
Expand Down
10 changes: 0 additions & 10 deletions server/apiv2/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ package apiv2
import (
"context"
"net/http"
"sync"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/apiv2/handlers"
"github.com/tikv/pd/server/apiv2/middlewares"
)

var once sync.Once

var group = apiutil.APIServiceGroup{
Name: "core",
IsCore: true,
Expand All @@ -49,12 +45,6 @@ const apiV2Prefix = "/pd/api/v2/"
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /pd/api/v2
func NewV2Handler(_ context.Context, svr *server.Server) (http.Handler, apiutil.APIServiceGroup, error) {
once.Do(func() {
// See https://github.com/pingcap/tidb-dashboard/blob/f8ecb64e3d63f4ed91c3dca7a04362418ade01d8/pkg/apiserver/apiserver.go#L84
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
router := gin.New()
router.Use(func(c *gin.Context) {
c.Set(middlewares.ServerContextKey, svr)
Expand Down
Loading