Skip to content

Commit 8df13e2

Browse files
committed
version 1.3.1: bugfix in etcd connexion: too many goroutines were launched
1 parent 8d4b74f commit 8df13e2

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SSHPROXY_VERSION ?= 1.3.0
1+
SSHPROXY_VERSION ?= 1.3.1
22
SSHPROXY_GIT_URL ?= github.com/cea-hpc/sshproxy
33

44
prefix ?= /usr

cmd/sshproxy/recorder.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
"github.com/cea-hpc/sshproxy/pkg/record"
2424
"github.com/cea-hpc/sshproxy/pkg/utils"
25+
26+
"go.etcd.io/etcd/clientv3"
2527
)
2628

2729
// Dup duplicates a []byte slice.
@@ -100,6 +102,7 @@ type Recorder struct {
100102
dumpfile string // path to filename where the raw records are dumped
101103
dumpLimitSize uint64 // number of bytes beyond which records are no longer dumped
102104
dumpLimitWindow time.Duration // time window in which dump size is accounted
105+
leaseID clientv3.LeaseID // etcd lease ID used for updating stats
103106
writer *record.Writer // *record.Writer where the raw records are dumped
104107
}
105108

@@ -108,7 +111,7 @@ type Recorder struct {
108111
// If dumpfile is not empty, the intercepted raw data will be written in this
109112
// file. Logging of basic statistics will be done every logStatsInterval seconds. Bandwidth will be updated in etcd every etcdStatsInterval seconds.
110113
// It will stop recording when the context is cancelled.
111-
func NewRecorder(conninfo *ConnInfo, dumpfile, command string, etcdStatsInterval time.Duration, logStatsInterval time.Duration, dumpLimitSize uint64, dumpLimitWindow time.Duration) *Recorder {
114+
func NewRecorder(conninfo *ConnInfo, dumpfile, command string, etcdStatsInterval time.Duration, logStatsInterval time.Duration, dumpLimitSize uint64, dumpLimitWindow time.Duration, leaseID clientv3.LeaseID) *Recorder {
112115
ch := make(chan record.Record)
113116

114117
return &Recorder{
@@ -125,27 +128,19 @@ func NewRecorder(conninfo *ConnInfo, dumpfile, command string, etcdStatsInterval
125128
dumpfile: dumpfile,
126129
dumpLimitSize: dumpLimitSize,
127130
dumpLimitWindow: dumpLimitWindow,
131+
leaseID: leaseID,
128132
writer: nil,
129133
}
130134
}
131135

132136
// updateStats writes the bandwidth to etcd
133-
func (r *Recorder) updateStats(ctx context.Context, cli *utils.Client, etcdPath string) {
137+
func (r *Recorder) updateStats(cli *utils.Client, etcdPath string) {
134138
if cli != nil {
135139
if cli.IsAlive() {
136-
keepAliveChan, err := cli.UpdateStats(ctx, etcdPath, r.bandwidth)
140+
err := cli.UpdateStats(etcdPath, r.bandwidth, r.leaseID)
137141
if err != nil {
138142
log.Errorf("updating stats: %v", err)
139143
}
140-
go func() {
141-
for {
142-
select {
143-
case <-keepAliveChan:
144-
case <-ctx.Done():
145-
return
146-
}
147-
}
148-
}()
149144
}
150145
}
151146
}
@@ -245,7 +240,7 @@ func (r *Recorder) Run(ctx context.Context, cli *utils.Client, etcdPath string)
245240
for {
246241
select {
247242
case <-time.After(r.etcdStatsInterval):
248-
r.updateStats(ctx, cli, etcdPath)
243+
r.updateStats(cli, etcdPath)
249244
case <-ctx.Done():
250245
return
251246
}

cmd/sshproxy/sshproxy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/docker/docker/pkg/term"
3434
"github.com/op/go-logging"
35+
"go.etcd.io/etcd/clientv3"
3536
)
3637

3738
var (
@@ -351,11 +352,13 @@ func mainExitCode() int {
351352
}()
352353

353354
var etcdPath string
355+
var leaseID clientv3.LeaseID
354356
// Register destination in etcd and keep it alive while running.
355357
if cli != nil && cli.IsAlive() {
356358
key := fmt.Sprintf("%s@%s", username, service)
357-
keepAliveChan, eP, err := cli.SetDestination(ctx, key, sshInfos.Dst(), hostport)
359+
keepAliveChan, eP, lID, err := cli.SetDestination(ctx, key, sshInfos.Dst(), hostport)
358360
etcdPath = eP
361+
leaseID = lID
359362
if err != nil {
360363
log.Warningf("setting destination in etcd: %v", err)
361364
}
@@ -444,7 +447,7 @@ func mainExitCode() int {
444447

445448
var recorder *Recorder
446449
if config.Dump != "" {
447-
recorder = NewRecorder(conninfo, config.Dump, originalCmd, config.EtcdStatsInterval.Duration(), config.LogStatsInterval.Duration(), config.DumpLimitSize, config.DumpLimitWindow.Duration())
450+
recorder = NewRecorder(conninfo, config.Dump, originalCmd, config.EtcdStatsInterval.Duration(), config.LogStatsInterval.Duration(), config.DumpLimitSize, config.DumpLimitWindow.Duration(), leaseID)
448451

449452
wg.Add(1)
450453
go func() {

misc/sshproxy.spec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
%global debug_package %{nil}
44

55
Name: sshproxy
6-
Version: 1.3.0
6+
Version: 1.3.1
77
Release: 1%{?dist}
88
Summary: SSH proxy
99
License: CeCILL-B
@@ -51,6 +51,9 @@ install -p -m 0644 config/sshproxy.yaml %{buildroot}%{_sysconfdir}/sshproxy
5151
%{_mandir}/man8/sshproxy-replay.8*
5252

5353
%changelog
54+
* Wed Sep 23 2020 Cyril Servant <[email protected]> - 1.3.1-1
55+
- sshproxy 1.3.1
56+
5457
* Wed Aug 05 2020 Cyril Servant <[email protected]> - 1.3.0-1
5558
- sshproxy 1.3.0
5659

pkg/utils/etcd.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,58 +205,51 @@ func (c *Client) GetDestination(key string) (string, error) {
205205
}
206206

207207
// SetDestination set current destination in etcd.
208-
func (c *Client) SetDestination(rootctx context.Context, key, sshdHostport string, dst string) (<-chan *clientv3.LeaseKeepAliveResponse, string, error) {
208+
func (c *Client) SetDestination(rootctx context.Context, key, sshdHostport string, dst string) (<-chan *clientv3.LeaseKeepAliveResponse, string, clientv3.LeaseID, error) {
209209
path := fmt.Sprintf("%s/%s/%s/%s", toConnectionKey(key), dst, sshdHostport, time.Now().Format(time.RFC3339Nano))
210210
ctx, cancel := context.WithTimeout(context.Background(), c.requestTimeout)
211211
resp, err := c.cli.Grant(ctx, c.keyTTL)
212212
cancel()
213213
if err != nil {
214-
return nil, "", err
214+
return nil, "", 0, err
215215
}
216216

217217
bytes, err := json.Marshal(&Bandwidth{
218218
In: 0,
219219
Out: 0,
220220
})
221221
if err != nil {
222-
return nil, "", err
222+
return nil, "", 0, err
223223
}
224224
ctx, cancel = context.WithTimeout(context.Background(), c.requestTimeout)
225225
_, err = c.cli.Put(ctx, path, string(bytes), clientv3.WithLease(resp.ID))
226226
cancel()
227227
if err != nil {
228-
return nil, "", err
228+
return nil, "", 0, err
229229
}
230230

231231
k, e := c.cli.KeepAlive(rootctx, resp.ID)
232-
return k, path, e
232+
return k, path, resp.ID, e
233233
}
234234

235235
// UpdateStats updates the stats (bandwidth in and out in kB/s) of a connection.
236-
func (c *Client) UpdateStats(rootctx context.Context, etcdPath string, stats map[int]uint64) (<-chan *clientv3.LeaseKeepAliveResponse, error) {
236+
func (c *Client) UpdateStats(etcdPath string, stats map[int]uint64, leaseID clientv3.LeaseID) error {
237237
bytes, err := json.Marshal(&Bandwidth{
238238
In: int(stats[0] / 1024),
239239
Out: int((stats[1] + stats[2]) / 1024),
240240
})
241241
if err != nil {
242-
return nil, err
242+
return err
243243
}
244244

245245
ctx, cancel := context.WithTimeout(context.Background(), c.requestTimeout)
246-
resp, err := c.cli.Grant(ctx, c.keyTTL)
247-
cancel()
248-
if err != nil {
249-
return nil, err
250-
}
251-
252-
ctx, cancel = context.WithTimeout(context.Background(), c.requestTimeout)
253-
_, err = c.cli.Put(ctx, etcdPath, string(bytes), clientv3.WithLease(resp.ID))
246+
_, err = c.cli.Put(ctx, etcdPath, string(bytes), clientv3.WithLease(leaseID))
254247
cancel()
255248
if err != nil {
256-
return nil, err
249+
return err
257250
}
258251

259-
return c.cli.KeepAlive(rootctx, resp.ID)
252+
return nil
260253
}
261254

262255
// GetHost returns the host (passed as "host:port") details. If host is not

0 commit comments

Comments
 (0)