source: code/trunk/vendor/github.com/valyala/fasthttp/stackless/func.go@ 145

Last change on this file since 145 was 145, checked in by Izuru Yakumo, 22 months ago

Updated the Makefile and vendored depedencies

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 1.6 KB
Line 
1package stackless
2
3import (
4 "runtime"
5 "sync"
6)
7
8// NewFunc returns stackless wrapper for the function f.
9//
10// Unlike f, the returned stackless wrapper doesn't use stack space
11// on the goroutine that calls it.
12// The wrapper may save a lot of stack space if the following conditions
13// are met:
14//
15// - f doesn't contain blocking calls on network, I/O or channels;
16// - f uses a lot of stack space;
17// - the wrapper is called from high number of concurrent goroutines.
18//
19// The stackless wrapper returns false if the call cannot be processed
20// at the moment due to high load.
21func NewFunc(f func(ctx interface{})) func(ctx interface{}) bool {
22 if f == nil {
23 panic("BUG: f cannot be nil")
24 }
25
26 funcWorkCh := make(chan *funcWork, runtime.GOMAXPROCS(-1)*2048)
27 onceInit := func() {
28 n := runtime.GOMAXPROCS(-1)
29 for i := 0; i < n; i++ {
30 go funcWorker(funcWorkCh, f)
31 }
32 }
33 var once sync.Once
34
35 return func(ctx interface{}) bool {
36 once.Do(onceInit)
37 fw := getFuncWork()
38 fw.ctx = ctx
39
40 select {
41 case funcWorkCh <- fw:
42 default:
43 putFuncWork(fw)
44 return false
45 }
46 <-fw.done
47 putFuncWork(fw)
48 return true
49 }
50}
51
52func funcWorker(funcWorkCh <-chan *funcWork, f func(ctx interface{})) {
53 for fw := range funcWorkCh {
54 f(fw.ctx)
55 fw.done <- struct{}{}
56 }
57}
58
59func getFuncWork() *funcWork {
60 v := funcWorkPool.Get()
61 if v == nil {
62 v = &funcWork{
63 done: make(chan struct{}, 1),
64 }
65 }
66 return v.(*funcWork)
67}
68
69func putFuncWork(fw *funcWork) {
70 fw.ctx = nil
71 funcWorkPool.Put(fw)
72}
73
74var funcWorkPool sync.Pool
75
76type funcWork struct {
77 ctx interface{}
78 done chan struct{}
79}
Note: See TracBrowser for help on using the repository browser.