|
|
|
@@ -37,8 +37,8 @@ var AllInMessage chan InWechatMsg |
|
|
|
func (m *SessionManager) startSessionManager(in <-chan InWechatMsg) { |
|
|
|
log.Println("session manager start..") |
|
|
|
m.sessions = map[string]openIDSession{} |
|
|
|
m.done = make(chan workDone, 2000) |
|
|
|
for { //forever looping |
|
|
|
m.done = make(chan workDone, 2000) //2000 different openID simultaneously. |
|
|
|
for { //forever looping |
|
|
|
select { |
|
|
|
case msg := <-in: |
|
|
|
log.Printf("SessionMgr : incoming msg %s (%s)", msg.header.FromUserName, msg.header.MsgType) |
|
|
|
@@ -67,10 +67,10 @@ func (m *SessionManager) addJob(v InWechatMsg) (jobCount int) { |
|
|
|
s.count++ |
|
|
|
m.sessions[openID] = s |
|
|
|
|
|
|
|
log.Printf("Incoming message in queue %d", s.count) |
|
|
|
log.Printf("Total Session count = %d", len(m.sessions)) |
|
|
|
log.Printf("SessionMgr/addJob: Incoming message in queue %d", s.count) |
|
|
|
log.Printf("SessionMgr/addJob: Total Session count = %d", len(m.sessions)) |
|
|
|
if s.count <= 0 { |
|
|
|
log.Fatal("new job added, but count <= 0") |
|
|
|
log.Fatal("SessionMgr/Addjob: new job added, but count <= 0") |
|
|
|
} |
|
|
|
return s.count |
|
|
|
} |
|
|
|
@@ -88,22 +88,25 @@ func (m *SessionManager) createSession(openID string) openIDSession { |
|
|
|
|
|
|
|
func (m *SessionManager) clearJobDone(d workDone) { |
|
|
|
s, found := m.sessions[d.openID] |
|
|
|
log.Println("SessionMgr/clearJobDone: start clearning jobs ... ") |
|
|
|
log.Println(s) |
|
|
|
log.Println(d) |
|
|
|
if found { |
|
|
|
s.count -= d.consumed |
|
|
|
if s.count == 0 { //no job to do |
|
|
|
m.sessions[d.openID] = s //update |
|
|
|
if s.count == 0 { //no job to do |
|
|
|
//remove from memory |
|
|
|
m.destroySession(d.openID) |
|
|
|
log.Printf("destroy session %s", d.openID) |
|
|
|
log.Printf("SessionMgr/clearJobDone: destroy session %s", d.openID) |
|
|
|
} else if s.count > 0 { |
|
|
|
go m.startJob(d.openID) //processing any newly coming jobs |
|
|
|
} else { |
|
|
|
log.Println(s) |
|
|
|
log.Fatal("session job count cannot be negative, problem session") |
|
|
|
log.Fatal("SessionMgr/clearJobDone: session job count cannot be negative, problem session") |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.Println(d) |
|
|
|
log.Fatal("When job done, we canot find proper session") |
|
|
|
log.Fatal("SessionMgr/clearJobDone: When job done, we canot find proper session") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -117,12 +120,12 @@ func (m *SessionManager) destroySession(openID string) { |
|
|
|
close(s.jobs) |
|
|
|
//delete it from memory |
|
|
|
delete(m.sessions, openID) |
|
|
|
log.Printf("total session count=%d", len(m.sessions)) |
|
|
|
log.Printf("SessionMgr/destroySession: total session count=%d", len(m.sessions)) |
|
|
|
} |
|
|
|
|
|
|
|
//worker thread |
|
|
|
//worker thread, cannot change session info |
|
|
|
func (m *SessionManager) startJob(openID string) { |
|
|
|
log.Println("start job worker...") |
|
|
|
log.Println("SessionMgr/startJob: enter...") |
|
|
|
jobFinished := workDone{openID, 0} |
|
|
|
//process all jobs in the channel |
|
|
|
hasJob := true |
|
|
|
@@ -132,7 +135,8 @@ func (m *SessionManager) startJob(openID string) { |
|
|
|
m.checkOpenID(openID, v) |
|
|
|
s := m.sessions[openID] |
|
|
|
s.data.incomingMsg(v) //<=== main logic for processing each incoming message |
|
|
|
m.sessions[openID] = s |
|
|
|
log.Println("in worker thread, set s to session") |
|
|
|
log.Println(s) |
|
|
|
jobFinished.consumed++ |
|
|
|
default: |
|
|
|
hasJob = false |
|
|
|
@@ -144,8 +148,8 @@ func (m *SessionManager) startJob(openID string) { |
|
|
|
|
|
|
|
func (m *SessionManager) checkOpenID(openID string, v InWechatMsg) { |
|
|
|
if v.header.FromUserName != openID { |
|
|
|
log.Println("Error: Weird Message below ...") |
|
|
|
log.Println("Error:[SessionMgr/checkOpenID] Weird Message below ...") |
|
|
|
log.Println(v) |
|
|
|
log.Fatalf("Error: worker thread for %s, see different id=%s \n", openID, v.header.FromUserName) |
|
|
|
log.Fatalf("Error:[[SessionMgr/checkOpenID]] worker thread for %s, see different id=%s \n", openID, v.header.FromUserName) |
|
|
|
} |
|
|
|
} |