summaryrefslogtreecommitdiffstats
path: root/modules/log/event_writer_conn.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /modules/log/event_writer_conn.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r--modules/log/event_writer_conn.go111
1 files changed, 111 insertions, 0 deletions
diff --git a/modules/log/event_writer_conn.go b/modules/log/event_writer_conn.go
new file mode 100644
index 0000000..022206a
--- /dev/null
+++ b/modules/log/event_writer_conn.go
@@ -0,0 +1,111 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package log
+
+import (
+ "io"
+ "net"
+)
+
+type WriterConnOption struct {
+ Addr string
+ Protocol string
+ Reconnect bool
+ ReconnectOnMsg bool
+}
+
+type eventWriterConn struct {
+ *EventWriterBaseImpl
+ connWriter connWriter
+}
+
+var _ EventWriter = (*eventWriterConn)(nil)
+
+func NewEventWriterConn(writerName string, writerMode WriterMode) EventWriter {
+ w := &eventWriterConn{EventWriterBaseImpl: NewEventWriterBase(writerName, "conn", writerMode)}
+ opt := writerMode.WriterOption.(WriterConnOption)
+ w.connWriter = connWriter{
+ ReconnectOnMsg: opt.ReconnectOnMsg,
+ Reconnect: opt.Reconnect,
+ Net: opt.Protocol,
+ Addr: opt.Addr,
+ }
+ w.OutputWriteCloser = &w.connWriter
+ return w
+}
+
+func init() {
+ RegisterEventWriter("conn", NewEventWriterConn)
+}
+
+// below is copied from old code
+
+type connWriter struct {
+ innerWriter io.WriteCloser
+
+ ReconnectOnMsg bool
+ Reconnect bool
+ Net string `json:"net"`
+ Addr string `json:"addr"`
+}
+
+var _ io.WriteCloser = (*connWriter)(nil)
+
+// Close the inner writer
+func (i *connWriter) Close() error {
+ if i.innerWriter != nil {
+ return i.innerWriter.Close()
+ }
+ return nil
+}
+
+// Write the data to the connection
+func (i *connWriter) Write(p []byte) (int, error) {
+ if i.neededConnectOnMsg() {
+ if err := i.connect(); err != nil {
+ return 0, err
+ }
+ }
+
+ if i.ReconnectOnMsg {
+ defer i.innerWriter.Close()
+ }
+
+ return i.innerWriter.Write(p)
+}
+
+func (i *connWriter) neededConnectOnMsg() bool {
+ if i.Reconnect {
+ i.Reconnect = false
+ return true
+ }
+
+ if i.innerWriter == nil {
+ return true
+ }
+
+ return i.ReconnectOnMsg
+}
+
+func (i *connWriter) connect() error {
+ if i.innerWriter != nil {
+ _ = i.innerWriter.Close()
+ i.innerWriter = nil
+ }
+
+ conn, err := net.Dial(i.Net, i.Addr)
+ if err != nil {
+ return err
+ }
+
+ if tcpConn, ok := conn.(*net.TCPConn); ok {
+ err = tcpConn.SetKeepAlive(true)
+ if err != nil {
+ return err
+ }
+ }
+
+ i.innerWriter = conn
+ return nil
+}