Skip to content

Commit 70eab8a

Browse files
committed
dir optimize
Signed-off-by: kl7sn <mex7.0828@gmail.com>
1 parent f7e299e commit 70eab8a

File tree

26 files changed

+307
-189
lines changed

26 files changed

+307
-189
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
.idea/*
1818

19-
logs
19+
logs
20+
test

preempt/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/gotomicro/cetus/preempt
2+
3+
go 1.20

test/syncmap/main.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"time"
7+
)
8+
9+
var ConnectionPool = sync.Pool{
10+
New: func() interface{} {
11+
return &Connection{}
12+
},
13+
}
14+
15+
func GetConn() *Connection {
16+
cli := ConnectionPool.Get().(*Connection)
17+
return cli
18+
}
19+
20+
func PutConn(cli *Connection) {
21+
cli.reset()
22+
ConnectionPool.Put(cli) // 放回连接池
23+
}
24+
25+
var redisOpZRangeCost = time.Millisecond * 1000
26+
var redisOpDeleteCost = time.Millisecond * 100
27+
28+
var clients = sync.Map{}
29+
30+
// Connection Operator structure
31+
type Connection struct {
32+
Id int
33+
Uid int
34+
}
35+
36+
func main() {
37+
go clear()
38+
39+
fmt.Println("....add....", time.Now().Unix())
40+
for j := 1; j < 10000; j++ {
41+
connect(j, j)
42+
}
43+
fmt.Println("....delete....", time.Now().Unix())
44+
45+
go func() {
46+
for j := 1; j < 10000; j++ {
47+
disconnect(get(j))
48+
}
49+
}()
50+
select {}
51+
}
52+
53+
func connect(id, uid int) {
54+
c := GetConn()
55+
c.Id = id
56+
c.Uid = uid
57+
clients.Store(c.Id, c)
58+
}
59+
60+
func disconnect(c *Connection) {
61+
if c == nil {
62+
return
63+
}
64+
clients.Delete(c.Id)
65+
// redis 操作
66+
time.Sleep(redisOpDeleteCost)
67+
PutConn(c)
68+
}
69+
70+
func get(id int) *Connection {
71+
val, ok := clients.Load(id)
72+
if ok {
73+
return val.(*Connection)
74+
}
75+
return nil
76+
}
77+
78+
func clear() {
79+
for {
80+
fmt.Println("....clear....", time.Now().Unix())
81+
82+
clients.Range(func(_, v interface{}) bool {
83+
client := v.(*Connection)
84+
zRangeOp(client.Id, client.Uid)
85+
return true
86+
})
87+
time.Sleep(time.Second * 3)
88+
}
89+
}
90+
91+
func zRangeOp(id, uid int) {
92+
time.Sleep(redisOpZRangeCost)
93+
94+
if uid == 0 {
95+
fmt.Println(id, uid, "..............Fatal Error..................................................", time.Now().Unix())
96+
}
97+
fmt.Println(id, uid)
98+
99+
}
100+
101+
func (cli *Connection) reset() {
102+
cli.Uid = 0
103+
cli.Id = 0
104+
}
File renamed without changes.

kauth/base.go renamed to xauth/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kauth
1+
package xauth
22

33
import (
44
"context"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kauth
1+
package xauth
22

33
import (
44
"testing"

kauth/errors.go renamed to xauth/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kauth
1+
package xauth
22

33
type Error struct {
44
s string

0 commit comments

Comments
 (0)