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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面