summaryrefslogtreecommitdiff
path: root/src/hub/src/spreadspace.org/sfive/s5srv.go
blob: 979cd83fc9d3de06a3d86a271965e4e894c31228 (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// sfive
//
// sfive - spreadspace streaming statistics suite is a generic
// statistic collection tool for streaming server infrastuctures.
// The system collects and stores meta data like number of views
// and throughput from a number of streaming servers and stores
// it in a global data store.
// The data acquisition is designed to be generic and extensible in
// order to support different streaming software.
// sfive also contains tools and applications to filter and visualize
// live and recorded data.
//
//
// Copyright (C) 2014-2017 Christian Pointner <equinox@spreadspace.org>
//                         Markus Grüneis <gimpf@gimpf.org>
//
// This file is part of sfive.
//
// sfive is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3
// as published by the Free Software Foundation.
//
// sfive 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with sfive. If not, see <http://www.gnu.org/licenses/>.
//

package sfive

import (
	"errors"
	"runtime"
	"sync"
)

type ingestToken struct {
	update   *UpdateFull
	response chan error
}

type ingestManyToken struct {
	updates  []*UpdateFull
	response chan error
}

type Server struct {
	store          *Store
	numWorker      int
	anonymization  AnonymizationAlgo
	geoip          GeoIPLookup
	quit           chan bool
	done           *sync.WaitGroup
	ingestChan     chan ingestToken
	ingestManyChan chan ingestManyToken
}

func (srv Server) transform(update *UpdateFull) *UpdateFull {
	bytesSentTotal := uint(0)
	clients := []Client{}
	for _, client := range update.Data.Clients {
		bytesSentTotal += client.BytesSent

		if srv.geoip != nil {
			if info, err := srv.geoip.Lookup(client.IP); err != nil {
				s5l.Printf("server|transform: Geo-IP lookup failed: %v", err)
			} else {
				client.GeoInfo = *info
			}
		}

		if srv.anonymization != nil {
			if aIP, err := srv.anonymization.Anonymize(client.IP); err != nil {
				s5l.Printf("server|transform: anonymization failed: %v", err)
			} else {
				client.IP = aIP
			}
		}

		clients = append(clients, client)
	}
	update.Data.Clients = clients

	if uint(len(update.Data.Clients)) > update.Data.ClientCount {
		if update.Data.ClientCount > 0 {
			s5l.Printf("server|transform: fixing client-count: %d -> %d", update.Data.ClientCount, len(update.Data.Clients))
		}
		update.Data.ClientCount = uint(len(update.Data.Clients))
	}
	if bytesSentTotal > update.Data.BytesSent {
		if update.Data.BytesSent > 0 {
			s5l.Printf("server|transform: fixing bytes-sent: %d -> %d", update.Data.BytesSent, bytesSentTotal)
		}
		update.Data.BytesSent = bytesSentTotal
	}

	return update
}

func (srv Server) transformMany(updates []*UpdateFull) {
	for _, update := range updates {
		srv.transform(update)
	}
}

func (srv Server) ingestWorker(idx int) {
	for {
		select {
		case <-srv.quit:
			return
		case token := <-srv.ingestChan:
			srv.transform(token.update)
			token.response <- srv.store.Append(token.update)
		case token := <-srv.ingestManyChan:
			srv.transformMany(token.updates)
			token.response <- srv.store.AppendMany(token.updates)
		}
	}
}

func (srv Server) Ingest(update *UpdateFull) error {
	token := ingestToken{update: update, response: make(chan error, 1)}
	defer close(token.response)
	srv.ingestChan <- token
	return <-token.response
}

func (srv Server) IngestMany(updates []*UpdateFull) error {
	token := ingestManyToken{updates: updates, response: make(chan error, 1)}
	defer close(token.response)
	srv.ingestManyChan <- token
	return <-token.response
}

func (srv Server) Close() {
	s5l.Printf("server: shutting down")
	close(srv.quit)
	srv.done.Wait()

	close(srv.ingestChan)
	close(srv.ingestManyChan)
	srv.store.Close()
	s5l.Printf("server: finished")
}

func NewServer(cfg SrvConfig) (srv *Server, err error) {
	// TODO: read configuration and create instance with correct settings
	srv = &Server{}
	if srv.store, err = NewStore(cfg.Store); err != nil {
		return
	}

	if cfg.Transform.Anonymize {
		if srv.anonymization, err = NewCryptopanAnonymization(cfg.Transform.AnonKeyfile); err != nil {
			err = errors.New("failed to initialize IP address anonymization: " + err.Error())
			return
		}
		s5l.Printf("server|transform: using IP address anonymization: %s", srv.anonymization)
	}

	if cfg.Transform.GeoipDB != "" {
		if srv.geoip, err = NewMaxMindGeoIP2(cfg.Transform.GeoipDB); err != nil {
			err = errors.New("failed to initialize Geo-IP Lookup: " + err.Error())
			return
		}
		s5l.Printf("server|transform: using Geo-IP Lookup: %s", srv.geoip)
	}

	srv.numWorker = runtime.NumCPU() // TODO: make this configurable
	srv.quit = make(chan bool)
	srv.done = &sync.WaitGroup{}
	srv.ingestChan = make(chan ingestToken, srv.numWorker)
	srv.ingestManyChan = make(chan ingestManyToken, srv.numWorker)
	for i := 0; i < srv.numWorker; i = i + 1 {
		srv.done.Add(1)
		go func(idx int) {
			defer srv.done.Done()
			srv.ingestWorker(idx)
		}(i)
	}
	s5l.Printf("server: started")
	return
}