-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
64 lines (55 loc) · 1.23 KB
/
agent.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
55
56
57
58
59
60
61
62
63
64
package mooncake
import (
"fmt"
"log"
"reflect"
"sync"
)
var (
ErrNoCallsRegistered = func(key string) error {
return fmt.Errorf("no calls registered for %s", key)
}
)
type MooncakeAgent struct {
mutex sync.Mutex
queue map[string]*AgentController
}
func NewAgent() *MooncakeAgent {
ma := new(MooncakeAgent)
ma.CleanQueue()
return ma
}
func (ma *MooncakeAgent) CleanQueue() *MooncakeAgent {
ma.queue = map[string]*AgentController{}
return ma
}
func (ma *MooncakeAgent) SetCall(key string, typeImpl reflect.Type) *AgentController {
ma.mutex.Lock()
defer ma.mutex.Unlock()
var agentTo AgentController
if v, has := ma.queue[key]; !has {
ma.queue[key] = new(AgentController)
agentTo = NewAgentController(key, typeImpl)
} else {
v.lifeTimeCount++
v.lifeTime = LT_REPEAT
agentTo = *v
}
ma.queue[key] = &agentTo
return &agentTo
}
func (ma *MooncakeAgent) GetCall(key string) []ReturnDetail {
ma.mutex.Lock()
defer ma.mutex.Unlock()
if v, has := ma.queue[key]; has {
valueToReturn := v.returnValues
v.lifeTimeCount--
if v.lifeTimeCount <= 0 && v.lifeTime != LT_ANY_TIME {
delete(ma.queue, key)
}
return valueToReturn
} else {
log.Fatalln(ErrNoCallsRegistered(key).Error())
}
return []ReturnDetail{}
}