forked from funny-falcon/go-iproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
97 lines (80 loc) · 1.8 KB
/
response.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package iproto
import "sync"
// RetCode is a iproto return code, which lays in first bytes of response
type RetCode uint32
// Response return codes
// RcOK - good answer
// RcTimeout - response where timeouted by ServiceWithDeadline
// RcShortBody - response with body shorter, than return code
// RcIOError - socket were disconnected before answere arrives
// RcCanceled - ...
const (
RcOK = RetCode(0)
RcTemporary = RetCode(1)
RcFatal = RetCode(2)
RcInternal = RetCode(3)
RcKindMask = RetCode(7)
)
const (
RcShutdown = RetCode(0xff03)
RcProtocolError = RetCode(0x0302)
RcInternalError = RetCode(0xfc02)
)
const (
RcCanceled = RetCode(0xff03)
RcIOError = RetCode(0xfe03)
RcTimeout = RetCode(0xfd03)
)
type Response struct {
Msg RequestType
Id uint32
Code RetCode
Body Body
}
func (res *Response) Valid() bool {
return res.Code&RcKindMask == 0
}
func (res *Response) Restartable() bool {
return res.Code&RcKindMask == RcTemporary
}
type Responder interface {
Respond(*Response)
}
type Callback func(*Response)
func (f Callback) Respond(r *Response) {
f(r)
}
type Chan chan *Response
func (ch Chan) Respond(r *Response) {
ch <- r
}
type RequestBookmark interface {
Respond(*Response)
setReq(req *Request, self RequestBookmark)
unchain() RequestBookmark
}
type Bookmark struct {
Request *Request
prev RequestBookmark
sync.Mutex
}
// Chain integrates Bookmark into callback chain
func (r *Bookmark) setReq(req *Request, self RequestBookmark) {
r.Lock()
r.Request = req
r.prev = req.chain
req.chain = self
r.Unlock()
}
// Unchain removes Bookmark from callback chain
func (r *Bookmark) unchain() (prev RequestBookmark) {
r.Lock()
prev = r.prev
r.Request.chain = prev
r.prev = nil
r.Request = nil
r.Unlock()
return
}
func (r *Bookmark) Respond(resp *Response) {
}