/
http_send.go
115 lines (97 loc) · 2.75 KB
/
http_send.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
// Copyright (C) ENEO Tecnologia SL - 2016
//
// Authors: Diego Fernández Barrera <dfernandez@redborder.com> <bigomby@gmail.com>
// Eugenio Pérez Martín <eugenio@redborder.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/lgpl-3.0.txt>.
// +nobuild examples
package main
import (
"fmt"
"sync"
"github.com/sirupsen/logrus"
"github.com/x-cray/logrus-prefixed-formatter"
"github.com/redBorder/rbforwarder"
"github.com/redBorder/rbforwarder/components/batch"
"github.com/redBorder/rbforwarder/components/httpsender"
"github.com/redBorder/rbforwarder/components/limiter"
)
func main() {
var wg sync.WaitGroup
var components []interface{}
const numMessages = 100000
httpLogger := logrus.New().WithField("prefix", "HTTP Sender")
httpLogger.Logger.Formatter = new(prefixed.TextFormatter)
f := rbforwarder.NewRBForwarder(rbforwarder.Config{
Retries: 2,
Backoff: 15,
QueueSize: 10,
})
// Add Limiter component
components = append(components, &limiter.Limiter{
Config: limiter.Config{
BytesLimit: 256 * 1024,
},
})
// Add Batch component
components = append(components, &batcher.Batcher{
Config: batcher.Config{
Workers: 2,
TimeoutMillis: 100,
Limit: 10000,
},
})
// Add HTTP Sender component
components = append(components, &httpsender.HTTPSender{
Config: httpsender.Config{
Workers: 10,
Logger: httpLogger,
URL: "http://localhost:8888",
},
})
f.PushComponents(components)
opts := map[string]interface{}{
"http_endpoint": "test",
"batch_group": "example",
}
f.Run()
wg.Add(1)
go func() {
var errors int
var messages int
fmt.Print("[")
for report := range f.GetOrderedReports() {
r := report.(rbforwarder.Report)
// fmt.Printf("[MESSAGE %d] %s\n", r.Opaque.(int), r.Status)
messages++
if messages%(numMessages/20) == 0 {
fmt.Printf("=")
}
if r.Code > 0 {
errors++
}
if messages >= numMessages {
break
}
}
fmt.Print("] ")
fmt.Printf("Sent %d messages with %d errors\n", messages, errors)
wg.Done()
}()
for i := 0; i < numMessages; i++ {
data := fmt.Sprintf("{\"message\": %d}", i)
f.Produce([]byte(data), opts, i)
}
wg.Wait()
}