Skip to content

Commit

Permalink
fix too many files (#103)
Browse files Browse the repository at this point in the history
* fix: too many open files (#101)

* chore: add auto sync robot in workflow (#102)
  • Loading branch information
nianiaJR authored May 5, 2022
1 parent eca5d26 commit 4cab578
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/auto-cherry-pick.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Auto Cherry Pick
on: workflow_dispatch

defaults:
run:
shell: bash

jobs:
auto-cherry-pick:
runs-on: [self-hosted, nebula-fast]
container:
image: reg.vesoft-inc.com/dashboard/dashboard-dev:centos7
credentials:
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
steps:
- name: auto cherry pick
uses: xigongdaEricyang/cherry-pick-robot@with-python
with:
from_repo: vesoft-inc/nebula-http-gateway
pr_label: ^v[0-9]*\.[0-9]*-cherry-pick$
10 changes: 8 additions & 2 deletions ccore/nebula/gateway/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,14 @@ func Connect(address string, port int, username string, password string, opts ..
return info, nil
}

func Disconnect(nsid string) {
pool.Close(nsid)
func Disconnect(nsid string) error {
client, err := pool.GetClient(nsid)
if err != nil {
return err
}
client.CloseChannel <- true

return nil
}

/*
Expand Down
51 changes: 31 additions & 20 deletions ccore/nebula/gateway/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const (
Unknown = -1
Param = 1
Params = 2
clientRecycleNum = 30
clientMaxNum = 200
SessionExpiredDuration int64 = 3600
)

type Account struct {
Expand Down Expand Up @@ -213,6 +216,14 @@ func NewClient(address string, port int, username string, password string, opts
clientMux.Lock()
defer clientMux.Unlock()

// TODO: it's better to add a schedule to make it instead
if currentClientNum > clientRecycleNum {
go recycleClients()
if currentClientNum >= clientMaxNum {
return nil, errors.New("There is no idle connection now, please try it later")
}
}

host := strings.Join([]string{address, strconv.Itoa(port)}, ":")
c, err := nebula.NewGraphClient([]string{host}, username, password, opts...)
if err != nil {
Expand All @@ -227,7 +238,7 @@ func NewClient(address string, port int, username string, password string, opts
return nil, err
}

ncid := u.String()
nsid := u.String()
ver := c.Version()

client := &Client{
Expand All @@ -242,22 +253,32 @@ func NewClient(address string, port int, username string, password string, opts
},
timezone: c.GetTimezoneInfo(),
}
clientPool[ncid] = client
clientPool[nsid] = client
currentClientNum++

// Make a goroutine to deal with concurrent requests from each connection
go handleRequest(ncid)
go handleRequest(nsid)

info := &ClientInfo{
ClientID: ncid,
ClientID: nsid,
NebulaVersion: ver,
}
return info, err
}

func handleRequest(ncid string) {
func recycleClients() {
for _, client := range clientPool {
now := time.Now().Unix()
expireAt := client.updateTime + SessionExpiredDuration
if now > expireAt {
client.CloseChannel <- true
}
}
}

func handleRequest(nsid string) {
var err error
client := clientPool[ncid]
client := clientPool[nsid]
for {
select {
case request := <-client.RequestChannel:
Expand Down Expand Up @@ -321,29 +342,19 @@ func handleRequest(ncid string) {
clientMux.Lock()
_ = client.graphClient.Close()
currentClientNum--
delete(clientPool, ncid)
delete(clientPool, nsid)
clientMux.Unlock()
return // Exit loop
}
}
}

func Close(ncid string) {
clientMux.Lock()
defer clientMux.Unlock()

if client, ok := clientPool[ncid]; ok {
_ = client.graphClient.Close()
currentClientNum--
delete(clientPool, ncid)
}
}

func GetClient(ncid string) (*Client, error) {
func GetClient(nsid string) (*Client, error) {
clientMux.Lock()
defer clientMux.Unlock()

if client, ok := clientPool[ncid]; ok {
if client, ok := clientPool[nsid]; ok {
client.updateTime = time.Now().Unix()
return client, nil
}

Expand Down
1 change: 1 addition & 0 deletions common/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func CreateFileWithPerm(filePath string, permCode string) (*os.File, error) {
filedir := path.Dir(filePath)
os.MkdirAll(filedir, os.FileMode(perm))
fd, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm))
defer fd.Close()
if os.IsExist(err) {
os.Chmod(filePath, os.FileMode(perm))
}
Expand Down
19 changes: 14 additions & 5 deletions controllers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (this *DatabaseController) Connect() {
json.Unmarshal(this.Ctx.Input.RequestBody, &params)

info, err := dao.Connect(params.Address, params.Port, params.Username, params.Password)
nsid := info.ClientID
if err == nil {
nsid := info.ClientID
res.Code = 0
m := make(map[string]types.Any)
m["nsid"] = nsid
Expand Down Expand Up @@ -81,11 +81,20 @@ func (this *DatabaseController) Home() {
func (this *DatabaseController) Disconnect() {
var res Response
nsid := this.GetSession(beego.AppConfig.String("sessionkey"))
if nsid != nil {
dao.Disconnect(nsid.(string))
if nsid == nil {
res.Code = -1
res.Message = "No connection existed"
} else {
err := dao.Disconnect(nsid.(string))
if err != nil {
res.Code = -1
res.Message = "Disconnect failed"
} else {
res.Code = 0
res.Message = "Disconnect successfully"
}
}
res.Code = 0
res.Message = "Disconnect successfully"

this.Data["json"] = &res
this.ServeJSON()
}
Expand Down

0 comments on commit 4cab578

Please sign in to comment.