summaryrefslogtreecommitdiff
path: root/internal/server
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-10-02 11:54:07 +0300
committerPaul Buetow <paul@buetow.org>2021-10-02 15:11:23 +0300
commit12c79f68bb5bda6673819d7b754820ecfe6d08ff (patch)
treea779dc77dc8bf5a2dfc2030c718ce4543bfba6c7 /internal/server
parent6e1af993924bc7bebe898b403962db5a6b3505d1 (diff)
reduce logging in serverless mode
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/handlers/readcommand.go12
-rw-r--r--internal/server/handlers/serverhandler.go93
2 files changed, 36 insertions, 69 deletions
diff --git a/internal/server/handlers/readcommand.go b/internal/server/handlers/readcommand.go
index c76ae2a..6579018 100644
--- a/internal/server/handlers/readcommand.go
+++ b/internal/server/handlers/readcommand.go
@@ -32,13 +32,13 @@ func (r *readCommand) Start(ctx context.Context, argc int, args []string, retrie
if argc >= 4 {
deserializedRegex, err := regex.Deserialize(strings.Join(args[2:], " "))
if err != nil {
- r.server.sendServerMessage(dlog.Server.Error(r.server.user, commandParseWarning, err))
+ r.server.send(r.server.serverMessages, dlog.Server.Error(r.server.user, commandParseWarning, err))
return
}
re = deserializedRegex
}
if argc < 3 {
- r.server.sendServerWarnMessage(dlog.Server.Warn(r.server.user, commandParseWarning, args, argc))
+ r.server.send(r.server.serverMessages, dlog.Server.Warn(r.server.user, commandParseWarning, args, argc))
return
}
r.readGlob(ctx, args[1], re, retries)
@@ -58,7 +58,7 @@ func (r *readCommand) readGlob(ctx context.Context, glob string, re regex.Regex,
if numPaths := len(paths); numPaths == 0 {
dlog.Server.Error(r.server.user, "No such file(s) to read", glob)
- r.server.sendServerWarnMessage(dlog.Server.Warn(r.server.user, "Unable to read file(s), check server logs"))
+ r.server.send(r.server.serverMessages, dlog.Server.Warn(r.server.user, "Unable to read file(s), check server logs"))
select {
case <-ctx.Done():
return
@@ -72,7 +72,7 @@ func (r *readCommand) readGlob(ctx context.Context, glob string, re regex.Regex,
return
}
- r.server.sendServerWarnMessage(dlog.Server.Warn(r.server.user, "Giving up to read file(s)"))
+ r.server.send(r.server.serverMessages, dlog.Server.Warn(r.server.user, "Giving up to read file(s)"))
return
}
@@ -93,7 +93,7 @@ func (r *readCommand) readFileIfPermissions(ctx context.Context, wg *sync.WaitGr
if !r.server.user.HasFilePermission(path, "readfiles") {
dlog.Server.Error(r.server.user, "No permission to read file", path, globID)
- r.server.sendServerWarnMessage(dlog.Server.Warn(r.server.user, "Unable to read file(s), check server logs"))
+ r.server.send(r.server.serverMessages, dlog.Server.Warn(r.server.user, "Unable to read file(s), check server logs"))
return
}
@@ -161,6 +161,6 @@ func (r *readCommand) makeGlobID(path, glob string) string {
return pathParts[len(pathParts)-1]
}
- r.server.sendServerWarnMessage(dlog.Server.Warn("Empty file path given?", path, glob))
+ r.server.send(r.server.serverMessages, dlog.Server.Warn("Empty file path given?", path, glob))
return ""
}
diff --git a/internal/server/handlers/serverhandler.go b/internal/server/handlers/serverhandler.go
index b664566..ace2626 100644
--- a/internal/server/handlers/serverhandler.go
+++ b/internal/server/handlers/serverhandler.go
@@ -15,8 +15,8 @@ import (
"github.com/mimecast/dtail/internal"
"github.com/mimecast/dtail/internal/config"
- "github.com/mimecast/dtail/internal/io/line"
"github.com/mimecast/dtail/internal/io/dlog"
+ "github.com/mimecast/dtail/internal/io/line"
"github.com/mimecast/dtail/internal/io/pool"
"github.com/mimecast/dtail/internal/mapr/server"
"github.com/mimecast/dtail/internal/omode"
@@ -46,6 +46,7 @@ type ServerHandler struct {
activeCommands int32
quiet bool
spartan bool
+ serverless bool
readBuf bytes.Buffer
writeBuf bytes.Buffer
}
@@ -99,6 +100,12 @@ func (h *ServerHandler) Read(p []byte) (n int, err error) {
return
}
+ if h.serverless {
+ // In serverless mode we have logged the server message already via the
+ // dlog logger, no need to send the message again to the client part.
+ return
+ }
+
// Handle normal server message (display to the user)
h.readBuf.WriteString("SERVER")
h.readBuf.WriteString(protocol.FieldDelimiter)
@@ -266,23 +273,24 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args []
splitted := strings.Split(args[0], ":")
commandName := splitted[0]
- options, err := readOptions(splitted[1:])
+ options, err := config.DeserializeOptions(splitted[1:])
if err != nil {
- h.sendServerMessage(dlog.Server.Error(h.user, err))
+ h.send(h.serverMessages, dlog.Server.Error(h.user, err))
commandFinished()
return
}
- if quiet, ok := options["quiet"]; ok {
- if quiet == "true" {
- dlog.Server.Debug(h.user, "Enabling quiet mode")
- h.quiet = true
- }
+
+ if quiet, _ := options["quiet"]; quiet == "true" {
+ dlog.Server.Debug(h.user, "Enabling quiet mode")
+ h.quiet = true
}
- if spartan, ok := options["spartan"]; ok {
- if spartan == "true" {
- dlog.Server.Debug(h.user, "Enabling spartan mode")
- h.spartan = true
- }
+ if spartan, _ := options["spartan"]; spartan == "true" {
+ dlog.Server.Debug(h.user, "Enabling spartan mode")
+ h.spartan = true
+ }
+ if serverless, _ := options["serverless"]; serverless == "true" {
+ dlog.Server.Debug(h.user, "Enabling serverless mode")
+ h.serverless = true
}
switch commandName {
@@ -303,7 +311,7 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args []
case "map":
command, aggregate, err := newMapCommand(h, argc, args)
if err != nil {
- h.sendServerMessage(err.Error())
+ h.send(h.serverMessages, err.Error())
dlog.Server.Error(h.user, err)
commandFinished()
return
@@ -320,14 +328,16 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args []
commandFinished()
default:
- h.sendServerMessage(dlog.Server.Error(h.user, "Received unknown user command", commandName, argc, args, options))
+ h.send(h.serverMessages, dlog.Server.Error(h.user, "Received unknown user command", commandName, argc, args, options))
commandFinished()
}
}
func (h *ServerHandler) handleAckCommand(argc int, args []string) {
if argc < 3 {
- h.sendServerWarnMessage(dlog.Server.Warn(h.user, commandParseWarning, args, argc))
+ if !h.quiet {
+ h.send(h.serverMessages, dlog.Server.Warn(h.user, commandParseWarning, args, argc))
+ }
return
}
if args[1] == "close" && args[2] == "connection" {
@@ -346,23 +356,8 @@ func (h *ServerHandler) send(ch chan<- string, message string) {
}
}
-func (h *ServerHandler) sendServerMessage(message string) {
- h.send(h.serverMessageC(), message)
-}
-
-func (h *ServerHandler) sendServerWarnMessage(message string) {
- if h.quiet {
- return
- }
- h.send(h.serverMessageC(), message)
-}
-
-func (h *ServerHandler) serverMessageC() chan<- string {
- return h.serverMessages
-}
-
-func (h *ServerHandler) flushMessages() {
- dlog.Server.Debug(h.user, "flushMessages()")
+func (h *ServerHandler) flush() {
+ dlog.Server.Debug(h.user, "flush()")
unsentMessages := func() int {
return len(h.lines) + len(h.serverMessages) + len(h.maprMessages)
@@ -381,11 +376,11 @@ func (h *ServerHandler) flushMessages() {
func (h *ServerHandler) shutdown() {
dlog.Server.Debug(h.user, "shutdown()")
- h.flushMessages()
+ h.flush()
go func() {
select {
- case h.serverMessageC() <- ".syn close connection":
+ case h.serverMessages <- ".syn close connection":
case <-h.done.Done():
}
}()
@@ -408,31 +403,3 @@ func (h *ServerHandler) decrementActiveCommands() int32 {
atomic.AddInt32(&h.activeCommands, -1)
return atomic.LoadInt32(&h.activeCommands)
}
-
-func readOptions(opts []string) (map[string]string, error) {
- dlog.Server.Debug("Parsing options", opts)
- options := make(map[string]string, len(opts))
-
- for _, o := range opts {
- kv := strings.SplitN(o, "=", 2)
- if len(kv) != 2 {
- return options, fmt.Errorf("Unable to parse options: %v", kv)
- }
- key := kv[0]
- val := kv[1]
-
- if strings.HasPrefix(val, "base64%") {
- s := strings.SplitN(val, "%", 2)
- decoded, err := base64.StdEncoding.DecodeString(s[1])
- if err != nil {
- return options, err
- }
- val = string(decoded)
- }
-
- dlog.Server.Debug("Setting option", key, val)
- options[key] = val
- }
-
- return options, nil
-}