Files
mostlymatter/vendor/github.com/rudderlabs/analytics-go/executor.go
George Goldberg 6cabc40e62 MM-23568: Add rudder to server diagnostics. (#14151)
* MM-23568: Add rudder to server diagnostics.

* Add unit test.

* Go mod tidy.

* CSP Header fix.

* Fix review comments.

* Update web/handlers.go

Co-Authored-By: Jesse Hallam <jesse.hallam@gmail.com>

* Partially address review comments.

* fix tests.

* Finish implementing review suggestions and then fixing tests.

* Fix CSP Header tests.

Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
2020-04-21 09:23:00 +01:00

54 строки
675 B
Go

package analytics
import "sync"
type executor struct {
queue chan func()
mutex sync.Mutex
size int
cap int
}
func newExecutor(cap int) *executor {
e := &executor{
queue: make(chan func(), 1),
cap: cap,
}
go e.loop()
return e
}
func (e *executor) do(task func()) (ok bool) {
e.mutex.Lock()
if e.size != e.cap {
e.queue <- task
e.size++
ok = true
}
e.mutex.Unlock()
return
}
func (e *executor) close() {
close(e.queue)
}
func (e *executor) loop() {
for task := range e.queue {
go e.run(task)
}
}
func (e *executor) run(task func()) {
defer e.done()
task()
}
func (e *executor) done() {
e.mutex.Lock()
e.size--
e.mutex.Unlock()
}