-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
55 lines (44 loc) · 1.11 KB
/
message.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
package gearman // import "github.com/nathanaelle/gearman/v2"
import (
"sync"
"github.com/nathanaelle/gearman/v2/protocol"
)
type (
// PacketEmiter is a interface to send packet to a socket pool
PacketEmiter interface {
Send(...protocol.Packet)
}
// Message is the structure for communication between a pool and a Client or a Worker
Message struct {
Reply PacketEmiter
Server Conn
Pkt protocol.Packet
}
chanPacketEmiter struct {
c chan<- protocol.Packet
}
funcPacketEmiter struct {
lock *sync.Mutex
packetEmiter func(protocol.Packet, PacketEmiter)
}
)
var _ PacketEmiter = chanPacketEmiter{}
var _ PacketEmiter = &funcPacketEmiter{}
func newChanPacketEmiter(c chan<- protocol.Packet) PacketEmiter {
return chanPacketEmiter{c}
}
func (cpe chanPacketEmiter) Send(pkts ...protocol.Packet) {
for _, pkt := range pkts {
cpe.c <- pkt
}
}
func newFuncPacketEmiter(pe func(protocol.Packet, PacketEmiter)) PacketEmiter {
return &funcPacketEmiter{
packetEmiter: pe,
}
}
func (fpe *funcPacketEmiter) Send(pkts ...protocol.Packet) {
for _, pkt := range pkts {
fpe.packetEmiter(pkt, fpe)
}
}