-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: husharp <[email protected]>
- Loading branch information
Showing
8 changed files
with
214 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2024 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. | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package alloc | ||
|
||
func environmentCheck(_ string) bool { | ||
return true | ||
} |
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,42 @@ | ||
// Copyright 2024 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. | ||
//go:build linux | ||
// +build linux | ||
|
||
package alloc | ||
|
||
import ( | ||
"github.com/cakturk/go-netstat/netstat" | ||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func environmentCheck(addr string) bool { | ||
valid, err := checkAddr(addr[len("http://"):]) | ||
if err != nil { | ||
log.Error("check port status failed", zap.Error(err)) | ||
return false | ||
} | ||
return valid | ||
} | ||
|
||
func checkAddr(addr string) (bool, error) { | ||
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool { | ||
return s.RemoteAddr.String() == addr || s.LocalAddr.String() == addr | ||
}) | ||
if err != nil { | ||
return false, err | ||
} | ||
return len(tabs) < 1, nil | ||
} |
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,48 @@ | ||
// Copyright 2024 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 alloc | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pingcap/log" | ||
flag "github.com/spf13/pflag" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var statusAddress = flag.String("status-addr", "0.0.0.0:20180", "status address") | ||
|
||
func RunHTTPServer() *http.Server { | ||
gin.SetMode(gin.ReleaseMode) | ||
engine := gin.New() | ||
engine.Use(gin.Recovery()) | ||
|
||
engine.GET("alloc", func(c *gin.Context) { | ||
addr := Alloc() | ||
c.String(http.StatusOK, addr) | ||
}) | ||
|
||
srv := &http.Server{Addr: *statusAddress, Handler: engine.Handler(), ReadHeaderTimeout: 3 * time.Second} | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatal("server listen error", zap.Error(err)) | ||
} | ||
}() | ||
|
||
return srv | ||
} |
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,65 @@ | ||
// Copyright 2024 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 alloc | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pingcap/log" | ||
"github.com/tikv/pd/pkg/errs" | ||
) | ||
|
||
var ( | ||
testAddrMutex sync.Mutex | ||
testAddrMap = make(map[string]struct{}) | ||
) | ||
|
||
// Alloc allocates a local URL for testing. | ||
func Alloc() string { | ||
for i := 0; i < 50; i++ { | ||
if u := tryAllocTestURL(); u != "" { | ||
return u | ||
} | ||
time.Sleep(200 * time.Millisecond) | ||
} | ||
log.Fatal("failed to alloc test URL") | ||
return "" | ||
} | ||
|
||
func tryAllocTestURL() string { | ||
l, err := net.Listen("tcp", "127.0.0.1:") | ||
if err != nil { | ||
return "" | ||
} | ||
addr := fmt.Sprintf("http://%s", l.Addr()) | ||
err = l.Close() | ||
if err != nil { | ||
log.Fatal("close failed", errs.ZapError(err)) | ||
} | ||
|
||
testAddrMutex.Lock() | ||
defer testAddrMutex.Unlock() | ||
if _, ok := testAddrMap[addr]; ok { | ||
return "" | ||
} | ||
if !environmentCheck(addr) { | ||
return "" | ||
} | ||
testAddrMap[addr] = struct{}{} | ||
return addr | ||
} |
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