From 1c54091cf71bf9f92475e02b94153e8b4bfbe36b Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 28 Dec 2015 17:20:14 +0100 Subject: refactoring of session-store, seperate public from private interface diff --git a/session_store.go b/session_store.go index e065182..43b5d23 100644 --- a/session_store.go +++ b/session_store.go @@ -63,12 +63,6 @@ type removeSessionRequest struct { response chan removeSessionResponse } -type SessionStoreChan struct { - newChan chan<- newSessionRequest - getChan chan<- getSessionRequest - removeChan chan<- removeSessionRequest -} - type SessionStore struct { store map[string]map[string]*Session quit chan bool @@ -99,20 +93,6 @@ func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { return } -func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, error) { - res_ch := make(chan newSessionResponse) - req := newSessionRequest{} - req.ctx = ctx - req.response = res_ch - self.newChan <- req - - res := <-res_ch - if res.err != nil { - return "", nil, res.err - } - return res.id, res.session, nil -} - func (self *SessionStore) get(user, id string) (resp getSessionResponse) { if session, exists := self.store[user][id]; exists { resp.session = session.getInterface() @@ -122,21 +102,6 @@ func (self *SessionStore) get(user, id string) (resp getSessionResponse) { return } -func (self *SessionStoreChan) Get(user, id string) (*SessionChan, error) { - res_ch := make(chan getSessionResponse) - req := getSessionRequest{} - req.user = user - req.id = id - req.response = res_ch - self.getChan <- req - - res := <-res_ch - if res.err != nil { - return nil, res.err - } - return res.session, nil -} - func (self *SessionStore) remove(user, id string) (resp removeSessionResponse) { if session, exists := self.store[user][id]; exists { go session.Cleanup() // cleanup could take a while -> don't block all the other stuff @@ -154,18 +119,6 @@ func (self *SessionStore) remove(user, id string) (resp removeSessionResponse) { return } -func (self *SessionStoreChan) Remove(user, id string) error { - res_ch := make(chan removeSessionResponse) - req := removeSessionRequest{} - req.user = user - req.id = id - req.response = res_ch - self.removeChan <- req - - res := <-res_ch - return res.err -} - func (self *SessionStore) dispatchRequests() { defer func() { self.done <- true }() for { @@ -182,6 +135,56 @@ func (self *SessionStore) dispatchRequests() { } } +// ********************************************************* +// Public Interface + +type SessionStoreChan struct { + newChan chan<- newSessionRequest + getChan chan<- getSessionRequest + removeChan chan<- removeSessionRequest +} + +func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, error) { + res_ch := make(chan newSessionResponse) + req := newSessionRequest{} + req.ctx = ctx + req.response = res_ch + self.newChan <- req + + res := <-res_ch + if res.err != nil { + return "", nil, res.err + } + return res.id, res.session, nil +} + +func (self *SessionStoreChan) Get(user, id string) (*SessionChan, error) { + res_ch := make(chan getSessionResponse) + req := getSessionRequest{} + req.user = user + req.id = id + req.response = res_ch + self.getChan <- req + + res := <-res_ch + if res.err != nil { + return nil, res.err + } + return res.session, nil +} + +func (self *SessionStoreChan) Remove(user, id string) error { + res_ch := make(chan removeSessionResponse) + req := removeSessionRequest{} + req.user = user + req.id = id + req.response = res_ch + self.removeChan <- req + + res := <-res_ch + return res.err +} + func (self *SessionStore) GetInterface() *SessionStoreChan { ch := &SessionStoreChan{} ch.newChan = self.newChan -- cgit v0.10.2