Skip to content

Commit

Permalink
add session lock (#162)
Browse files Browse the repository at this point in the history
* add session lock

* fix workflow
  • Loading branch information
HarrisChu authored Dec 2, 2021
1 parent 8e32d1b commit 804d6d3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on:
pull_request:
branches: [ master ]

concurrency:
cancel-in-progress: true

jobs:
go-client:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -62,4 +59,4 @@ jobs:
- name: down
if: always()
run: |
make down
make down
8 changes: 8 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package nebula_go

import (
"fmt"
"sync"

"github.com/facebook/fbthrift/thrift/lib/go/thrift"
"github.com/vesoft-inc/nebula-go/v2/nebula"
Expand All @@ -26,11 +27,14 @@ type Session struct {
connection *connection
connPool *ConnectionPool
log Logger
mu sync.Mutex
timezoneInfo
}

// Execute returns the result of the given query as a ResultSet
func (session *Session) Execute(stmt string) (*ResultSet, error) {
session.mu.Lock()
defer session.mu.Unlock()
if session.connection == nil {
return nil, fmt.Errorf("failed to execute: Session has been released")
}
Expand Down Expand Up @@ -130,6 +134,8 @@ func (session *Session) Execute(stmt string) (*ResultSet, error) {
// ]
// }
func (session *Session) ExecuteJson(stmt string) ([]byte, error) {
session.mu.Lock()
defer session.mu.Unlock()
if session.connection == nil {
return nil, fmt.Errorf("failed to execute: Session has been released")
}
Expand Down Expand Up @@ -182,6 +188,8 @@ func (session *Session) Release() {
if session == nil {
return
}
session.mu.Lock()
defer session.mu.Unlock()
if session.connection == nil {
session.log.Warn("Session has been released")
return
Expand Down
47 changes: 47 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

package nebula_go

import (
"testing"
"time"
)

func TestSession_Execute(t *testing.T) {
config := PoolConfig{}
host := HostAddress{address, port}
pool, err := NewConnectionPool([]HostAddress{host}, config, DefaultLogger{})
if err != nil {
t.Fatal(err)
}

sess, err := pool.GetSession("root", "nebula")
if err != nil {
t.Fatal(err)
}

f := func(s *Session) {
time.Sleep(10 * time.Microsecond)
reps, err := s.Execute("yield 1")
if err != nil {
t.Fatal(err)
}
if !reps.IsSucceed() {
t.Fatal(reps.resp.ErrorMsg)
}
}
go func() {
for {
f(sess)
}
}()
go func() {
for {
f(sess)
}
}()
time.Sleep(300 * time.Millisecond)
}

0 comments on commit 804d6d3

Please sign in to comment.