forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
102 lines (84 loc) · 2.6 KB
/
session.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
package share
import (
"context"
"fmt"
"log"
"github.com/buraksezer/olric"
"github.com/buraksezer/olric/client"
config_olric "github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/serializer"
"github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
)
var sessServer *olric.Olric
// SessionConnect 加载会话信息
func SessionConnect(conf config.SessionConfig) {
var clientConfig = &client.Config{
Servers: []string{fmt.Sprintf("%s:%d", conf.Host, conf.Port)},
Serializer: serializer.NewMsgpackSerializer(),
Client: config_olric.NewClient(),
}
c, err := client.New(clientConfig)
if err != nil {
exception.New("会话服务器连接失败 %s", 500, err.Error()).Throw()
}
dm := c.NewDMap("local-session")
session.MemoryUse(session.ClientDMap{DMap: dm})
}
// SessionServerStop 关闭会话服务器
func SessionServerStop() {
if sessServer != nil {
sessServer.Shutdown(context.Background())
}
}
// SessionServerStart 启动会话服务器
func SessionServerStart() {
c := &config_olric.Config{
BindAddr: config.Conf.Session.Host,
BindPort: config.Conf.Session.Port,
ReadRepair: false,
ReplicaCount: 1,
WriteQuorum: 1,
ReadQuorum: 1,
MemberCountQuorum: 1,
Peers: []string{},
DMaps: &config_olric.DMaps{},
StorageEngines: config_olric.NewStorageEngine(),
}
m, err := config_olric.NewMemberlistConfig("local")
if err != nil {
panic(fmt.Sprintf("unable to create a new memberlist config: %v", err))
}
// m.BindAddr = config.Conf.Session.Host
m.BindPort = config.Conf.Session.Port
m.AdvertisePort = config.Conf.Session.Port
c.MemberlistConfig = m
// c.MemberlistConfig.BindAddr = config.Conf.Session.Host
// c.MemberlistConfig.BindPort = 3308
// c := config_olric.New("local")
// c.BindAddr = config.Conf.Session.Host
// c.BindPort = config.Conf.Session.Port
// c.Logger.SetOutput(ioutil.Discard) // 暂时关闭日志
ctx, cancel := context.WithCancel(context.Background())
c.Started = func() {
defer cancel()
log.Println("[INFO] Olric is ready to accept connections")
}
sessServer, err = olric.New(c)
if err != nil {
log.Fatalf("Failed to create Olric instance: %v", err)
}
go func() {
err = sessServer.Start() // Call Start at background. It's a blocker call.
if err != nil {
log.Fatalf("olric.Start returned an error: %v", err)
}
}()
<-ctx.Done()
dm, err := sessServer.NewDMap("local-session")
if err != nil {
log.Fatalf("olric.NewDMap returned an error: %v", err)
}
session.MemoryUse(session.ServerDMap{DMap: dm})
}