doujiao3072 2018-01-28 05:40
浏览 22
已采纳

转到http侦听器,每秒钟更新一次数据

I'm trying to build a little website in Go containing a report based on data collected from a web service. It uses an API to query the service for data. The service can only be queried once every few seconds.

However, I have to query it a number of times to get the complete report data. Right now I just hammer the API to update my whole data structure each time the http handler (http.HandleFunc) is called. Of course, this is bad because it triggers lots of queries to the external API that are throttled. So, my report comes up very, very, very, slowly.

What I want to do instead is to have a function to updateReportData in a non-blocking way and store that data in some variable that the http.HandleFunc() can just ingest without calling the external API.

But, I'm very new to Go (and things like closures, semaphores, concurrency, etc) and so I'm not really sure how to build this. Should I be using channels? Should I use timers? How can I get the updateReportData to not block the http.HandleFunc, but still run on a fixed interval?

To sum up, I want to have a background routine update a data structure on a fixed interval and I want to be able to use http.HandleFunc to serve whatever data is in the data structure any time i make an http request to the program. I just have no idea how to start. Any advice would be appreciated.

  • 写回答

1条回答 默认 最新

  • doujujian0052 2018-01-28 05:44
    关注

    There are a few things you need to do:

    1. Create a background service that polls for the data. This service can run as a goroutine that periodically checks for new data.
    2. Create a channel that the background service uses to send new data to a centralized place to store the values. The background service should write data to this channel whenever it finds something new. Another option would be to protect the centralized data store with a mutex. Depending on the way the data is written and read, one option will be a better choice.
    3. Create a HTTP handler that returns the current contents of the centralized data store.

    Here is a simplified example showing how to use a goroutine and a sync.RWMutext to accomplish what you want:

    package main
    
    import (
        "fmt"
        "net/http"
        "sync"
        "time"
    )
    
    var (
        timeSumsMu sync.RWMutex
        timeSums   int64
    )
    
    func main() {
        // Start the goroutine that will sum the current time
        // once per second.
        go runDataLoop()
        // Create a handler that will read-lock the mutext and
        // write the summed time to the client
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            timeSumsMu.RLock()
            defer timeSumsMu.RUnlock()
            fmt.Fprint(w, timeSums)
        })
        http.ListenAndServe(":8080", nil)
    }
    
    func runDataLoop() {
        for {
            // Within an infinite loop, lock the mutex and
            // increment our value, then sleep for 1 second until
            // the next time we need to get a value.
            timeSumsMu.Lock()
            timeSums += time.Now().Unix()
            timeSumsMu.Unlock()
            time.Sleep(1 * time.Second)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行