-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
141 lines (123 loc) · 2.92 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"database/sql"
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"rwlock"
_ "rwlock/driver/mysql"
)
func LogLevel(level string) log.Level {
l, err := log.ParseLevel(string(level))
if err != nil {
l = log.InfoLevel
log.Warnf("error parsing level %q: %v, using %q ", level, err, l)
}
return l
}
func createTable(dbname string) {
force := false //不强制建数据库
verbose := true
err := orm.RunSyncdb("default", force, verbose) //建表
if err != nil {
log.Errorf("err: %s", err)
}
}
func createdb(conn string, dbname string) error {
db, err := sql.Open("mysql", conn)
if err != nil {
log.Errorf("Cannot open database(%s), err: %v", conn, err)
return err
}
usedbSql := fmt.Sprintf("use %s", dbname)
_, err = db.Exec(usedbSql)
if err == nil {
log.Infof("DB(%s) already exists, no need to create it.", dbname)
return err
}
defer db.Close()
dbCreateSql := fmt.Sprintf("CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", dbname)
_, err = db.Exec(dbCreateSql)
if err == nil {
log.Infof("Succeed to create db(%s)", dbname)
}
usedbSql = fmt.Sprintf("use %s", dbname)
_, err = db.Exec(usedbSql)
if err == nil {
log.Infof("DB(%s) already exists, no need to create it.", dbname)
return err
}
return nil
}
func init() {
conn := "root:00010001@tcp(127.0.0.1:3306)/mysql?charset=utf8"
createdb(conn, "lock")
}
func main() {
// Initialize database
orm.RegisterDriver("mysql", orm.DRMySQL)
conn := "root:00010001@tcp(127.0.0.1:3306)/lock?charset=utf8"
orm.RegisterDataBase("default", "mysql", conn)
log.SetLevel(LogLevel("debug"))
o := orm.NewOrm()
if err := o.Using("default"); err != nil {
log.Errorf("err: %s", err)
}
createTable("lock")
// Register driver
rwlock.InitDriver("mysql", 10*time.Second)
/*
lock, err := rwlock.GetRwlocker("aaa")
if err != nil {
log.Errorf("dddd: %s", err)
}
var ok bool
if ok, err = lock.Wlock(); err != nil {
log.Errorf("Get Rlocker error: %s", err)
}
if ok {
log.Debugf("Get Rlock successfuly")
} else {
log.Debugf("Can not Get rlock")
}
*/
//lock.RUnlock()
// if ok, err = lock.Rlock(); err != nil {
// log.Errorf("Get Wlocker error: %s", err)
// }
// if ok {
// log.Debugf("Get Wlock successfuly")
// }
lock1, err1 := rwlock.GetRwlocker("bbb")
if err1 != nil {
log.Errorf("dddd: %s", err1)
}
var err error
var ok bool
if ok, err = lock1.Rlock(); err != nil {
log.Errorf("Get Rlocker error: %s", err)
}
if ok {
log.Debugf("Get Rlock successfuly")
} else {
log.Debugf("Can not Get rlock")
}
go func(lock *rwlock.Rwlocker) {
var ok bool
var err error
if ok, err = lock1.Wlock(); err != nil {
log.Errorf("Get Rlocker error: %s", err)
}
if ok {
log.Debugf("Get Wlock successfuly")
} else {
log.Debugf("Can not Get Wlock")
}
}(lock1)
time.Sleep(3 * time.Second)
lock1.RUnlock()
/*
*/
}