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

feat: add NewAdapterWithPool() #37

Merged
merged 3 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: add pool adapter
  • Loading branch information
marcozac committed Dec 3, 2022
commit 6ba5713a47f0a3a80da112de3db370f318acc731
11 changes: 11 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ func NewAdapterWithKey(network string, address string, key string) (*Adapter, er
return newAdapter(network, address, key, "", "")
}

// NewAdapterWithPool is the constructor for Adapter.
func NewAdapterWithPool(pool *redis.Pool) (*Adapter, error) {
hsluoyz marked this conversation as resolved.
Show resolved Hide resolved
a := &Adapter{}
a.conn = pool.Get()

// Call the destructor when the object is released.
runtime.SetFinalizer(a, finalizer)

return a, nil
}

type Option func(*Adapter)

func NewAdpaterWithOption(options ...Option) (*Adapter, error) {
Expand Down
22 changes: 21 additions & 1 deletion adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/util"
"github.com/gomodule/redigo/redis"
)

func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
Expand Down Expand Up @@ -356,7 +357,7 @@ func TestAdapters(t *testing.T) {
a, _ := NewAdapter("tcp", "127.0.0.1:6379")

// Use the following if Redis has password like "123"
//a, err := NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")
// a, err := NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")

// Use the following if you use Redis with a account
// a, err := NewAdapterWithUser("tcp", "127.0.0.1:6379", "testaccount", "userpass")
Expand All @@ -369,3 +370,22 @@ func TestAdapters(t *testing.T) {
testUpdatePolicies(t, a)
testUpdateFilteredPolicies(t, a)
}

func TestPoolAdapters(t *testing.T) {
a, err := NewAdapterWithPool(&redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "127.0.0.1:6379")
},
})
if err != nil {
t.Fatal(err)
}

testSaveLoad(t, a)
testAutoSave(t, a)
testFilteredPolicy(t, a)
testAddPolicies(t, a)
testRemovePolicies(t, a)
testUpdatePolicies(t, a)
testUpdateFilteredPolicies(t, a)
}