You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.1 KiB
60 lines
1.1 KiB
package simulation |
|
|
|
type WebServerNode struct { |
|
ID string |
|
Queue []*Request |
|
CapacityRPS int |
|
BaseLatencyMs int |
|
PenaltyPerRPS float64 |
|
Processed []*Request |
|
Alive bool |
|
} |
|
|
|
func (ws *WebServerNode) GetID() string { |
|
return ws.ID |
|
} |
|
|
|
func (ws *WebServerNode) Type() string { |
|
return "webserver" |
|
} |
|
|
|
func (ws *WebServerNode) IsAlive() bool { |
|
return ws.Alive |
|
} |
|
|
|
func (ws *WebServerNode) Tick(tick int) { |
|
toProcess := min(ws.CapacityRPS, len(ws.Queue)) |
|
for i := 0; i < toProcess; i++ { |
|
req := ws.Queue[0] |
|
req.LatencyMS += ws.BaseLatencyMs |
|
ws.Processed = append(ws.Processed, req) |
|
|
|
ws.Queue[i] = nil |
|
} |
|
|
|
ws.Queue = ws.Queue[toProcess:] |
|
|
|
if len(ws.Queue) > ws.CapacityRPS { |
|
overload := len(ws.Queue) - ws.CapacityRPS |
|
for _, req := range ws.Queue { |
|
req.LatencyMS += int(ws.PenaltyPerRPS * float64(overload)) |
|
} |
|
} |
|
} |
|
|
|
func (ws *WebServerNode) Receive(req *Request) { |
|
ws.Queue = append(ws.Queue, req) |
|
} |
|
|
|
func (ws *WebServerNode) Emit() []*Request { |
|
out := ws.Processed |
|
ws.Processed = nil |
|
return out |
|
} |
|
|
|
func min(a, b int) int { |
|
if a < b { |
|
return a |
|
} |
|
return b |
|
}
|
|
|