Mercurial > gemma
comparison auth/persistent.go @ 201:80dc7bbe97db
Persistent session store: Implemented Do.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sun, 22 Jul 2018 09:34:45 +0200 |
parents | 8426a92fda00 |
children | dba50c51cda7 |
comparison
equal
deleted
inserted
replaced
200:8426a92fda00 | 201:80dc7bbe97db |
---|---|
165 | 165 |
166 r := <-resCh | 166 r := <-resCh |
167 return r.newToken, r.err | 167 return r.newToken, r.err |
168 } | 168 } |
169 | 169 |
170 func (pcp *PersistentConnectionPool) trim(conn *Connection) { | |
171 | |
172 conn.refCount-- | |
173 | |
174 for { | |
175 least := time.Now() | |
176 var count int | |
177 var oldest *Connection | |
178 | |
179 for _, con := range pcp.conns { | |
180 if con.db != nil && con.refCount <= 0 { | |
181 if last := con.last(); last.Before(least) { | |
182 least = last | |
183 oldest = con | |
184 } | |
185 count++ | |
186 } | |
187 } | |
188 if count <= maxOpen { | |
189 break | |
190 } | |
191 oldest.close() | |
192 } | |
193 } | |
194 | |
170 func (pcp *PersistentConnectionPool) Do(token string, fn func(*sql.DB) error) error { | 195 func (pcp *PersistentConnectionPool) Do(token string, fn func(*sql.DB) error) error { |
171 log.Println("Do: Not implemented, yet.") | 196 |
172 return nil | 197 type result struct { |
198 con *Connection | |
199 err error | |
200 } | |
201 | |
202 res := make(chan result) | |
203 | |
204 pcp.cmds <- func(pcp *PersistentConnectionPool) { | |
205 con := pcp.conns[token] | |
206 if con == nil { | |
207 res <- result{err: ErrNoSuchToken} | |
208 return | |
209 } | |
210 con.touch() | |
211 // store the session here. The ref counting for | |
212 // open db connections is irrelevant for persistence | |
213 // as they all come up closed when the system reboots. | |
214 pcp.store(token, con) | |
215 | |
216 if con.db != nil { | |
217 con.refCount++ | |
218 res <- result{con: con} | |
219 return | |
220 } | |
221 | |
222 session := con.session | |
223 db, err := opendb(session.User, session.Password) | |
224 if err != nil { | |
225 res <- result{err: err} | |
226 return | |
227 } | |
228 con.db = db | |
229 con.refCount++ | |
230 res <- result{con: con} | |
231 } | |
232 | |
233 r := <-res | |
234 | |
235 if r.err != nil { | |
236 return r.err | |
237 } | |
238 | |
239 defer func() { | |
240 pcp.cmds <- func(pcp *PersistentConnectionPool) { | |
241 pcp.trim(r.con) | |
242 } | |
243 }() | |
244 | |
245 return fn(r.con.db) | |
173 } | 246 } |
174 | 247 |
175 func (pcp *PersistentConnectionPool) Session(token string) *Session { | 248 func (pcp *PersistentConnectionPool) Session(token string) *Session { |
176 log.Println("Session: Not implemented, yet.") | 249 log.Println("Session: Not implemented, yet.") |
177 return nil | 250 return nil |