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

add session lock #162

Merged
merged 2 commits into from
Dec 2, 2021
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
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)
}