-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor approach to use different protocol instead of parameter.
This is a new approach to switch between http and http batching. It only is different in this regard from the previous attempts, and only contains refactorings besides this change.
- Loading branch information
1 parent
f206257
commit 35104c5
Showing
5 changed files
with
136 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package syslog | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"time" | ||
|
||
"code.cloudfoundry.org/go-loggregator/v9/rpc/loggregator_v2" | ||
metrics "code.cloudfoundry.org/go-metric-registry" | ||
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress" | ||
) | ||
|
||
const BATCHSIZE = 256 * 1024 | ||
|
||
type HTTPSBatchWriter struct { | ||
HTTPSWriter | ||
msgBatch bytes.Buffer | ||
batchSize int | ||
sendInterval time.Duration | ||
sendTimer TriggerTimer | ||
egrMsgCount float64 | ||
} | ||
|
||
func NewHTTPSBatchWriter( | ||
binding *URLBinding, | ||
netConf NetworkTimeoutConfig, | ||
tlsConf *tls.Config, | ||
egressMetric metrics.Counter, | ||
c *Converter, | ||
) egress.WriteCloser { | ||
client := httpClient(netConf, tlsConf) | ||
binding.URL.Scheme = "https" // reset the scheme for usage to a valid http scheme | ||
return &HTTPSBatchWriter{ | ||
HTTPSWriter: HTTPSWriter{ | ||
url: binding.URL, | ||
appID: binding.AppID, | ||
hostname: binding.Hostname, | ||
client: client, | ||
egressMetric: egressMetric, | ||
syslogConverter: c, | ||
}, | ||
batchSize: BATCHSIZE, | ||
sendInterval: time.Second, | ||
egrMsgCount: 0, | ||
} | ||
} | ||
|
||
func (w *HTTPSBatchWriter) sendMsgBatch() error { | ||
currentEgrCount := w.egrMsgCount | ||
currentMsg := w.msgBatch.Bytes() | ||
|
||
w.egrMsgCount = 0 | ||
w.msgBatch.Reset() | ||
|
||
return w.sendHttpRequest(currentMsg, currentEgrCount) | ||
} | ||
|
||
// Modified Write function | ||
func (w *HTTPSBatchWriter) Write(env *loggregator_v2.Envelope) error { | ||
msgs, err := w.syslogConverter.ToRFC5424(env, w.hostname) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, msg := range msgs { | ||
w.msgBatch.Write(msg) | ||
w.egrMsgCount += 1 | ||
w.startAndTriggerSend() | ||
} | ||
return nil | ||
} | ||
|
||
// TODO: Error back propagation. Errors are not looked at further down the call chain | ||
func (w *HTTPSBatchWriter) startAndTriggerSend() { | ||
if !w.sendTimer.Running() { | ||
w.sendTimer.Start(w.sendInterval, func() { | ||
w.sendMsgBatch() | ||
}) | ||
} | ||
if w.msgBatch.Len() >= w.batchSize { | ||
w.sendTimer.Trigger() | ||
} | ||
} | ||
|
||
type TriggerTimer struct { | ||
trigger chan int | ||
running bool | ||
} | ||
|
||
type Timer interface { | ||
Start(d time.Duration, f func()) | ||
} | ||
|
||
func NewTriggerTimer() Timer { | ||
return &TriggerTimer{ | ||
running: false, | ||
} | ||
} | ||
|
||
func (t *TriggerTimer) Start(d time.Duration, f func()) { | ||
t.running = true | ||
for { | ||
timer := time.NewTimer(d) | ||
select { | ||
case <-timer.C: | ||
case <-t.trigger: | ||
f() | ||
t.running = false | ||
} | ||
} | ||
} | ||
|
||
func (t *TriggerTimer) Trigger() { | ||
t.trigger <- 1 | ||
} | ||
|
||
func (t *TriggerTimer) Running() bool { | ||
return t.running | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters