/
udp.go
181 lines (161 loc) · 4.33 KB
/
udp.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"math"
"net"
"os"
"sync"
"time"
"agones.dev/agones/pkg/util/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait"
)
// udpServer is a rate limited udp server that echos
// udp packets back to senders
type udpServer struct {
logger *logrus.Entry
conn net.PacketConn
rateLimit rate.Limit
rateBurst int
clock clock.Clock
limitsMutex sync.Mutex
limits map[string]*visitor
healthMutex sync.RWMutex
health bool
}
// visitor tracks when a visitor last sent
// a packet, and it's rate limit
type visitor struct {
stamp time.Time
limit *rate.Limiter
}
// newUDPServer returns a new udpServer implementation
// withe the rate limit
func newUDPServer(rateLimit rate.Limit) *udpServer {
udpSrv := &udpServer{
rateLimit: rateLimit,
rateBurst: int(math.Floor(float64(rateLimit))),
clock: clock.RealClock{},
limitsMutex: sync.Mutex{},
limits: map[string]*visitor{},
}
udpSrv.logger = runtime.NewLoggerWithType(udpSrv)
return udpSrv
}
// run runs the udp server. Non blocking operation
func (u *udpServer) run(ctx context.Context) {
u.healthy()
logger.Info("Starting UDP server")
var err error
u.conn, err = net.ListenPacket("udp", ":8080")
if err != nil {
logger.WithError(err).Fatal("Could not start udp server")
}
go func() {
defer u.unhealthy()
wait.Until(u.cleanUp, time.Minute, ctx.Done())
}()
u.readWriteLoop(ctx)
}
// cleans up visitors, if they are more than a
// minute without being touched
func (u *udpServer) cleanUp() {
u.limitsMutex.Lock()
defer u.limitsMutex.Unlock()
for k, v := range u.limits {
if u.clock.Now().Sub(v.stamp) > time.Minute {
delete(u.limits, k)
}
}
}
// readWriteLoop reads the UDP packet in, and then echos the data back
// in a rate limited way
func (u *udpServer) readWriteLoop(ctx context.Context) {
go func() {
defer u.unhealthy()
for {
select {
case <-ctx.Done():
return
default:
b := make([]byte, 1024)
_, sender, err := u.conn.ReadFrom(b)
if err != nil {
if ctx.Err() != nil && err == os.ErrClosed {
return
}
u.logger.WithError(err).Error("Error reading udp packet")
continue
}
go u.rateLimitedEchoResponse(b, sender)
}
}
}()
}
// rateLimitedEchoResponse echos the udp request, but is ignored if
// it is past its rate limit
func (u *udpServer) rateLimitedEchoResponse(b []byte, sender net.Addr) {
var vis *visitor
u.limitsMutex.Lock()
key := sender.String()
vis, ok := u.limits[key]
if !ok {
vis = &visitor{limit: rate.NewLimiter(u.rateLimit, u.rateBurst)}
u.limits[key] = vis
}
vis.stamp = u.clock.Now()
u.limitsMutex.Unlock()
if vis.limit.Allow() {
b = bytes.TrimRight(b, "\x00")
if _, err := u.conn.WriteTo(b, sender); err != nil {
u.logger.WithError(err).Error("Error sending returning udp packet")
}
} else {
logger.WithField("addr", sender.String()).Warn("Rate limited. No response sent")
}
}
// close closes and shutdown the udp server
func (u *udpServer) close() {
if err := u.conn.Close(); err != nil {
logger.WithError(err).Error("Error closing udp connection")
}
}
// healthy marks this udpServer as healthy
func (u *udpServer) healthy() {
u.healthMutex.Lock()
defer u.healthMutex.Unlock()
u.health = true
}
// unhealthy marks this udpServer as unhealthy
func (u *udpServer) unhealthy() {
u.healthMutex.Lock()
defer u.healthMutex.Unlock()
u.health = false
}
// Health returns the health of the UDP Server.
// true is healthy, false is not
func (u *udpServer) Health() error {
u.healthMutex.RLock()
defer u.healthMutex.RUnlock()
if !u.health {
return errors.New("UDP Server is unhealthy")
}
return nil
}