-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_torrent.go
223 lines (184 loc) · 6.7 KB
/
store_torrent.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package tracker
import (
"context"
"time"
"github.com/gofrs/uuid"
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type TorrentStorable interface {
// Add torrent to store.
AddTorrent(ctx context.Context, infoHash []byte) (Torrent, error)
// Get torrent from store.
Torrent(ctx context.Context, infoHash []byte) (Torrent, error)
// Increments torrentID completed property by one.
IncrementTorrent(ctx context.Context, torrentID uuid.UUID) error
// Get all torrents in store.
Torrents(ctx context.Context) ([]Torrent, error)
Scrape(ctx context.Context, hashes [][]byte) ([]Torrent, error)
// Get all peers for torrentID.
Peers(ctx context.Context, torrentID uuid.UUID) ([]Peer, error)
// Try to update peer which already exist in the store.
// Operation success is denoted by bool.
UpdatePeerWithKey(ctx context.Context, torrentID uuid.UUID, req AnnounceRequest) (bool, error)
// Update or insert peer to store.
UpsertPeer(ctx context.Context, torrentID uuid.UUID, req AnnounceRequest) error
// Remove stale peers that have not announced in interval.
CleanPeers(ctx context.Context, interval time.Duration) (int, error)
// Log announce request.
Log(ctx context.Context, req AnnounceRequest) error
// Test store connection.
Ping(ctx context.Context) (bool, error)
}
type torrentStore struct {
pool *pgxpool.Pool
}
func NewTorrentStore(pool *pgxpool.Pool) *torrentStore {
return &torrentStore{
pool: pool,
}
}
func (ts *torrentStore) AddTorrent(ctx context.Context, infoHash []byte) (Torrent, error) {
query := `insert into torrents (id, info_hash, completed, created_at)
values (gen_random_uuid(), $1, 0, now())
returning id, info_hash, completed, created_at, 0 as seeders, 0 as leechers`
rows, err := ts.pool.Query(ctx, query, infoHash)
if err != nil {
return Torrent{}, err
}
defer rows.Close()
torrent, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[Torrent])
if err != nil {
return Torrent{}, err
}
return torrent, nil
}
func (ts *torrentStore) IncrementTorrent(ctx context.Context, torrentID uuid.UUID) error {
query := `update torrents
set completed = completed + 1
where id = $1`
_, err := ts.pool.Exec(ctx, query, torrentID)
if err != nil {
return err
}
return nil
}
func (ts *torrentStore) Peers(ctx context.Context, torrentID uuid.UUID) ([]Peer, error) {
query := `select id, torrent_id, peer_id, ip, port, uploaded, downloaded, "left", event, key, updated_at
from peers
where torrent_id = $1
limit 24`
var peers []Peer
rows, err := ts.pool.Query(ctx, query, torrentID)
if err != nil {
return []Peer{}, err
}
defer rows.Close()
peers, err = pgx.CollectRows[Peer](rows, pgx.RowToStructByName[Peer])
if err != nil {
return []Peer{}, err
}
return peers, nil
}
func (ts *torrentStore) UpdatePeerWithKey(ctx context.Context, torrentID uuid.UUID, req AnnounceRequest) (bool, error) {
query := `update peers set
peer_id = $1, ip = $2, port = $3, uploaded = $4, downloaded = $5, "left" = $6, event = $7, updated_at = now()
where torrent_id = $8 and key = $9`
tag, err := ts.pool.Exec(ctx, query,
req.PeerID, req.IP, req.Port, req.Uploaded, req.Downloaded, req.Left, req.Event,
torrentID, req.Key)
if err != nil {
return false, err
}
return (tag.RowsAffected() > 0), nil
}
func (ts *torrentStore) UpsertPeer(ctx context.Context, torrentID uuid.UUID, req AnnounceRequest) error {
query := `insert into peers (id, torrent_id, peer_id, ip, port, uploaded, downloaded, "left", event, key, updated_at)
values (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, now())
on conflict (torrent_id, peer_id) do update set
"left" = $10, uploaded = $11, downloaded = $12, updated_at = now(), event = $13`
_, err := ts.pool.Exec(ctx, query,
torrentID, req.PeerID, req.IP, req.Port, req.Uploaded, req.Downloaded, req.Left, req.Event, req.Key,
req.Left, req.Uploaded, req.Downloaded, req.Event)
if err != nil {
return err
}
return nil
}
func (ts *torrentStore) Torrent(ctx context.Context, infoHash []byte) (Torrent, error) {
query := `select t.id, t.info_hash, t.completed, t.created_at,
(select count(*) from peers where peers.torrent_id = t.id and peers.left = 0) as seeders,
(select count(*) from peers where peers.torrent_id = t.id and peers.left != 0) as leechers
from torrents t
where t.info_hash = $1
limit 1`
var torrent Torrent
rows, err := ts.pool.Query(ctx, query, infoHash)
if err != nil {
return Torrent{}, err
}
defer rows.Close()
torrent, err = pgx.CollectOneRow(rows, pgx.RowToStructByName[Torrent])
if err != nil {
return Torrent{}, err
}
return torrent, nil
}
func (ts *torrentStore) Torrents(ctx context.Context) ([]Torrent, error) {
query := `select t.id, t.info_hash, t.completed, t.created_at,
(select count(*) from peers where peers.torrent_id = t.id and peers.left = 0) as seeders,
(select count(*) from peers where peers.torrent_id = t.id and peers.left != 0) as leechers
from torrents t`
var torrents []Torrent
rows, err := ts.pool.Query(ctx, query)
if err != nil {
return []Torrent{}, err
}
defer rows.Close()
torrents, err = pgx.CollectRows[Torrent](rows, pgx.RowToStructByName[Torrent])
if err != nil {
return []Torrent{}, err
}
return torrents, nil
}
func (ts *torrentStore) Scrape(ctx context.Context, hashes [][]byte) ([]Torrent, error) {
query := `select t.id, t.info_hash, t.completed, t.created_at,
(select count(*) from peers where peers.torrent_id = t.id and peers.left = 0) as seeders,
(select count(*) from peers where peers.torrent_id = t.id and peers.left != 0) as leechers
from torrents t
where t.info_hash = any($1)`
rows, err := ts.pool.Query(ctx, query, hashes)
if err != nil {
return nil, err
}
torrents, err := pgx.CollectRows[Torrent](rows, pgx.RowToStructByName[Torrent])
if err != nil {
return []Torrent{}, err
}
return torrents, nil
}
// Removers stale peers that have not updated in a x duration.
func (ts *torrentStore) CleanPeers(ctx context.Context, interval time.Duration) (int, error) {
query := `delete from peers where updated_at < now() - $1::interval`
tag, err := ts.pool.Exec(ctx, query, interval)
if err != nil {
return 0, err
}
return int(tag.RowsAffected()), nil
}
func (ts *torrentStore) Log(ctx context.Context, req AnnounceRequest) error {
query := `insert into announce_log (id, info_hash, peer_id, event, ip, port, key, uploaded, downloaded, "left", created_at)
values (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, now())`
_, err := ts.pool.Exec(ctx, query, req.InfoHash, req.PeerID, req.Event, req.IP, req.Port, req.Key, req.Uploaded, req.Downloaded, req.Left)
if err != nil {
return err
}
return nil
}
func (ts *torrentStore) Ping(ctx context.Context) (bool, error) {
err := ts.pool.Ping(ctx)
if err != nil {
return false, err
}
return true, nil
}