-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
71 lines (61 loc) · 2.04 KB
/
context.go
File metadata and controls
71 lines (61 loc) · 2.04 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* This file is part of GridWorker.
*
* Copyright (c) 2018 Mocha Industries, LLC.
* All rights reserved.
*
* GridWorker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GridWorker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GridWorker. If not, see <https://www.gnu.org/licenses/>.
*/
package gridworker
// Context is used to pass information and give responses in
// worker tasks
type Context struct {
// Response is a chan that the worker responds with
Response chan *Message
// input is the base input Message for the worker
input *Message
// reciept is the task receipt for the context
reciept *Reciept
// closed is used to determine if the task has sent it's final message
closed bool
// process pool is a reference to the process pool
processPool *processPool
}
// contextPool is a struct that allows us to get context objects
// out of a pre alloced pool rather than reallocing every time
type contextPool struct {
// pool is the actual context pool
pool *limitPool
}
// newContextPool creates a new contextPool for the processPool
func (p *processPool) newContextPool() {
p.contextPool = &contextPool{
pool: newLimitPool(contextPoolLimit, func() interface{} {
return &Context{
Response: make(chan *Message, 1),
processPool: p,
}
}),
}
}
// newContext gets a new context object from the context pool
func (c *contextPool) newContext() *Context {
return c.pool.get().(*Context)
}
// release returns the context to the context pool
func (c *Context) release() {
c.input = nil
c.closed = false
c.processPool.contextPool.pool.put(c)
}