Mercurial > gemma
view pkg/auth/connection.go @ 471:1b08432a0e5d
Cosmetics.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 23 Aug 2018 10:11:50 +0200 |
parents | c1047fd04a3a |
children | b2dc9c2f69e0 |
line wrap: on
line source
package auth import ( "database/sql" "errors" "io" "log" "sync" "time" "gemma.intevation.de/gemma/pkg/misc" ) var ErrNoSuchToken = errors.New("No such token") const ( maxOpen = 16 maxDBIdle = time.Minute * 5 ) type Connection struct { session *Session access time.Time db *sql.DB refCount int mu sync.Mutex } func (c *Connection) serialize(w io.Writer) error { if err := c.session.serialize(w); err != nil { return err } access, err := c.last().MarshalText() if err != nil { return err } wr := misc.BinWriter{w, nil} wr.WriteBin(uint32(len(access))) wr.WriteBin(access) return wr.Err } func (c *Connection) deserialize(r io.Reader) error { session := new(Session) if err := session.deserialize(r); err != nil { return err } rd := misc.BinReader{r, nil} var l uint32 rd.ReadBin(&l) access := make([]byte, l) rd.ReadBin(access) if rd.Err != nil { return rd.Err } var t time.Time if err := t.UnmarshalText(access); err != nil { return err } *c = Connection{ session: session, access: t, } return nil } func (c *Connection) set(session *Session) { c.session = session c.touch() } func (c *Connection) touch() { c.mu.Lock() c.access = time.Now() c.mu.Unlock() } func (c *Connection) last() time.Time { c.mu.Lock() access := c.access c.mu.Unlock() return access } func (c *Connection) close() { if c.db != nil { if err := c.db.Close(); err != nil { log.Printf("warn: %v\n", err) } c.db = nil } }