/
store.go
154 lines (125 loc) 路 3.14 KB
/
store.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package dahua
import (
"context"
"sync"
"time"
"github.com/ItsNotGoodName/ipcmanview/internal/bus"
"github.com/ItsNotGoodName/ipcmanview/internal/core"
"github.com/rs/zerolog/log"
)
func newStoreClient(conn Conn) storeClient {
return storeClient{
Client: NewClient(conn),
}
}
type storeClient struct {
Client
}
func (c storeClient) logError(err error) {
if err != nil {
log.Err(err).Int64("id", c.Client.Conn.ID).Msg("Failed to close Client connection")
}
}
func NewStore() *Store {
return &Store{
shutdownTimeout: 3 * time.Second,
clientsMu: sync.Mutex{},
clients: make(map[int64]storeClient),
}
}
// Store holds clients.
type Store struct {
shutdownTimeout time.Duration
clientsMu sync.Mutex
clients map[int64]storeClient
}
func (*Store) String() string {
return "dahua.Store"
}
// Close closes all clients.
func (s *Store) Close() {
wg := sync.WaitGroup{}
ctx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
defer cancel()
for _, client := range s.clients {
wg.Add(1)
go func(client storeClient) {
defer wg.Done()
client.logError(client.Client.Close(ctx))
}(client)
}
wg.Wait()
}
func (s *Store) getOrCreateClient(ctx context.Context, conn Conn) Client {
client, ok := s.clients[conn.ID]
if !ok {
// Not found
client = newStoreClient(conn)
s.clients[conn.ID] = client
} else if !client.Client.Conn.EQ(conn) {
// Found but not equal
client.logError(client.Client.CloseNoWait(ctx))
client = newStoreClient(conn)
s.clients[conn.ID] = client
}
return client.Client
}
func (s *Store) GetClient(ctx context.Context, deviceID int64) (Client, error) {
s.clientsMu.Lock()
conn, err := GetConn(ctx, deviceID)
if err != nil {
s.clientsMu.Unlock()
return Client{}, err
}
client := s.getOrCreateClient(ctx, conn)
s.clientsMu.Unlock()
return client, nil
}
func (s *Store) ListClient(ctx context.Context) ([]Client, error) {
s.clientsMu.Lock()
conns, err := ListConn(ctx)
if err != nil {
s.clientsMu.Unlock()
return nil, err
}
var clients []Client
for _, conn := range conns {
clients = append(clients, s.getOrCreateClient(ctx, conn))
}
s.clientsMu.Unlock()
return clients, nil
}
func (s *Store) deleteClient(ctx context.Context, deviceID int64) {
s.clientsMu.Lock()
client, found := s.clients[deviceID]
if found {
delete(s.clients, deviceID)
}
s.clientsMu.Unlock()
if found {
client.logError(client.Client.Close(ctx))
}
}
func (s *Store) Register(hub *bus.Hub) *Store {
upsert := func(ctx context.Context, deviceID int64) error {
if _, err := s.GetClient(ctx, deviceID); err != nil {
if core.IsNotFound(err) {
s.deleteClient(ctx, deviceID)
return nil
}
return err
}
return nil
}
hub.OnDahuaDeviceCreated(s.String(), func(ctx context.Context, event bus.DahuaDeviceCreated) error {
return upsert(ctx, event.DeviceID)
})
hub.OnDahuaDeviceUpdated(s.String(), func(ctx context.Context, event bus.DahuaDeviceUpdated) error {
return upsert(ctx, event.DeviceID)
})
hub.OnDahuaDeviceDeleted(s.String(), func(ctx context.Context, event bus.DahuaDeviceDeleted) error {
s.deleteClient(ctx, event.DeviceID)
return nil
})
return s
}