-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11bcf0b
commit e07b86d
Showing
12 changed files
with
194 additions
and
298 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Go package | ||
|
||
on: [ push ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
# action文档 | ||
# https://github.com/marketplace/actions/start-mariadb | ||
- uses: getong/[email protected] | ||
with: | ||
host port: 3306 # Optional, default value is 3306. The port of host | ||
container port: 3306 # Optional, default value is 3306. The port of container | ||
character set server: 'utf8mb4' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld | ||
collation server: 'utf8mb4_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld | ||
# mariadb version: '10.4.10' # Optional, default value is "latest". The version of the MariaDB | ||
mysql database: 'storage_lock_test' # Optional, default value is "test". The specified database which will be create | ||
# 这里就直接写死了 | ||
mysql root password: "UeGqAm8CxYGldMDLoNNt" # Required if "mysql user" is empty, default is empty. The root superuser password | ||
# mysql user: 'developer' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too | ||
# mysql password: ${{ secrets.DatabasePassword }} # Required if "mysql user" exists. The password for the "mysql user" | ||
|
||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.19' | ||
|
||
- name: Test | ||
run: go test -v ./... | ||
env: | ||
STORAGE_LOCK_MARIA_DSN: "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test" |
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 |
---|---|---|
@@ -1,7 +1,16 @@ | ||
# MariaDB Locks | ||
|
||
# 一、这是什么 | ||
|
||
基于MariaDB 实现的分布式锁。 | ||
|
||
# 二、安装依赖 | ||
|
||
```bash | ||
go get -u github.com/storage-lock/go-mariadb-locks | ||
``` | ||
|
||
# 三、API示例 | ||
|
||
TODO 2023-9-24 03:40:09 | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
package mysql_locks | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
mariadb_storage "github.com/storage-lock/go-mariadb-storage" | ||
mysql_storage "github.com/storage-lock/go-mysql-storage" | ||
storage_lock "github.com/storage-lock/go-storage-lock" | ||
storage_lock_factory "github.com/storage-lock/go-storage-lock-factory" | ||
) | ||
|
||
var dsnStorageLockFactoryBeanFactory *storage_lock_factory.StorageLockFactoryBeanFactory[string, *sql.DB] = storage_lock_factory.NewStorageLockFactoryBeanFactory[string, *sql.DB]() | ||
|
||
func NewMariadbLockByDsn(ctx context.Context, dsn string, lockId string) (*storage_lock.StorageLock, error) { | ||
factory, err := GetMariadbLockFactoryByDsn(ctx, dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return factory.CreateLock(lockId) | ||
} | ||
|
||
func NewMariadbLockByDsnWithOptions(ctx context.Context, uri string, options *storage_lock.StorageLockOptions) (*storage_lock.StorageLock, error) { | ||
factory, err := GetMariadbLockFactoryByDsn(ctx, uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return factory.CreateLockWithOptions(options) | ||
} | ||
|
||
func GetMariadbLockFactoryByDsn(ctx context.Context, dsn string) (*storage_lock_factory.StorageLockFactory[*sql.DB], error) { | ||
return dsnStorageLockFactoryBeanFactory.GetOrInit(ctx, dsn, func(ctx context.Context) (*storage_lock_factory.StorageLockFactory[*sql.DB], error) { | ||
connectionManager := mariadb_storage.NewMariadbConnectionManagerFromDsn(dsn) | ||
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager) | ||
storage, err := mysql_storage.NewMysqlStorage(ctx, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
factory := storage_lock_factory.NewStorageLockFactory(storage, options.ConnectionManager) | ||
return factory, 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,21 @@ | ||
package mysql_locks | ||
|
||
import ( | ||
"context" | ||
storage_lock_test_helper "github.com/storage-lock/go-storage-lock-test-helper" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNewMysqlLockByDsn(t *testing.T) { | ||
mysqlDsn := os.Getenv("STORAGE_LOCK_MARIA_DSN") | ||
assert.NotEmpty(t, mysqlDsn) | ||
|
||
factory, err := GetMariadbLockFactoryByDsn(context.Background(), mysqlDsn) | ||
assert.Nil(t, err) | ||
|
||
storage_lock_test_helper.PlayerNum = 10 | ||
storage_lock_test_helper.EveryOnePlayTimes = 100 | ||
storage_lock_test_helper.TestStorageLock(t, factory) | ||
} |
Oops, something went wrong.