-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
73 lines (67 loc) · 2 KB
/
writer.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
/*
* Copyright (c) 2017,2019 Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package main
import (
"github.com/farsightsec/sielink"
"github.com/golang/protobuf/proto"
)
// A payloadWriter packs up its input in a sielink Payload as an
// nmsg container with channel `channel`, and sends it over
// `writeChannel` to a writer goroutine writing to the client link.
//
// `writeChannel` has a buffer of size 1. If a a new payload is
// ready to send, and the previous payload hasn't been collected
// by the writer goroutine, the previous payload is dropped and
// its loss recorded in the new payload.
type payloadWriter struct {
ctx *Context
channel *uint32
writeChannel chan *sielink.Payload
}
func newPayloadWriter(ctx *Context) *payloadWriter {
wchan := make(chan *sielink.Payload, 1)
res := &payloadWriter{
ctx: ctx,
writeChannel: wchan,
channel: proto.Uint32(ctx.Config.Channel),
}
go func() {
for p := range wchan {
traceMsg(ctx, "Sending payload: len=%d loss=%d payloads",
len(p.GetData()),
p.GetLinkLoss().GetPayloads())
ctx.Client.Send(p)
}
}()
return res
}
func (c *payloadWriter) sendPayload(p *sielink.Payload) {
for {
// Note: this presumes cap(c.writeChannel) == 1
// If c.writeChannel is larger, the "case discard := "
// case needs to be moved under a default: case.
select {
case c.writeChannel <- p:
c.ctx.NmsgUp.Messages++
c.ctx.NmsgUp.Bytes += uint64(len(p.GetData()))
return
case discard := <-c.writeChannel:
p.RecordDiscard(discard)
c.ctx.NmsgDiscard.Messages++
c.ctx.NmsgDiscard.Bytes += uint64(len(discard.GetData()))
}
}
}
func (c *payloadWriter) Write(b []byte) (int, error) {
c.sendPayload(&sielink.Payload{
Channel: c.channel,
PayloadType: sielink.PayloadType_NmsgContainer.Enum(),
Data: b,
})
return len(b), nil
}