-
Notifications
You must be signed in to change notification settings - Fork 495
/
scope_pool.go
54 lines (43 loc) · 1.14 KB
/
scope_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package stateful
import "sync"
// ScopePool - pooling mechanism for Scope
// The idea behind scope pool is to pool scopes and to put them only
// the needed variables for execution.
type ScopePool interface {
Get() *Scope
Put(scope *Scope)
ReferenceVariables() []string
}
type scopePool struct {
referenceVariables []string
pool sync.Pool
}
// NewScopePool - creates new ScopePool for the given Node
func NewScopePool(referenceVariables []string) ScopePool {
scopePool := &scopePool{
referenceVariables: referenceVariables,
}
scopePool.pool = sync.Pool{
New: func() interface{} {
scope := NewScope()
for _, refVariable := range scopePool.referenceVariables {
scope.Set(refVariable, empty)
}
return scope
},
}
return scopePool
}
func (s *scopePool) ReferenceVariables() []string {
return s.referenceVariables
}
// Get - returns a scope from a pool with the needed reference variables
// (with nil values/old values) in the scope
func (s *scopePool) Get() *Scope {
return s.pool.Get().(*Scope)
}
// Put - put used scope back to the pool
func (s *scopePool) Put(scope *Scope) {
scope.Reset()
s.pool.Put(scope)
}