duanjian5059 2014-03-26 00:53
浏览 272
已采纳

计算在Go中调用(请求处理程序)函数的次数

Context

I'm making a web app that serves dynamically generated pdfs. These contain content from the internet, so every time it serves a pdf, it downloads a number of files to a new temporary folder.

The Problem

I end up with a large number of folders after I load the page once, so it seems that, for some reason, the handler is being called multiple times, which is an issue because I'm downloading multiple times more than I need to of not insubstantial files. I'd like to check at what stage of the process multiple requests are occurring.

The Question

Is there a way of working out how many times a function has been called, quite possibly using closures? (I haven't quite got closures into my mental model for programming yet; I don't completely understand them/how they're used). This would preferably be something involving an int in the language rather than printing something at every stage and counting by hand - I'm looking for a more scalable solution than that (for later situations as well as this one).

Thanks!

  • 写回答

2条回答 默认 最新

  • du3669 2014-03-26 01:10
    关注

    Here are two ways you can count function calls, and one for method calls. There are plenty of other ways too, but just to get you started:

    Using closure: (not what I would recommended)

    package main

    import(
        "fmt"
        "sync/atomic"
    )
    
    var Foo = func() (func() uint64) {
        var called uint64
        return func() uint64 {
            atomic.AddUint64(&called, 1)
            fmt.Println("Foo!")
            return called
        }
    }()
    
    func main() {
        Foo()
        c := Foo()
        fmt.Printf("Foo() is called %d times
    ", c)
    }
    

    Playground: http://play.golang.org/p/euKbamdI7h

    Using global counter:

    package main
    
    import (
        "fmt"
        "sync/atomic"
    )
    
    var called uint64
    
    func Foo() {
        atomic.AddUint64(&called, 1)
        fmt.Println("Foo!");
    }
    
    func main() {
        Foo()
        Foo()
        fmt.Printf("Foo() is called %d times
    ", called)
    }
    

    Playground: http://play.golang.org/p/3Ib29VCnoF

    Counting method calls:

    package main
    
    import (
        "fmt"
        "sync/atomic"
    )
    
    type T struct {
        Called uint64
    }
    
    func (t *T) Foo() {
        atomic.AddUint64(&t.Called, 1)
        fmt.Println("Foo!")
    }
    
    func main() {
        var obj T
        obj.Foo()
        obj.Foo()
        fmt.Printf("obj.Foo() is called %d times
    ", obj.Called)
    }
    

    Playground: http://play.golang.org/p/59eOQdUQU1

    Edit:

    I just realized that the handler might not be in your own package. In such a case, you might want to write a wrapper:

    var called uint64
    
    func Foo() {
        atomic.AddUint64(&called, 1)
        importedPackage.Foo()
    }
    

    Edit 2:

    Updated the examples to use atomic +1 operations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器