This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandleSessionRestore.go
107 lines (92 loc) · 2.36 KB
/
handleSessionRestore.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
package webwire
import (
"encoding/json"
"fmt"
"github.com/qbeon/webwire-go/message"
)
// handleSessionRestore handles session restoration (by session key) requests
// and returns an error if the ongoing connection cannot be proceeded
func (srv *server) handleSessionRestore(
con *connection,
msg *message.Message,
) {
finalize := func() {
srv.deregisterHandler(con)
// Release message buffer
msg.Close()
}
if !srv.sessionsEnabled {
srv.failMsg(con, msg, ErrSessionsDisabled{})
finalize()
return
}
key := string(msg.MsgPayload.Data)
sessConsNum := srv.sessionRegistry.sessionConnectionsNum(key)
if sessConsNum >= 0 && srv.sessionRegistry.maxConns > 0 &&
uint(sessConsNum+1) > srv.sessionRegistry.maxConns {
srv.failMsg(con, msg, ErrMaxSessConnsReached{})
finalize()
return
}
// Call session manager lookup hook
result, err := srv.sessionManager.OnSessionLookup(key)
if err != nil {
// Fail message with internal error and log it in case the handler fails
srv.failMsg(con, msg, nil)
finalize()
srv.errorLog.Printf("session search handler failed: %s", err)
return
}
if result == nil {
// Fail message with special error if the session wasn't found
srv.failMsg(con, msg, ErrSessionNotFound{})
finalize()
return
}
sessionCreation := result.Creation()
sessionLastLookup := result.LastLookup()
sessionInfo := result.Info()
// JSON encode the session
encodedSessionObj := JSONEncodedSession{
Key: key,
Creation: sessionCreation,
LastLookup: sessionLastLookup,
Info: sessionInfo,
}
encodedSession, err := json.Marshal(&encodedSessionObj)
if err != nil {
srv.failMsg(con, msg, nil)
finalize()
srv.errorLog.Printf(
"couldn't encode session object (%v): %s",
encodedSessionObj,
err,
)
return
}
// Parse attached session info
var parsedSessInfo SessionInfo
if sessionInfo != nil && srv.sessionInfoParser != nil {
parsedSessInfo = srv.sessionInfoParser(sessionInfo)
}
con.setSession(&Session{
Key: key,
Creation: sessionCreation,
LastLookup: sessionLastLookup,
Info: parsedSessInfo,
})
if err := srv.sessionRegistry.register(con); err != nil {
panic(fmt.Errorf("the number of concurrent session connections was " +
"unexpectedly exceeded",
))
}
srv.fulfillMsg(
con,
msg,
Payload{
Encoding: EncodingUtf8,
Data: encodedSession,
},
)
finalize()
}