A running example of the code from:
- http://marcio.io/2015/07/handling-1-million-requests-per-minute-with-golang
- http://nesv.github.io/golang/2014/02/25/worker-queues-in-go.html
Small refactorings made to original code:
A running example of the code from:
Small refactorings made to original code:
| from typing import Tuple | |
| class TrieNode(object): | |
| """ | |
| Our trie node implementation. Very basic. but does the job | |
| """ | |
| def __init__(self, char: str): | |
| self.char = char |
| package main | |
| import ( | |
| "errors" | |
| "fmt" | |
| "strconv" | |
| "gonum.org/v1/gonum/graph" | |
| "gonum.org/v1/gonum/graph/multi" | |
| ) |
| type State struct { | |
| Id int64 | |
| Value interface{} | |
| } | |
| type Link struct { | |
| Id int64 | |
| T, F graph.Node | |
| Rules map[Operator]Event | |
| } |
| type Event string | |
| type Operator string | |
| var NodeIDCntr = 0 | |
| var LineIdCntr = 1 | |
| type StateMachine struct { | |
| PresentState State | |
| g *multi.DirectedGraph | |
| } |
| func New() *StateMachine { | |
| s := &StateMachine{} | |
| s.g = multi.NewDirectedGraph() | |
| return s | |
| } |
| func (s *StateMachine) Init(initStateValue interface{}) State { | |
| s.PresentState = State{Id: int64(NodeIDCntr), Value: initStateValue} | |
| s.g.AddNode(s.PresentState) | |
| NodeIDCntr++ | |
| return s.PresentState | |
| } | |
| func (s *StateMachine) NewState(stateValue interface{}) State { | |
| state := State{Id: int64(NodeIDCntr), Value: stateValue} | |
| s.g.AddNode(state) |
| func NewRule(triggerConditionOperator Operator, comparisonValue Event) map[Operator]Event { | |
| return map[Operator]Event{triggerConditionOperator: comparisonValue} | |
| } |