-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproxy.go
More file actions
80 lines (70 loc) · 1.65 KB
/
Copy pathproxy.go
File metadata and controls
80 lines (70 loc) · 1.65 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
72
73
74
75
76
77
78
79
80
package handler
import (
"net"
"sync"
"sync/atomic"
"time"
"github.com/cmd-stream/cmd-stream-go/core"
dlgt "github.com/cmd-stream/cmd-stream-go/delegate"
)
// Proxy implemets the core.Proxy interface.
type Proxy[T any] struct {
transport dlgt.ServerTransport[T]
flushFlag *uint32
mu *sync.Mutex
seq core.Seq
at time.Time
}
// At returns the time the Proxy received the command from the network.
func (p Proxy[T]) At() time.Time {
return p.at
}
// Seq returns the sequence number of the Command.
func (p Proxy[T]) Seq() core.Seq {
return p.seq
}
// LocalAddr returns the local network address.
func (p Proxy[T]) LocalAddr() net.Addr {
return p.transport.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (p Proxy[T]) RemoteAddr() net.Addr {
return p.transport.RemoteAddr()
}
// Send transmits a result to the client.
func (p Proxy[T]) Send(result core.Result) (n int, err error) {
p.mu.Lock()
n, err = p.transport.Send(p.seq, result)
p.mu.Unlock()
if err != nil {
return
}
return n, p.flush()
}
// SendWithDeadline transmits a result with a specified deadline.
func (p Proxy[T]) SendWithDeadline(deadline time.Time, result core.Result) (
n int, err error,
) {
p.mu.Lock()
err = p.transport.SetSendDeadline(deadline)
if err != nil {
p.mu.Unlock()
return
}
n, err = p.transport.Send(p.seq, result)
if err != nil {
p.mu.Unlock()
return
}
p.mu.Unlock()
return n, p.flush()
}
func (p Proxy[T]) flush() (err error) {
if swapped := atomic.CompareAndSwapUint32(p.flushFlag, 0, 1); swapped {
p.mu.Lock()
atomic.CompareAndSwapUint32(p.flushFlag, 1, 0)
err = p.transport.Flush()
p.mu.Unlock()
}
return
}