forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocklist.go
61 lines (53 loc) · 1.59 KB
/
blocklist.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package auth
import (
"context"
"fmt"
"sync"
)
var DefaultBlockList BlockList = &InMemoryBlockList{
blocked: map[string]bool{},
}
// BlockList manages a list of blocked IDs.
type BlockList interface {
// IsBlocked returns true if the id is blocked
IsBlocked(ctx context.Context, id, namespace string) (bool, error)
// Add adds an ID to the blocklist
Add(ctx context.Context, id, namespace string) error
// Remove removes an ID from the blocklist
Remove(ctx context.Context, id, namespace string) error
// List returns the entire list of blocked IDs
List(ctx context.Context) ([]string, error)
}
// InMemoryBlockList is an implementation of BlockList that maintains the list in memory only.
type InMemoryBlockList struct {
sync.RWMutex
blocked map[string]bool
}
func (i *InMemoryBlockList) IsBlocked(ctx context.Context, id, namespace string) (bool, error) {
i.RLock()
defer i.RUnlock()
return i.blocked[fmt.Sprintf("%s:%s", namespace, id)], nil
}
func (i *InMemoryBlockList) Add(ctx context.Context, id, namespace string) error {
i.Lock()
defer i.Unlock()
i.blocked[fmt.Sprintf("%s:%s", namespace, id)] = true
return nil
}
func (i *InMemoryBlockList) Remove(ctx context.Context, id, namespace string) error {
i.Lock()
defer i.Unlock()
delete(i.blocked, fmt.Sprintf("%s:%s", namespace, id))
return nil
}
func (i *InMemoryBlockList) List(ctx context.Context) ([]string, error) {
i.RLock()
defer i.RUnlock()
res := make([]string, len(i.blocked))
idx := 0
for k, _ := range i.blocked {
res[idx] = k
idx++
}
return res, nil
}