|
5 | 5 | _ "net/http/pprof" |
6 | 6 | "os" |
7 | 7 | "os/signal" |
| 8 | + "sync/atomic" |
8 | 9 | "syscall" |
9 | 10 | "time" |
10 | 11 |
|
@@ -116,41 +117,94 @@ func main() { |
116 | 117 | default: |
117 | 118 | log.Panicf("no writer config entry found") |
118 | 119 | } |
| 120 | + |
119 | 121 | // create status |
120 | | - status.Init(theReader, theWriter) |
| 122 | + if config.Opt.Advanced.StatusPort != 0 { |
| 123 | + status.Init(theReader, theWriter) |
| 124 | + } |
| 125 | + // create log entry count |
| 126 | + logEntryCount := status.EntryCount{ |
| 127 | + ReadCount: 0, |
| 128 | + WriteCount: 0, |
| 129 | + } |
121 | 130 |
|
122 | 131 | log.Infof("start syncing...") |
123 | 132 |
|
124 | | - ch := theReader.StartRead(ctx) |
125 | 133 | go waitShutdown(cancel) |
126 | 134 |
|
| 135 | + chrs := theReader.StartRead(ctx) |
| 136 | + |
| 137 | + theWriter.StartWrite(ctx) |
| 138 | + |
| 139 | + readerDone := make(chan bool) |
| 140 | + |
| 141 | + for _, chr := range chrs { |
| 142 | + go func(ch chan *entry.Entry) { |
| 143 | + for e := range ch { |
| 144 | + // calc arguments |
| 145 | + e.Parse() |
| 146 | + |
| 147 | + // update reader status |
| 148 | + if config.Opt.Advanced.StatusPort != 0 { |
| 149 | + status.AddReadCount(e.CmdName) |
| 150 | + } |
| 151 | + // update log entry count |
| 152 | + atomic.AddUint64(&logEntryCount.ReadCount, 1) |
| 153 | + |
| 154 | + // filter |
| 155 | + if !filter.Filter(e) { |
| 156 | + log.Debugf("skip command: %v", e) |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + // run lua function |
| 161 | + log.Debugf("function before: %v", e) |
| 162 | + entries := luaRuntime.RunFunction(e) |
| 163 | + log.Debugf("function after: %v", entries) |
| 164 | + |
| 165 | + // write |
| 166 | + for _, theEntry := range entries { |
| 167 | + theEntry.Parse() |
| 168 | + theWriter.Write(theEntry) |
| 169 | + |
| 170 | + // update writer status |
| 171 | + if config.Opt.Advanced.StatusPort != 0 { |
| 172 | + status.AddWriteCount(theEntry.CmdName) |
| 173 | + } |
| 174 | + // update log entry count |
| 175 | + atomic.AddUint64(&logEntryCount.WriteCount, 1) |
| 176 | + } |
| 177 | + } |
| 178 | + readerDone <- true |
| 179 | + }(chr) |
| 180 | + } |
| 181 | + |
| 182 | + // caluate ops and log to screen |
| 183 | + go func() { |
| 184 | + if config.Opt.Advanced.LogInterval <= 0 { |
| 185 | + log.Infof("log interval is 0, will not log to screen") |
| 186 | + return |
| 187 | + } |
| 188 | + ticker := time.NewTicker(time.Duration(config.Opt.Advanced.LogInterval) * time.Second) |
| 189 | + defer ticker.Stop() |
| 190 | + for range ticker.C { |
| 191 | + logEntryCount.UpdateOPS() |
| 192 | + log.Infof("%s, %s", logEntryCount.String(), theReader.StatusString()) |
| 193 | + } |
| 194 | + }() |
| 195 | + |
127 | 196 | ticker := time.NewTicker(1 * time.Second) |
128 | 197 | defer ticker.Stop() |
| 198 | + readerCnt := len(chrs) |
129 | 199 | Loop: |
130 | 200 | for { |
131 | 201 | select { |
132 | | - case e, ok := <-ch: |
133 | | - if !ok { |
134 | | - // ch has been closed, exit the loop |
135 | | - break Loop |
136 | | - } |
137 | | - // calc arguments |
138 | | - e.Parse() |
139 | | - status.AddReadCount(e.CmdName) |
140 | | - |
141 | | - // filter |
142 | | - if !filter.Filter(e) { |
143 | | - log.Debugf("skip command: %v", e) |
144 | | - continue |
| 202 | + case done := <-readerDone: |
| 203 | + if done { |
| 204 | + readerCnt-- |
145 | 205 | } |
146 | | - log.Debugf("function before: %v", e) |
147 | | - entries := luaRuntime.RunFunction(e) |
148 | | - log.Debugf("function after: %v", entries) |
149 | | - |
150 | | - for _, theEntry := range entries { |
151 | | - theEntry.Parse() |
152 | | - theWriter.Write(theEntry) |
153 | | - status.AddWriteCount(theEntry.CmdName) |
| 206 | + if readerCnt == 0 { |
| 207 | + break Loop |
154 | 208 | } |
155 | 209 | case <-ticker.C: |
156 | 210 | pingEntry := entry.NewEntry() |
|
0 commit comments