From b6cc6a360646d54b140e2730c4172a31f7bf7700 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Thu, 1 Jul 2021 21:22:03 +0300 Subject: [PATCH] Add a way to add process ID to file names When you use S3 and write from multiple machines, you need to ensure that file will have unique name for each machine. This PR adds special %i function which you can use in file templates, which will add some random process uniq string to the file. --- output_file.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/output_file.go b/output_file.go index c329ccc..b87827e 100644 --- a/output_file.go +++ b/output_file.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "log" + "math/rand" "os" "path/filepath" "runtime/debug" @@ -19,6 +20,21 @@ import ( "github.com/buger/goreplay/size" ) +var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") +var instanceID string + +func init() { + instanceID = randSeq(8) +} + +func randSeq(n int) string { + b := make([]rune, n) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + return string(b) +} + var dateFileNameFuncs = map[string]func(*FileOutput) string{ "%Y": func(o *FileOutput) string { return time.Now().Format("2006") }, "%m": func(o *FileOutput) string { return time.Now().Format("01") }, @@ -29,6 +45,7 @@ var dateFileNameFuncs = map[string]func(*FileOutput) string{ "%NS": func(o *FileOutput) string { return fmt.Sprint(time.Now().Nanosecond()) }, "%r": func(o *FileOutput) string { return string(o.currentID) }, "%t": func(o *FileOutput) string { return string(o.payloadType) }, + "%i": func(o *FileOutput) string { return instanceID }, } // FileOutputConfig ...