Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Sep 23, 2023
1 parent 11bcf0b commit e07b86d
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 298 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/golang.yaml
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"
9 changes: 9 additions & 0 deletions README.md
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

108 changes: 0 additions & 108 deletions examples/quick_start/main.go

This file was deleted.

122 changes: 0 additions & 122 deletions examples/with_detailed_setting/main.go

This file was deleted.

29 changes: 0 additions & 29 deletions global.go

This file was deleted.

41 changes: 41 additions & 0 deletions global_dsn.go
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
})
}
21 changes: 21 additions & 0 deletions global_dsn_test.go
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)
}
Loading

0 comments on commit e07b86d

Please sign in to comment.