响应后异步工作

ugmeyewa  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(306)

我正在尝试实现http服务器:
用一些逻辑来计算
重定向用户
记录用户数据
目标是实现最大吞吐量(至少15k rps)。为了做到这一点,我想异步保存日志。我使用kafka作为日志系统,将代码块单独记录到单独的goroutine中。当前实施的总体示例:

package main

import (
    "github.com/confluentinc/confluent-kafka-go/kafka"
    "net/http"
    "time"
    "encoding/json"
)

type log struct {
    RuntimeParam  string `json:"runtime_param"`
    AsyncParam    string `json:"async_param"`
    RemoteAddress string `json:"remote_address"`
}

var (
    producer, _ = kafka.NewProducer(&kafka.ConfigMap{
        "bootstrap.servers": "localhost:9092,localhost:9093",
        "queue.buffering.max.ms": 1 * 1000,
        "go.delivery.reports": false,
        "client.id": 1,
    })
    topicName = "log"
)

func main() {
    siteMux := http.NewServeMux()
    siteMux.HandleFunc("/", httpHandler)
    srv := &http.Server{
        Addr: ":8080",
        Handler: siteMux,
        ReadTimeout:  2 * time.Second,
        WriteTimeout: 5 * time.Second,
        IdleTimeout:  10 * time.Second,
    }
    if err := srv.ListenAndServe(); err != nil {
        panic(err)
    }
}

func httpHandler(w http.ResponseWriter, r *http.Request) {
    handlerLog := new(log)
    handlerLog.RuntimeParam = "runtimeDataString"
    http.Redirect(w, r, "http://google.com", 301)
    go func(goroutineLog *log, request *http.Request) {
        goroutineLog.AsyncParam = "asyncDataString"
        goroutineLog.RemoteAddress = r.RemoteAddr
        jsonLog, err := json.Marshal(goroutineLog)
        if err == nil {
            producer.ProduceChannel() <- &kafka.Message{
                TopicPartition: kafka.TopicPartition{Topic: &topicName, Partition: kafka.PartitionAny},
                Value:          jsonLog,
            }
        }
    }(handlerLog, r)
}

问题是:
使用单独的goroutine来实现异步日志记录是正确的/有效的还是应该使用不同的方法(工人和渠道)
也许有一种方法可以进一步提高服务器的性能,但我没有找到?

6yt4nkrj

6yt4nkrj1#

是的,这是正确和有效地使用goroutine(正如flimzy在评论中指出的)。我完全同意,这是个好办法。
问题是,处理程序可能在goroutine开始处理所有内容之前完成执行,请求(即指针)可能会消失,或者您可能会在中间件堆栈中遇到一些冲突。我看了你的评论,这不是你的情况,但一般来说,你不应该把请求传递给一个goroutine。正如我从你的代码中看到的,你实际上只使用了 RemoteAddr 为什么不直接重定向并将日志记录放在defer语句中?所以,我要重写一下你的处理程序:

func httpHandler(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, "http://google.com", 301)
    defer func(runtimeDataString, RemoteAddr string) {
            handlerLog := new(log)
            handlerLog.RuntimeParam = runtimeDataString
            handlerLog.AsyncParam = "asyncDataString"
            handlerLog.RemoteAddress = RemoteAddr
            jsonLog, err := json.Marshal(handlerLog)
            if err == nil {
                producer.ProduceChannel() <- &kafka.Message{
                    TopicPartition: kafka.TopicPartition{Topic: &topicName, Partition: kafka.PartitionAny},
                    Value:          jsonLog,
                }
            }
        }("runtimeDataString", r.RemoteAddr)
}

goroutines不太可能提高服务器的性能,因为您只是提前发送响应,而那些kafka连接可能会在后台堆积起来,降低整个服务器的速度。如果您发现这是一个瓶颈,您可以考虑在本地保存日志,并将它们发送到服务器之外的另一个进程(或工作池)中的kafka。这可能会随着时间的推移而分散工作负载(比如当您有更多请求时发送更少的日志,反之亦然)。

相关问题