-
Notifications
You must be signed in to change notification settings - Fork 56
/
proxy.go
163 lines (147 loc) · 3.31 KB
/
proxy.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2020 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zmq4
import (
"context"
"fmt"
"log"
"sync"
"golang.org/x/sync/errgroup"
)
// Proxy connects a frontend socket to a backend socket.
type Proxy struct {
ctx context.Context // life-line of proxy
grp *errgroup.Group
cmds chan proxyCmd
}
type proxyCmd byte
const (
proxyStats proxyCmd = iota
proxyPause
proxyResume
proxyKill
)
// NewProxy creates a new Proxy value.
// It proxies messages received on the frontend to the backend (and vice versa)
// If capture is not nil, messages proxied are also sent on that socket.
//
// Conceptually, data flows from frontend to backend. Depending on the
// socket types, replies may flow in the opposite direction.
// The direction is conceptual only; the proxy is fully symmetric and
// there is no technical difference between frontend and backend.
//
// Before creating a Proxy, users must set any socket options,
// and Listen or Dial both frontend and backend sockets.
func NewProxy(ctx context.Context, front, back, capture Socket) *Proxy {
grp, ctx := errgroup.WithContext(ctx)
proxy := Proxy{
ctx: ctx,
grp: grp,
cmds: make(chan proxyCmd),
}
proxy.init(front, back, capture)
return &proxy
}
func (p *Proxy) Pause() { p.cmds <- proxyPause }
func (p *Proxy) Stats() { p.cmds <- proxyStats }
func (p *Proxy) Resume() { p.cmds <- proxyResume }
func (p *Proxy) Kill() { p.cmds <- proxyKill }
// Run runs the proxy loop.
func (p *Proxy) Run() error {
return p.grp.Wait()
}
func (p *Proxy) init(front, back, capture Socket) {
canRecv := func(sck Socket) bool {
switch sck.Type() {
case Push:
return false
default:
return true
}
}
canSend := func(sck Socket) bool {
switch sck.Type() {
case Pull:
return false
default:
return true
}
}
type Pipe struct {
name string
dst Socket
src Socket
}
var (
quit = make(chan struct{})
pipes = []Pipe{
{
name: "backend",
dst: back,
src: front,
},
{
name: "frontend",
dst: front,
src: back,
},
}
)
// workers makes sure all goroutines are launched and scheduled.
var workers sync.WaitGroup
workers.Add(len(pipes) + 1)
for i := range pipes {
pipe := pipes[i]
if pipe.src == nil || !canRecv(pipe.src) {
workers.Done()
continue
}
p.grp.Go(func() error {
workers.Done()
canSend := canSend(pipe.dst)
for {
msg, err := pipe.src.Recv()
select {
case <-p.ctx.Done():
return p.ctx.Err()
case <-quit:
return nil
default:
if canSend {
err = pipe.dst.Send(msg)
if err != nil {
log.Printf("could not forward to %s: %+v", pipe.name, err)
continue
}
}
if err == nil && capture != nil && len(msg.Frames) != 0 {
_ = capture.Send(msg)
}
}
}
})
}
p.grp.Go(func() error {
workers.Done()
for {
select {
case <-p.ctx.Done():
return p.ctx.Err()
case cmd := <-p.cmds:
switch cmd {
case proxyPause, proxyResume, proxyStats:
// TODO
case proxyKill:
close(quit)
return nil
default:
// API error. panic.
panic(fmt.Errorf("invalid control socket command: %v", cmd))
}
}
}
})
// wait for all worker routines to be scheduled.
workers.Wait()
}