-
Notifications
You must be signed in to change notification settings - Fork 4
/
slot.go
60 lines (48 loc) · 1.03 KB
/
slot.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
package porthos
import (
"sync"
)
// Slot of a RPC call.
type Slot interface {
// ResponseChannel returns the response channel.
ResponseChannel() <-chan ClientResponse
// Dispose response resources.
Dispose()
// Correlation ID
GetCorrelationID() (string, error)
}
type slot struct {
responseChannel chan ClientResponse
mutex sync.Mutex
id string
}
func (slot *slot) GetCorrelationID() (string, error) {
var err error
if slot.id == "" {
slot.id, err = NewUUIDv4()
}
return slot.id, err
}
func (slot *slot) ResponseChannel() <-chan ClientResponse {
return slot.responseChannel
}
func (slot *slot) Dispose() {
slot.mutex.Lock()
defer slot.mutex.Unlock()
if slot.responseChannel != nil {
close(slot.responseChannel)
slot.responseChannel = nil
}
}
func (slot *slot) sendResponse(c ClientResponse) {
slot.mutex.Lock()
defer slot.mutex.Unlock()
if slot.responseChannel != nil {
slot.responseChannel <- c
}
}
func NewSlot() *slot {
return &slot{
responseChannel: make(chan ClientResponse),
}
}