-
Notifications
You must be signed in to change notification settings - Fork 2
/
request_record.go
71 lines (63 loc) 路 2.07 KB
/
request_record.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
package server
import (
"fmt"
"net/http"
"sync"
"github.com/cairoeth/preconfirmations/rpc/database"
"github.com/google/uuid"
)
type requestRecord struct {
requestEntry database.RequestEntry
ethSendRawTxEntries []*database.EthSendRawTxEntry
mutex sync.Mutex
db database.Store
}
func NewRequestRecord(db database.Store) *requestRecord {
return &requestRecord{
db: db,
}
}
func (r *requestRecord) AddEthSendRawTxEntry(id uuid.UUID) *database.EthSendRawTxEntry {
entry := &database.EthSendRawTxEntry{
ID: id,
RequestID: r.requestEntry.ID,
}
r.mutex.Lock()
defer r.mutex.Unlock()
r.ethSendRawTxEntries = append(r.ethSendRawTxEntries, entry)
return entry
}
func (r *requestRecord) UpdateRequestEntry(req *http.Request, reqStatus int, error string) {
r.requestEntry.HTTPMethod = req.Method
r.requestEntry.Error = error
r.requestEntry.HTTPURL = req.URL.Path
r.requestEntry.HTTPQueryParam = req.URL.RawQuery
r.requestEntry.HTTPResponseStatus = reqStatus
r.requestEntry.Origin = req.Header.Get("Origin")
r.requestEntry.Host = req.Header.Get("Host")
}
// SaveRecord will insert both requestRecord and rawTxEntries to db
func (r *requestRecord) SaveRecord() error {
entries := r.getValidRawTxEntriesToSave()
if len(entries) > 0 { // Save entries if the request contains rawTxEntries
if err := r.db.SaveRequestEntry(r.requestEntry); err != nil {
return fmt.Errorf("SaveRequestEntry failed %v", err)
}
if err := r.db.SaveRawTxEntries(entries); err != nil {
return fmt.Errorf("SaveRawTxEntries failed %v", err)
}
}
return nil
}
// getValidRawTxEntriesToSave returns list of rawTxEntry which are either sent to relay or mempool or entry with error
func (r *requestRecord) getValidRawTxEntriesToSave() []*database.EthSendRawTxEntry {
entries := make([]*database.EthSendRawTxEntry, 0, len(r.ethSendRawTxEntries))
r.mutex.Lock()
defer r.mutex.Unlock()
for _, entry := range r.ethSendRawTxEntries {
if entry.ErrorCode != 0 || entry.WasSentToRelay || entry.WasSentToMempool {
entries = append(entries, entry)
}
}
return entries
}