duan19850312 2013-08-03 14:20
浏览 72

Go中的磁盘备份阵列

How can I create a disk-based delayed queue in Go?

I am writing a Go program to take certain actions on data after certain time intervals. Here is a stripped down version.

func IncomingJob (data MyStruct) {
    //Run immediately
    dosomething(&data, 1)
    time.Sleep(5 * time.Minute)
    //Run after 5 minutes has passed from initial arrival
    dosomething(&data, 2)
    time.Sleep(5 * time.Minute)
    //Run after 10 minutes has passed from initial arrival
    dosomething(&data, 3)
    time.Sleep(50 * time.Minute)
    //Run after 60 minutes has passed from initial arrival
    dosomething(&data, 4)
}

This func would be initialized as a goroutinue.

go IncomingJob(data)

The problem I am having is that each instance of IncomingJob runs for 60 minutes and the MyStruct object stays in memory for that duration. The total memory usage is pretty huge. If I want to support 1 million runs per hour, that means at any given time, there are 1 million MyStruct objects in memory waiting, doing nothing. I can use time.AfterFunc for this as well, but that doesn't change anything with respect to memory usage.

Is there some disk backed FIFO queue/buffer implementation in Go? That way I could have 3 queues (for different intervals) and poll it and sleep until its the correct time for the polled data to be reprocessed. This way I would lose some CPU cycles in serialization, add I/O latency to an otherwise in-memory application but save a massive amount of memory.

Update: time.AfterFunc uses a lot less memory.

func IncomingJob (data MyStruct) {
    //Run immediately
    dosomething(&data, 1)
    time.AfterFunc(5 * time.Minute, func() {
        //Run after 5 minutes has passed from initial arrival
        dosomething(&data, 2)
        time.AfterFunc(5 * time.Minute, func() {
            //Run after 10 minutes has passed from initial arrival
            dosomething(&data, 3)
        })
            time.AfterFunc(50 * time.Minute, func() {
                //Run after 50 minutes has passed from initial arrival
                dosomething(&data, 4)
        })
    })
}
  • 写回答

1条回答 默认 最新

  • dopnpoh056622 2013-08-03 14:48
    关注

    Sounds like a job for an embedded database. I'm sure a FIFO can be modelled pretty easily by eg. a NoSQL key/value store.

    You might want to have a look at kv (author here). Catch: Values are limited to 64k, so you might need to compose separate FIFO items out of more than one K/V pair.

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题