聊聊storagetapper的pool

本文主要研究一下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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值