我正在尝试使用go-json-rest
在golang中编写REST服务该服务的目的只是将收到的数据转换为CSV并进行记录。 由于负载可能很重,我想使用goroutines进行日志记录。 目前我已经创建了四个LogWorkers(goroutine) 每个goroutine都会将CSV记录到单独的文件中。
当我执行代码时,始终从最后一个goroutine触发日志。我看到在我的日志文件夹中创建的单个文件来自第四个例程。
这是我的服务器代码
package main
import (
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
"strconv"
"time"
)
const workerCount = 4
var evChannel = make(chan Event)
var workers = make([]*LogWorker, workerCount)
const maxLogFileSize = 100 // In MB
const maxLogFileBackups = 30
const maxLogFileAge = 5
const logFileName = "/home/sam/tmp/go_logs/event_"
func main() {
// Initialize workers
// Four workers is being created
for i := 0; i < workerCount; i++ {
var fileName = logFileName + strconv.Itoa(i)
workers[i] = NewLogWorker(fileName, maxLogFileSize, maxLogFileBackups, maxLogFileAge)
go workers[i].Work(evChannel)
}
// Initialize REST API
api := rest.NewApi()
//api.Use(rest.DefaultDevStack...)
api.Use(rest.DefaultCommonStack...)
router, err := rest.MakeRouter(
rest.Post("/events", StoreEvents),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":4545", api.MakeHandler()))
}
func StoreEvents(w rest.ResponseWriter, r *rest.Request) {
event := Event{}
err := r.DecodeJsonPayload(&event)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO : Add validation if needed
// Add code to parse the request and add further information to event
// log.Println()
select {
case evChannel <- event:
case <- time.After(5 * time.Second):
// throw away the message, so sad
}
// evChannel <- event
//log.Println(Csv(event))
w.WriteHeader(http.StatusOK)
}
这是我的工人代码
package main
import (
"gopkg.in/natefinch/lumberjack.v2"
"log"
"fmt"
)
type LogWorker struct {
FileName string
MaxSize int // In megabytes
MaxBackups int // No of backups per worker
MaxAge int // maximum number of days to retain old log files
}
func NewLogWorker(fileName string, maxSize int, maxBackups int, maxAge int) (lw *LogWorker) {
return &LogWorker {fileName, maxSize, maxBackups, maxAge}
}
func (lw *LogWorker) Work(evChannel chan Event) {
fmt.Println(lw.FileName)
log.SetOutput(&lumberjack.Logger {
Filename: lw.FileName,
MaxSize: lw.MaxSize,
MaxBackups: lw.MaxBackups,
MaxAge: lw.MaxAge,
})
log.SetFlags(0)
for {
event := <- evChannel
log.Println(Csv(event))
}
}
请注意,event是一个包含一些字符串字段的结构。 在SO中已经存在类似的问题。当我尝试在playground中执行goroutine时,它仍会打印上次执行例程中的值。提供的答案有一些等待。完成。由于我的工人需要持续运行,我不认为我可以使用它。
请帮我找出为什么我的所有goroutines(LogWorkers)都没有使用?
答案 0 :(得分:4)
您正在每个goroutine中设置日志包的默认全局记录器输出。
你可能想做更多的事情:
func (lw *LogWorker) Work(evChannel chan Event) {
fmt.Println(lw.FileName)
lg := log.New(&lumberjack.Logger {
Filename: lw.FileName,
MaxSize: lw.MaxSize,
MaxBackups: lw.MaxBackups,
MaxAge: lw.MaxAge,
}, "", 0)
for {
event := <- evChannel
lg.Println(Csv(event))
}
}
这将为每个goroutine提供一个记录器。
在你的版本中,你可能只有最后一个执行(可能是最后一个生成的,但不能保证)
为了进一步改进,您可能还希望将for循环写为:
for event := range evChannel {
lg.Println(Csv(event))
}
这样,它将在通道关闭时终止goroutine,而不是旋转来自封闭通道的空值。 fetchLimit以供参考