-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.go
More file actions
46 lines (37 loc) · 972 Bytes
/
migrate.go
File metadata and controls
46 lines (37 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fastwire
import "sync"
// MigrationToken is an 8-byte connection identifier for connection migration.
type MigrationToken [8]byte
// MigrationTokenSize is the wire size of a migration token.
const MigrationTokenSize = 8
// tokenTable maps migration tokens to connections for server-side lookup.
type tokenTable struct {
mu sync.RWMutex
tokens map[MigrationToken]*Connection
}
func newTokenTable() *tokenTable {
return &tokenTable{
tokens: make(map[MigrationToken]*Connection),
}
}
func (tt *tokenTable) get(token MigrationToken) *Connection {
tt.mu.RLock()
c := tt.tokens[token]
tt.mu.RUnlock()
return c
}
func (tt *tokenTable) put(token MigrationToken, conn *Connection) {
tt.mu.Lock()
tt.tokens[token] = conn
tt.mu.Unlock()
}
func (tt *tokenTable) remove(token MigrationToken) {
tt.mu.Lock()
delete(tt.tokens, token)
tt.mu.Unlock()
}
func (tt *tokenTable) count() int {
tt.mu.RLock()
defer tt.mu.RUnlock()
return len(tt.tokens)
}