forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventloop_group.go
61 lines (53 loc) · 1.38 KB
/
eventloop_group.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
// Copyright 2019 Andy Pan. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gnet
// LoadBalance sets the load balancing method.
//type LoadBalance int
//
//const (
// // RoundRobin requests that connections are distributed to a loop in a
// // round-robin fashion.
// RoundRobin LoadBalance = iota
// // Random requests that connections are randomly distributed.
// Random
// // LeastConnections assigns the next accepted connection to the loop with
// // the least number of active connections.
// LeastConnections
//)
// IEventLoopGroup represents a set of event-loops.
type IEventLoopGroup interface {
register(*loop)
next() *loop
iterate(func(int, *loop) bool)
len() int
}
type eventLoopGroup struct {
nextLoopIndex int
eventLoops []*loop
size int
}
func (g *eventLoopGroup) register(lp *loop) {
g.eventLoops = append(g.eventLoops, lp)
g.size++
}
// Built-in load-balance algorithm is Round-Robin.
// TODO: support more load-balance algorithms.
func (g *eventLoopGroup) next() (lp *loop) {
lp = g.eventLoops[g.nextLoopIndex]
g.nextLoopIndex++
if g.nextLoopIndex >= g.size {
g.nextLoopIndex = 0
}
return
}
func (g *eventLoopGroup) iterate(f func(int, *loop) bool) {
for i, lp := range g.eventLoops {
if !f(i, lp) {
break
}
}
}
func (g *eventLoopGroup) len() int {
return g.size
}