Mercurial > gemma
annotate auth/connection.go @ 303:75e32633fb96
Makefile: Force rebuild of dependencies when 3rdpartylibs.sh changes.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Wed, 01 Aug 2018 13:37:48 +0200 |
parents | 11d1a488b08f |
children | 33b59c848771 |
rev | line source |
---|---|
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package auth |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 "database/sql" |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "errors" |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
6 "io" |
126
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
27
diff
changeset
|
7 "log" |
134
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
8 "sync" |
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 "time" |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 ) |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 var ErrNoSuchToken = errors.New("No such token") |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 const ( |
130
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
15 maxOpen = 16 |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
16 maxDBIdle = time.Minute * 5 |
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 ) |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 type Connection struct { |
134
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
20 session *Session |
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 |
131
af114cf64822
Added some reference counting to open db connection to not forcefully close them when system is under pressure. TODO: check if this race free (it should).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
130
diff
changeset
|
22 access time.Time |
af114cf64822
Added some reference counting to open db connection to not forcefully close them when system is under pressure. TODO: check if this race free (it should).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
130
diff
changeset
|
23 db *sql.DB |
af114cf64822
Added some reference counting to open db connection to not forcefully close them when system is under pressure. TODO: check if this race free (it should).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
130
diff
changeset
|
24 refCount int |
134
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
25 |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
26 mu sync.Mutex |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
27 } |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
28 |
198
c20e86a3c073
Made serialisation of sessions symmetrical.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
29 func (c *Connection) serialize(w io.Writer) error { |
c20e86a3c073
Made serialisation of sessions symmetrical.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
30 if err := c.session.serialize(w); err != nil { |
c20e86a3c073
Made serialisation of sessions symmetrical.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
31 return err |
c20e86a3c073
Made serialisation of sessions symmetrical.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
32 } |
c20e86a3c073
Made serialisation of sessions symmetrical.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
33 access, err := c.last().MarshalText() |
215
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
34 if err != nil { |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
35 return err |
198
c20e86a3c073
Made serialisation of sessions symmetrical.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
36 } |
215
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
37 wr := binWriter{w: w} |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
38 wr.write(uint32(len(access))) |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
39 wr.write(access) |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
40 return wr.err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
41 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
42 |
197
e85413e5befa
Cleaned up serialisation/deserilisation of sessions a bit.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
193
diff
changeset
|
43 func (c *Connection) deserialize(r io.Reader) error { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
44 session := new(Session) |
197
e85413e5befa
Cleaned up serialisation/deserilisation of sessions a bit.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
193
diff
changeset
|
45 if err := session.deserialize(r); err != nil { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
46 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
47 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
48 |
215
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
49 rd := binReader{r: r} |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
50 var l uint32 |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
51 rd.read(&l) |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
52 access := make([]byte, l) |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
53 rd.read(access) |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
54 |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
55 if rd.err != nil { |
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
56 return rd.err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
57 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
58 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
59 var t time.Time |
215
f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
204
diff
changeset
|
60 if err := t.UnmarshalText(access); err != nil { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
61 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
62 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
63 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
64 *c = Connection{ |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
65 session: session, |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
66 access: t, |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
67 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
68 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
69 return nil |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
70 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
71 |
134
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
72 func (c *Connection) set(session *Session) { |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
73 c.session = session |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
74 c.touch() |
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 |
134
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
77 func (c *Connection) touch() { |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
78 c.mu.Lock() |
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 c.access = time.Now() |
134
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
80 c.mu.Unlock() |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
81 } |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
82 |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
83 func (c *Connection) last() time.Time { |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
84 c.mu.Lock() |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
85 access := c.access |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
86 c.mu.Unlock() |
0c56c56a1c44
Removed the JWT layer from the session management.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
133
diff
changeset
|
87 return access |
26
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
96a429c5f227
Fundamental connection pool based on tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
130
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
90 func (c *Connection) close() { |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
91 if c.db != nil { |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
92 if err := c.db.Close(); err != nil { |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
93 log.Printf("warn: %v\n", err) |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
94 } |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
95 c.db = nil |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
96 } |
13b82701b1fb
Take expiring time from serialized tokens to garbage collect them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
129
diff
changeset
|
97 } |