-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackerrepl.go
143 lines (113 loc) · 2.7 KB
/
trackerrepl.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
package main
import (
"fmt"
"os"
"sync"
"time"
"github.com/BurntSushi/toml"
)
type replTracker struct {
v *os.File
i *os.File
c *os.File
vMu sync.Mutex
vCnf *replConf
iMu sync.Mutex
iCnf *replConf
cMu sync.Mutex
cCnf *replConf
}
func newReplTracker() (*replTracker, error) {
tm := time.Now()
ft := tm.Format("200601021504")
v, err := os.Create(ft + "_valid")
if err != nil {
return nil, err
}
i, err := os.Create(ft + "_invalid")
if err != nil {
return nil, err
}
c, err := os.Create(ft + "_original")
if err != nil {
return nil, err
}
t := &replTracker{
v: v,
i: i,
c: c,
vCnf: &replConf{},
iCnf: &replConf{},
cCnf: &replConf{},
}
return t, nil
}
func (t *replTracker) logServers(srvrs *replServersConf) {
addReplTrackedServers(&t.vMu, t.vCnf, srvrs)
addReplTrackedServers(&t.iMu, t.iCnf, srvrs)
addReplTrackedServers(&t.cMu, t.cCnf, srvrs)
}
func addReplTrackedServers(mu sync.Locker, cnf *replConf, srvrs *replServersConf) {
s := *srvrs
s.Accounts = nil
mu.Lock()
cnf.Servers = append(cnf.Servers, s)
mu.Unlock()
}
func (t *replTracker) logValidAccts(srvrID int, accts *replAccountsConf) {
addReplTrackedAccts(&t.vMu, t.vCnf, srvrID, accts)
addReplTrackedAccts(&t.cMu, t.cCnf, srvrID, accts)
}
func (t *replTracker) logInvalidAccts(srvrID int, accts *replAccountsConf) {
addReplTrackedAccts(&t.iMu, t.iCnf, srvrID, accts)
addReplTrackedAccts(&t.cMu, t.cCnf, srvrID, accts)
}
func addReplTrackedAccts(mu sync.Locker, cnf *replConf, srvrID int, accts *replAccountsConf) {
mu.Lock()
cnf.Servers[srvrID].Accounts = append(cnf.Servers[srvrID].Accounts, *accts)
mu.Unlock()
}
func (t *replTracker) close() error {
var errs []error
if err := closeReplTrackerFile(&t.vMu, t.v, t.vCnf); err != nil {
errs = append(errs, err)
}
if err := closeReplTrackerFile(&t.iMu, t.i, t.iCnf); err != nil {
errs = append(errs, err)
}
if err := closeReplTrackerFile(&t.cMu, t.c, t.cCnf); err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
return nil
}
e := ""
for _, err := range errs {
e += "& " + err.Error()
}
return fmt.Errorf(e)
}
func closeReplTrackerFile(mu sync.Locker, f *os.File, cnf *replConf) error {
mu.Lock()
defer mu.Unlock()
defer func() { _ = f.Close() }()
cnf = pruneEmptyReplTrackedServers(cnf)
if len(cnf.Servers) == 0 {
return nil
}
type replwrap struct {
Repl replConf `toml:"Repl,omitempty" json:"Repl,omitempty"`
}
enc := toml.NewEncoder(f)
return enc.Encode(replwrap{*cnf})
}
func pruneEmptyReplTrackedServers(cnf *replConf) *replConf {
ss := cnf.Servers[:0]
for _, s := range cnf.Servers {
if s.Accounts != nil && len(s.Accounts) > 0 {
ss = append(ss, s)
}
}
cnf.Servers = ss
return cnf
}