聊聊storagetapper的pool

codecraft · · 438 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要研究一下storagetapper的pool

Thread

storagetapper/pool/pool.go

type Thread interface {
    Start(m uint, f func())
    Adjust(m uint)
    Terminate() bool
    NumProcs() uint
}
Thread接口定义了Start、Adjust、Terminate、NumProcs方法

pool

storagetapper/pool/pool.go

/*Create helps to hide poolImpl in the package, but not really required */
func Create() Thread {
    return &poolImpl{}
}

type poolImpl struct {
    mutex       sync.Mutex
    numProcs    uint
    maxNumProcs uint
    fn          func()
}

/*Start instantiates a pool of size of 'm' of 'f' goroutines */
/*Start and Create separation allows to pass pool instance to 'f' goroutine */
func (p *poolImpl) Start(m uint, f func()) {
    p.fn = f
    p.Adjust(m)
}

/*Adjust resizes the pool. It creates new threads if requested size is bigger
* then current size, while it assumes threads cooperation when requested size is
* smaller then current size. Threads should periodically call Terminate function
* and obey the result. */
func (p *poolImpl) Adjust(m uint) {
    p.mutex.Lock()
    defer p.mutex.Unlock()
    log.Debugf("Current size=%v, current maximum size=%v, requested size=%v", p.numProcs, p.maxNumProcs, m)
    p.maxNumProcs = m
    if p.numProcs < p.maxNumProcs {
        adj := p.maxNumProcs - p.numProcs
        shutdown.Register(int32(adj))
        for i := uint(0); i < adj; i++ {
            go func() { defer shutdown.Done(); p.fn() }()
        }
        p.numProcs = m
    }
}

/*Terminate return true if the caller thread need to terminate */
func (p *poolImpl) Terminate() bool {
    //Uncomment if Terminate is called frequently
    //Introduces a race when thread can miss Pool resize event, that's ok, so as
    //some other threads may see the event, or we will see it on the next
    //iteration
    //    if p.numProcs <= p.maxNumProcs {
    //        return false
    //    }

    p.mutex.Lock()
    defer p.mutex.Unlock()

    if p.numProcs > p.maxNumProcs {
        p.numProcs--
        log.Debugf("Terminating. Current size=%v, current maximum size=%v", p.numProcs, p.maxNumProcs)
        return true
    }

    return false
}

/*NumProcs return current size of the pool */
func (p *poolImpl) NumProcs() uint {
    p.mutex.Lock()
    defer p.mutex.Unlock()
    return p.numProcs
}
poolImpl定义了mutex、numProcs、maxNumProcs、fn属性;它实现了Thread接口,其Start方法设置了fn,同时执行Adjust方法;Adjust方法在numProcs小于maxNumProcs时会执行shutdown.Register,然后挨个执行shutdown.Done();Terminate方法对于numProcs大于maxNumProcs的情况递减numProcs

实例

storagetapper/pool/pool_test.go

func TestBasic(t *testing.T) {
    var m sync.Mutex
    var nProcs int32

    sig := make(chan bool)

    p := Create()

    if p.NumProcs() != 0 {
        t.Fatalf("Initially not zero")
    }

    p.Start(2, func() {
        m.Lock()
        atomic.AddInt32(&nProcs, 1)
        log.Debugf("Starting new proc, nProcs=%v", nProcs)
        m.Unlock()
        for !p.Terminate() {
            <-sig
            log.Debugf("Woken up")
        }
        m.Lock()
        atomic.AddInt32(&nProcs, -1)
        log.Debugf("Terminating proc, nProcs=%v", nProcs)
        m.Unlock()
    })

    /* Check that both real number and reported by thread pool equal to expected
    * value */
    waitFor(&nProcs, 2, 5, t)
    if p.NumProcs() != 2 {
        t.Fatalf("numProcs != 2")
    }

    p.Adjust(8)

    waitFor(&nProcs, 8, 5, t)
    if p.NumProcs() != 8 {
        t.Fatalf("numProcs != 8")
    }

    p.Adjust(3)

    for i := 0; i < 5; i++ {
        sig <- true
    }

    waitFor(&nProcs, 3, 5, t)
    if p.NumProcs() != 3 {
        t.Fatalf("numProcs != 3")
    }

    p.Adjust(0)
    for i := 0; i < 3; i++ {
        sig <- true
    }

    waitFor(&nProcs, 0, 5, t)
    if p.NumProcs() != 0 {
        t.Fatalf("numProcs != 0")
    }
}

小结

storagetapper的Thread接口定义了Start、Adjust、Terminate、NumProcs方法;poolImpl实现了Thread接口;其Adjust可以在numProcs小于maxNumProcs的时候进行扩容;Terminate会在numProcs大于maxNumProcs的时候递减numProcs。

doc


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊storagetapper的pool

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

438 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传