repo
stringlengths
5
54
path
stringlengths
4
155
func_name
stringlengths
1
118
original_string
stringlengths
52
85.5k
language
stringclasses
1 value
code
stringlengths
52
85.5k
code_tokens
sequence
docstring
stringlengths
6
2.61k
docstring_tokens
sequence
sha
stringlengths
40
40
url
stringlengths
85
252
partition
stringclasses
1 value
google/seesaw
ncc/lb.go
LBInterfaceUp
func (ncc *SeesawNCC) LBInterfaceUp(iface *ncctypes.LBInterface, out *int) error { // TODO(jsing): Handle IPv6-only, and improve IPv6 route setup. netIface, err := iface.Interface() if err != nil { return err } nodeIface, err := net.InterfaceByName(iface.NodeInterface) if err != nil { return fmt.Errorf("Failed to get node interface: %v", err) } nodeNet, err := findNetwork(nodeIface, iface.Node.IPv4Addr) if err != nil { return fmt.Errorf("Failed to get node network: %v", err) } log.Infof("Bringing up LB interface %s on %s", nodeNet, iface.Name) if !nodeNet.Contains(iface.ClusterVIP.IPv4Addr) { return fmt.Errorf("Node network %s does not contain cluster VIP %s", nodeNet, iface.ClusterVIP.IPv4Addr) } gateway, err := routeDefaultIPv4() if err != nil { return fmt.Errorf("Failed to get IPv4 default route: %v", err) } if err := ifaceUp(netIface); err != nil { return fmt.Errorf("Failed to bring interface up: %v", err) } // Configure routing. if err := ipRunIface(netIface, "route add %s dev %s table %d", nodeNet, iface.Name, iface.RoutingTableID); err != nil { return fmt.Errorf("Failed to configure routing: %v", err) } if err := ipRunIface(netIface, "route add 0/0 via %s dev %s table %d", gateway, iface.Name, iface.RoutingTableID); err != nil { return fmt.Errorf("Failed to configure routing: %v", err) } return nil }
go
func (ncc *SeesawNCC) LBInterfaceUp(iface *ncctypes.LBInterface, out *int) error { // TODO(jsing): Handle IPv6-only, and improve IPv6 route setup. netIface, err := iface.Interface() if err != nil { return err } nodeIface, err := net.InterfaceByName(iface.NodeInterface) if err != nil { return fmt.Errorf("Failed to get node interface: %v", err) } nodeNet, err := findNetwork(nodeIface, iface.Node.IPv4Addr) if err != nil { return fmt.Errorf("Failed to get node network: %v", err) } log.Infof("Bringing up LB interface %s on %s", nodeNet, iface.Name) if !nodeNet.Contains(iface.ClusterVIP.IPv4Addr) { return fmt.Errorf("Node network %s does not contain cluster VIP %s", nodeNet, iface.ClusterVIP.IPv4Addr) } gateway, err := routeDefaultIPv4() if err != nil { return fmt.Errorf("Failed to get IPv4 default route: %v", err) } if err := ifaceUp(netIface); err != nil { return fmt.Errorf("Failed to bring interface up: %v", err) } // Configure routing. if err := ipRunIface(netIface, "route add %s dev %s table %d", nodeNet, iface.Name, iface.RoutingTableID); err != nil { return fmt.Errorf("Failed to configure routing: %v", err) } if err := ipRunIface(netIface, "route add 0/0 via %s dev %s table %d", gateway, iface.Name, iface.RoutingTableID); err != nil { return fmt.Errorf("Failed to configure routing: %v", err) } return nil }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceUp", "(", "iface", "*", "ncctypes", ".", "LBInterface", ",", "out", "*", "int", ")", "error", "{", "// TODO(jsing): Handle IPv6-only, and improve IPv6 route setup.", "netIface", ",", "err", ":=", "iface", ".", "Interface", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "nodeIface", ",", "err", ":=", "net", ".", "InterfaceByName", "(", "iface", ".", "NodeInterface", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "nodeNet", ",", "err", ":=", "findNetwork", "(", "nodeIface", ",", "iface", ".", "Node", ".", "IPv4Addr", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "log", ".", "Infof", "(", "\"", "\"", ",", "nodeNet", ",", "iface", ".", "Name", ")", "\n\n", "if", "!", "nodeNet", ".", "Contains", "(", "iface", ".", "ClusterVIP", ".", "IPv4Addr", ")", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "nodeNet", ",", "iface", ".", "ClusterVIP", ".", "IPv4Addr", ")", "\n", "}", "\n\n", "gateway", ",", "err", ":=", "routeDefaultIPv4", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "if", "err", ":=", "ifaceUp", "(", "netIface", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "// Configure routing.", "if", "err", ":=", "ipRunIface", "(", "netIface", ",", "\"", "\"", ",", "nodeNet", ",", "iface", ".", "Name", ",", "iface", ".", "RoutingTableID", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "if", "err", ":=", "ipRunIface", "(", "netIface", ",", "\"", "\"", ",", "gateway", ",", "iface", ".", "Name", ",", "iface", ".", "RoutingTableID", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// LBInterfaceUp brings the load balancing interface up.
[ "LBInterfaceUp", "brings", "the", "load", "balancing", "interface", "up", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L184-L223
train
google/seesaw
ncc/lb.go
LBInterfaceAddVserver
func (ncc *SeesawNCC) LBInterfaceAddVserver(lbVserver *ncctypes.LBInterfaceVserver, out *int) error { return iptablesAddRules(lbVserver.Vserver, lbVserver.Iface.ClusterVIP, lbVserver.AF) }
go
func (ncc *SeesawNCC) LBInterfaceAddVserver(lbVserver *ncctypes.LBInterfaceVserver, out *int) error { return iptablesAddRules(lbVserver.Vserver, lbVserver.Iface.ClusterVIP, lbVserver.AF) }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceAddVserver", "(", "lbVserver", "*", "ncctypes", ".", "LBInterfaceVserver", ",", "out", "*", "int", ")", "error", "{", "return", "iptablesAddRules", "(", "lbVserver", ".", "Vserver", ",", "lbVserver", ".", "Iface", ".", "ClusterVIP", ",", "lbVserver", ".", "AF", ")", "\n", "}" ]
// LBInterfaceAddVserver adds the specified Vserver to the load balancing interface.
[ "LBInterfaceAddVserver", "adds", "the", "specified", "Vserver", "to", "the", "load", "balancing", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L226-L228
train
google/seesaw
ncc/lb.go
LBInterfaceDeleteVserver
func (ncc *SeesawNCC) LBInterfaceDeleteVserver(lbVserver *ncctypes.LBInterfaceVserver, out *int) error { return iptablesDeleteRules(lbVserver.Vserver, lbVserver.Iface.ClusterVIP, lbVserver.AF) }
go
func (ncc *SeesawNCC) LBInterfaceDeleteVserver(lbVserver *ncctypes.LBInterfaceVserver, out *int) error { return iptablesDeleteRules(lbVserver.Vserver, lbVserver.Iface.ClusterVIP, lbVserver.AF) }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceDeleteVserver", "(", "lbVserver", "*", "ncctypes", ".", "LBInterfaceVserver", ",", "out", "*", "int", ")", "error", "{", "return", "iptablesDeleteRules", "(", "lbVserver", ".", "Vserver", ",", "lbVserver", ".", "Iface", ".", "ClusterVIP", ",", "lbVserver", ".", "AF", ")", "\n", "}" ]
// LBInterfaceDeleteVserver removes the specified Vserver from the load balancing interface.
[ "LBInterfaceDeleteVserver", "removes", "the", "specified", "Vserver", "from", "the", "load", "balancing", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L231-L233
train
google/seesaw
ncc/lb.go
LBInterfaceAddVIP
func (ncc *SeesawNCC) LBInterfaceAddVIP(vip *ncctypes.LBInterfaceVIP, out *int) error { switch vip.Type { case seesaw.UnicastVIP: netIface, err := vip.Iface.Interface() if err != nil { return err } iface, network, err := selectInterfaceNetwork(netIface, vip.IP.IP()) if err != nil { return fmt.Errorf("Failed to select interface and network for %v: %v", vip.VIP, err) } log.Infof("Adding VIP %s to %s", vip.IP.IP(), iface.Name) if err := ifaceAddIPAddr(iface, vip.IP.IP(), network.Mask); err != nil { return err } return routeLocal(iface, vip.IP.IP(), vip.Iface.Node) case seesaw.AnycastVIP, seesaw.DedicatedVIP: dummyIface, err := net.InterfaceByName(vip.Iface.DummyInterface) if err != nil { return fmt.Errorf("Failed to find dummy interface: %v", err) } prefixLen := net.IPv6len * 8 if vip.IP.IP().To4() != nil { prefixLen = net.IPv4len * 8 } mask := net.CIDRMask(prefixLen, prefixLen) log.Infof("Adding VIP %s to %s", vip.IP.IP(), dummyIface.Name) if err := ifaceAddIPAddr(dummyIface, vip.IP.IP(), mask); err != nil { return err } return routeLocal(dummyIface, vip.IP.IP(), vip.Iface.Node) default: return fmt.Errorf("Unknown VIPType for %v: %v", vip.VIP, vip.Type) } }
go
func (ncc *SeesawNCC) LBInterfaceAddVIP(vip *ncctypes.LBInterfaceVIP, out *int) error { switch vip.Type { case seesaw.UnicastVIP: netIface, err := vip.Iface.Interface() if err != nil { return err } iface, network, err := selectInterfaceNetwork(netIface, vip.IP.IP()) if err != nil { return fmt.Errorf("Failed to select interface and network for %v: %v", vip.VIP, err) } log.Infof("Adding VIP %s to %s", vip.IP.IP(), iface.Name) if err := ifaceAddIPAddr(iface, vip.IP.IP(), network.Mask); err != nil { return err } return routeLocal(iface, vip.IP.IP(), vip.Iface.Node) case seesaw.AnycastVIP, seesaw.DedicatedVIP: dummyIface, err := net.InterfaceByName(vip.Iface.DummyInterface) if err != nil { return fmt.Errorf("Failed to find dummy interface: %v", err) } prefixLen := net.IPv6len * 8 if vip.IP.IP().To4() != nil { prefixLen = net.IPv4len * 8 } mask := net.CIDRMask(prefixLen, prefixLen) log.Infof("Adding VIP %s to %s", vip.IP.IP(), dummyIface.Name) if err := ifaceAddIPAddr(dummyIface, vip.IP.IP(), mask); err != nil { return err } return routeLocal(dummyIface, vip.IP.IP(), vip.Iface.Node) default: return fmt.Errorf("Unknown VIPType for %v: %v", vip.VIP, vip.Type) } }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceAddVIP", "(", "vip", "*", "ncctypes", ".", "LBInterfaceVIP", ",", "out", "*", "int", ")", "error", "{", "switch", "vip", ".", "Type", "{", "case", "seesaw", ".", "UnicastVIP", ":", "netIface", ",", "err", ":=", "vip", ".", "Iface", ".", "Interface", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "iface", ",", "network", ",", "err", ":=", "selectInterfaceNetwork", "(", "netIface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "iface", ".", "Name", ")", "\n", "if", "err", ":=", "ifaceAddIPAddr", "(", "iface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "network", ".", "Mask", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "return", "routeLocal", "(", "iface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "vip", ".", "Iface", ".", "Node", ")", "\n", "case", "seesaw", ".", "AnycastVIP", ",", "seesaw", ".", "DedicatedVIP", ":", "dummyIface", ",", "err", ":=", "net", ".", "InterfaceByName", "(", "vip", ".", "Iface", ".", "DummyInterface", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "prefixLen", ":=", "net", ".", "IPv6len", "*", "8", "\n", "if", "vip", ".", "IP", ".", "IP", "(", ")", ".", "To4", "(", ")", "!=", "nil", "{", "prefixLen", "=", "net", ".", "IPv4len", "*", "8", "\n", "}", "\n", "mask", ":=", "net", ".", "CIDRMask", "(", "prefixLen", ",", "prefixLen", ")", "\n", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "dummyIface", ".", "Name", ")", "\n", "if", "err", ":=", "ifaceAddIPAddr", "(", "dummyIface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "mask", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "return", "routeLocal", "(", "dummyIface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "vip", ".", "Iface", ".", "Node", ")", "\n", "default", ":", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "vip", ".", "Type", ")", "\n", "}", "\n", "}" ]
// LBInterfaceAddVIP adds the specified VIP to the load balancing interface.
[ "LBInterfaceAddVIP", "adds", "the", "specified", "VIP", "to", "the", "load", "balancing", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L236-L270
train
google/seesaw
ncc/lb.go
LBInterfaceDeleteVIP
func (ncc *SeesawNCC) LBInterfaceDeleteVIP(vip *ncctypes.LBInterfaceVIP, out *int) error { switch vip.Type { case seesaw.UnicastVIP: netIface, err := vip.Iface.Interface() if err != nil { return err } iface, network, err := findInterfaceNetwork(netIface, vip.IP.IP()) if err != nil { return fmt.Errorf("Failed to find interface and network for %v: %v", vip.VIP, err) } log.Infof("Removing VIP %s from %s", vip.IP.IP(), iface.Name) if err := ifaceDelIPAddr(iface, vip.IP.IP(), network.Mask); err != nil { return fmt.Errorf("Failed to delete VIP %s: %v", vip.VIP, err) } if err := removeLocalRoutes(vip.IP.IP()); err != nil { log.Infof("Failed to remove local routes for VIP %s: %v", vip.VIP, err) } if err := removeMainRoutes(vip.VIP.IP.IP()); err != nil { log.Infof("Failed to remove main routes for VIP %s: %v", vip.VIP, err) } return nil case seesaw.AnycastVIP, seesaw.DedicatedVIP: dummyIface, err := net.InterfaceByName(vip.Iface.DummyInterface) if err != nil { return fmt.Errorf("Failed to find dummy interface: %v", err) } prefixLen := net.IPv6len * 8 if vip.IP.IP().To4() != nil { prefixLen = net.IPv4len * 8 } mask := net.CIDRMask(prefixLen, prefixLen) log.Infof("Removing VIP %s from %s", vip.IP.IP(), dummyIface.Name) if err = ifaceDelIPAddr(dummyIface, vip.IP.IP(), mask); err != nil { return fmt.Errorf("Failed to delete VIP %s: %v", vip.VIP, err) } // Workaround for kernel bug(?). The route should have been removed when // address was removed, but this doesn't always happen. An error is // non-fatal since it probably means the route doesn't exist. if err := removeLocalRoutes(vip.VIP.IP.IP()); err != nil { log.Infof("Failed to remove local routes for VIP %s: %v", vip.VIP, err) } if err := removeMainRoutes(vip.VIP.IP.IP()); err != nil { log.Infof("Failed to remove main routes for VIP %s: %v", vip.VIP, err) } return nil default: return fmt.Errorf("Unknown VIPType for %v: %v", vip.VIP, vip.VIP.Type) } }
go
func (ncc *SeesawNCC) LBInterfaceDeleteVIP(vip *ncctypes.LBInterfaceVIP, out *int) error { switch vip.Type { case seesaw.UnicastVIP: netIface, err := vip.Iface.Interface() if err != nil { return err } iface, network, err := findInterfaceNetwork(netIface, vip.IP.IP()) if err != nil { return fmt.Errorf("Failed to find interface and network for %v: %v", vip.VIP, err) } log.Infof("Removing VIP %s from %s", vip.IP.IP(), iface.Name) if err := ifaceDelIPAddr(iface, vip.IP.IP(), network.Mask); err != nil { return fmt.Errorf("Failed to delete VIP %s: %v", vip.VIP, err) } if err := removeLocalRoutes(vip.IP.IP()); err != nil { log.Infof("Failed to remove local routes for VIP %s: %v", vip.VIP, err) } if err := removeMainRoutes(vip.VIP.IP.IP()); err != nil { log.Infof("Failed to remove main routes for VIP %s: %v", vip.VIP, err) } return nil case seesaw.AnycastVIP, seesaw.DedicatedVIP: dummyIface, err := net.InterfaceByName(vip.Iface.DummyInterface) if err != nil { return fmt.Errorf("Failed to find dummy interface: %v", err) } prefixLen := net.IPv6len * 8 if vip.IP.IP().To4() != nil { prefixLen = net.IPv4len * 8 } mask := net.CIDRMask(prefixLen, prefixLen) log.Infof("Removing VIP %s from %s", vip.IP.IP(), dummyIface.Name) if err = ifaceDelIPAddr(dummyIface, vip.IP.IP(), mask); err != nil { return fmt.Errorf("Failed to delete VIP %s: %v", vip.VIP, err) } // Workaround for kernel bug(?). The route should have been removed when // address was removed, but this doesn't always happen. An error is // non-fatal since it probably means the route doesn't exist. if err := removeLocalRoutes(vip.VIP.IP.IP()); err != nil { log.Infof("Failed to remove local routes for VIP %s: %v", vip.VIP, err) } if err := removeMainRoutes(vip.VIP.IP.IP()); err != nil { log.Infof("Failed to remove main routes for VIP %s: %v", vip.VIP, err) } return nil default: return fmt.Errorf("Unknown VIPType for %v: %v", vip.VIP, vip.VIP.Type) } }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceDeleteVIP", "(", "vip", "*", "ncctypes", ".", "LBInterfaceVIP", ",", "out", "*", "int", ")", "error", "{", "switch", "vip", ".", "Type", "{", "case", "seesaw", ".", "UnicastVIP", ":", "netIface", ",", "err", ":=", "vip", ".", "Iface", ".", "Interface", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "iface", ",", "network", ",", "err", ":=", "findInterfaceNetwork", "(", "netIface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "iface", ".", "Name", ")", "\n", "if", "err", ":=", "ifaceDelIPAddr", "(", "iface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "network", ".", "Mask", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "if", "err", ":=", "removeLocalRoutes", "(", "vip", ".", "IP", ".", "IP", "(", ")", ")", ";", "err", "!=", "nil", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "if", "err", ":=", "removeMainRoutes", "(", "vip", ".", "VIP", ".", "IP", ".", "IP", "(", ")", ")", ";", "err", "!=", "nil", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "return", "nil", "\n", "case", "seesaw", ".", "AnycastVIP", ",", "seesaw", ".", "DedicatedVIP", ":", "dummyIface", ",", "err", ":=", "net", ".", "InterfaceByName", "(", "vip", ".", "Iface", ".", "DummyInterface", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "prefixLen", ":=", "net", ".", "IPv6len", "*", "8", "\n", "if", "vip", ".", "IP", ".", "IP", "(", ")", ".", "To4", "(", ")", "!=", "nil", "{", "prefixLen", "=", "net", ".", "IPv4len", "*", "8", "\n", "}", "\n", "mask", ":=", "net", ".", "CIDRMask", "(", "prefixLen", ",", "prefixLen", ")", "\n\n", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "dummyIface", ".", "Name", ")", "\n", "if", "err", "=", "ifaceDelIPAddr", "(", "dummyIface", ",", "vip", ".", "IP", ".", "IP", "(", ")", ",", "mask", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n\n", "// Workaround for kernel bug(?). The route should have been removed when", "// address was removed, but this doesn't always happen. An error is", "// non-fatal since it probably means the route doesn't exist.", "if", "err", ":=", "removeLocalRoutes", "(", "vip", ".", "VIP", ".", "IP", ".", "IP", "(", ")", ")", ";", "err", "!=", "nil", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "if", "err", ":=", "removeMainRoutes", "(", "vip", ".", "VIP", ".", "IP", ".", "IP", "(", ")", ")", ";", "err", "!=", "nil", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "err", ")", "\n", "}", "\n", "return", "nil", "\n", "default", ":", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "vip", ".", "VIP", ",", "vip", ".", "VIP", ".", "Type", ")", "\n", "}", "\n\n", "}" ]
// LBInterfaceDeleteVIP removes the specified VIP from the load balancing // interface.
[ "LBInterfaceDeleteVIP", "removes", "the", "specified", "VIP", "from", "the", "load", "balancing", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L274-L326
train
google/seesaw
ncc/lb.go
LBInterfaceAddVLAN
func (ncc *SeesawNCC) LBInterfaceAddVLAN(vlan *ncctypes.LBInterfaceVLAN, out *int) error { netIface, err := vlan.Iface.Interface() if err != nil { return err } return ifaceAddVLAN(netIface, vlan.VLAN) }
go
func (ncc *SeesawNCC) LBInterfaceAddVLAN(vlan *ncctypes.LBInterfaceVLAN, out *int) error { netIface, err := vlan.Iface.Interface() if err != nil { return err } return ifaceAddVLAN(netIface, vlan.VLAN) }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceAddVLAN", "(", "vlan", "*", "ncctypes", ".", "LBInterfaceVLAN", ",", "out", "*", "int", ")", "error", "{", "netIface", ",", "err", ":=", "vlan", ".", "Iface", ".", "Interface", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "return", "ifaceAddVLAN", "(", "netIface", ",", "vlan", ".", "VLAN", ")", "\n", "}" ]
// LBInterfaceAddVLAN creates a VLAN interface on the load balancing interface.
[ "LBInterfaceAddVLAN", "creates", "a", "VLAN", "interface", "on", "the", "load", "balancing", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L329-L335
train
google/seesaw
ncc/lb.go
LBInterfaceDeleteVLAN
func (ncc *SeesawNCC) LBInterfaceDeleteVLAN(vlan *ncctypes.LBInterfaceVLAN, out *int) error { netIface, err := vlan.Iface.Interface() if err != nil { return err } return ifaceDelVLAN(netIface, vlan.VLAN) }
go
func (ncc *SeesawNCC) LBInterfaceDeleteVLAN(vlan *ncctypes.LBInterfaceVLAN, out *int) error { netIface, err := vlan.Iface.Interface() if err != nil { return err } return ifaceDelVLAN(netIface, vlan.VLAN) }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "LBInterfaceDeleteVLAN", "(", "vlan", "*", "ncctypes", ".", "LBInterfaceVLAN", ",", "out", "*", "int", ")", "error", "{", "netIface", ",", "err", ":=", "vlan", ".", "Iface", ".", "Interface", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "return", "ifaceDelVLAN", "(", "netIface", ",", "vlan", ".", "VLAN", ")", "\n", "}" ]
// LBInterfaceDeleteVLAN deletes a VLAN interface from the load balancing interface.
[ "LBInterfaceDeleteVLAN", "deletes", "a", "VLAN", "interface", "from", "the", "load", "balancing", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L338-L344
train
google/seesaw
ncc/lb.go
vlanInterfaces
func vlanInterfaces(pIface *net.Interface) ([]*net.Interface, error) { allIfaces, err := net.Interfaces() if err != nil { return nil, err } ifaces := make([]*net.Interface, 0, len(allIfaces)) prefix := fmt.Sprintf("%s.", pIface.Name) for i, iface := range allIfaces { if strings.HasPrefix(iface.Name, prefix) { ifaces = append(ifaces, &allIfaces[i]) } } return ifaces, nil }
go
func vlanInterfaces(pIface *net.Interface) ([]*net.Interface, error) { allIfaces, err := net.Interfaces() if err != nil { return nil, err } ifaces := make([]*net.Interface, 0, len(allIfaces)) prefix := fmt.Sprintf("%s.", pIface.Name) for i, iface := range allIfaces { if strings.HasPrefix(iface.Name, prefix) { ifaces = append(ifaces, &allIfaces[i]) } } return ifaces, nil }
[ "func", "vlanInterfaces", "(", "pIface", "*", "net", ".", "Interface", ")", "(", "[", "]", "*", "net", ".", "Interface", ",", "error", ")", "{", "allIfaces", ",", "err", ":=", "net", ".", "Interfaces", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "ifaces", ":=", "make", "(", "[", "]", "*", "net", ".", "Interface", ",", "0", ",", "len", "(", "allIfaces", ")", ")", "\n", "prefix", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "pIface", ".", "Name", ")", "\n", "for", "i", ",", "iface", ":=", "range", "allIfaces", "{", "if", "strings", ".", "HasPrefix", "(", "iface", ".", "Name", ",", "prefix", ")", "{", "ifaces", "=", "append", "(", "ifaces", ",", "&", "allIfaces", "[", "i", "]", ")", "\n", "}", "\n", "}", "\n", "return", "ifaces", ",", "nil", "\n", "}" ]
// vlanInterfaces returns a slice containing the VLAN interfaces associated // with a physical interface.
[ "vlanInterfaces", "returns", "a", "slice", "containing", "the", "VLAN", "interfaces", "associated", "with", "a", "physical", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L348-L361
train
google/seesaw
ncc/lb.go
findInterfaceNetwork
func findInterfaceNetwork(pIface *net.Interface, ip net.IP) (*net.Interface, *net.IPNet, error) { ifaces, err := vlanInterfaces(pIface) ifaces = append(ifaces, pIface) if err != nil { return nil, nil, err } for _, iface := range ifaces { if network, _ := findNetwork(iface, ip); network != nil { return iface, network, nil } } return nil, nil, fmt.Errorf("Failed to find IP %v on any interface", ip) }
go
func findInterfaceNetwork(pIface *net.Interface, ip net.IP) (*net.Interface, *net.IPNet, error) { ifaces, err := vlanInterfaces(pIface) ifaces = append(ifaces, pIface) if err != nil { return nil, nil, err } for _, iface := range ifaces { if network, _ := findNetwork(iface, ip); network != nil { return iface, network, nil } } return nil, nil, fmt.Errorf("Failed to find IP %v on any interface", ip) }
[ "func", "findInterfaceNetwork", "(", "pIface", "*", "net", ".", "Interface", ",", "ip", "net", ".", "IP", ")", "(", "*", "net", ".", "Interface", ",", "*", "net", ".", "IPNet", ",", "error", ")", "{", "ifaces", ",", "err", ":=", "vlanInterfaces", "(", "pIface", ")", "\n", "ifaces", "=", "append", "(", "ifaces", ",", "pIface", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "nil", ",", "err", "\n", "}", "\n", "for", "_", ",", "iface", ":=", "range", "ifaces", "{", "if", "network", ",", "_", ":=", "findNetwork", "(", "iface", ",", "ip", ")", ";", "network", "!=", "nil", "{", "return", "iface", ",", "network", ",", "nil", "\n", "}", "\n", "}", "\n", "return", "nil", ",", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "ip", ")", "\n", "}" ]
// findInterfaceNetwork searches for the given IP address on the given physical // interface and its associated VLAN interfaces. If the address is not // configured on any interface, an error is returned.
[ "findInterfaceNetwork", "searches", "for", "the", "given", "IP", "address", "on", "the", "given", "physical", "interface", "and", "its", "associated", "VLAN", "interfaces", ".", "If", "the", "address", "is", "not", "configured", "on", "any", "interface", "an", "error", "is", "returned", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L366-L378
train
google/seesaw
ncc/lb.go
findNetwork
func findNetwork(iface *net.Interface, ip net.IP) (*net.IPNet, error) { addrs, err := iface.Addrs() if err != nil { return nil, fmt.Errorf("Failed to get addresses for interface %q: %v", iface.Name, err) } for _, addr := range addrs { ipStr := addr.String() ipAddr, ipNet, err := net.ParseCIDR(ipStr) if err != nil { return nil, fmt.Errorf("Failed to parse interface address %q - %v: %v", iface.Name, ipStr, err) } if ipAddr.Equal(ip) { return ipNet, nil } } return nil, fmt.Errorf("Failed to find IP %v on interface %q", ip, iface.Name) }
go
func findNetwork(iface *net.Interface, ip net.IP) (*net.IPNet, error) { addrs, err := iface.Addrs() if err != nil { return nil, fmt.Errorf("Failed to get addresses for interface %q: %v", iface.Name, err) } for _, addr := range addrs { ipStr := addr.String() ipAddr, ipNet, err := net.ParseCIDR(ipStr) if err != nil { return nil, fmt.Errorf("Failed to parse interface address %q - %v: %v", iface.Name, ipStr, err) } if ipAddr.Equal(ip) { return ipNet, nil } } return nil, fmt.Errorf("Failed to find IP %v on interface %q", ip, iface.Name) }
[ "func", "findNetwork", "(", "iface", "*", "net", ".", "Interface", ",", "ip", "net", ".", "IP", ")", "(", "*", "net", ".", "IPNet", ",", "error", ")", "{", "addrs", ",", "err", ":=", "iface", ".", "Addrs", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "iface", ".", "Name", ",", "err", ")", "\n", "}", "\n", "for", "_", ",", "addr", ":=", "range", "addrs", "{", "ipStr", ":=", "addr", ".", "String", "(", ")", "\n", "ipAddr", ",", "ipNet", ",", "err", ":=", "net", ".", "ParseCIDR", "(", "ipStr", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "iface", ".", "Name", ",", "ipStr", ",", "err", ")", "\n", "}", "\n", "if", "ipAddr", ".", "Equal", "(", "ip", ")", "{", "return", "ipNet", ",", "nil", "\n", "}", "\n", "}", "\n", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "ip", ",", "iface", ".", "Name", ")", "\n", "}" ]
// findNetwork returns the network for the given IP address on the given // interface. If the address is not configured on the interface, an error is // returned.
[ "findNetwork", "returns", "the", "network", "for", "the", "given", "IP", "address", "on", "the", "given", "interface", ".", "If", "the", "address", "is", "not", "configured", "on", "the", "interface", "an", "error", "is", "returned", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/lb.go#L383-L399
train
google/seesaw
watchdog/service.go
newService
func newService(name, binary string) *Service { return &Service{ name: name, binary: binary, args: make([]string, 0), dependencies: make(map[string]*Service), dependents: make(map[string]*Service), done: make(chan bool), shutdown: make(chan bool, 1), started: make(chan bool, 1), stopped: make(chan bool, 1), termTimeout: 5 * time.Second, } }
go
func newService(name, binary string) *Service { return &Service{ name: name, binary: binary, args: make([]string, 0), dependencies: make(map[string]*Service), dependents: make(map[string]*Service), done: make(chan bool), shutdown: make(chan bool, 1), started: make(chan bool, 1), stopped: make(chan bool, 1), termTimeout: 5 * time.Second, } }
[ "func", "newService", "(", "name", ",", "binary", "string", ")", "*", "Service", "{", "return", "&", "Service", "{", "name", ":", "name", ",", "binary", ":", "binary", ",", "args", ":", "make", "(", "[", "]", "string", ",", "0", ")", ",", "dependencies", ":", "make", "(", "map", "[", "string", "]", "*", "Service", ")", ",", "dependents", ":", "make", "(", "map", "[", "string", "]", "*", "Service", ")", ",", "done", ":", "make", "(", "chan", "bool", ")", ",", "shutdown", ":", "make", "(", "chan", "bool", ",", "1", ")", ",", "started", ":", "make", "(", "chan", "bool", ",", "1", ")", ",", "stopped", ":", "make", "(", "chan", "bool", ",", "1", ")", ",", "termTimeout", ":", "5", "*", "time", ".", "Second", ",", "}", "\n", "}" ]
// newService returns an initialised service.
[ "newService", "returns", "an", "initialised", "service", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L70-L85
train
google/seesaw
watchdog/service.go
AddArgs
func (svc *Service) AddArgs(args string) { svc.args = strings.Fields(args) }
go
func (svc *Service) AddArgs(args string) { svc.args = strings.Fields(args) }
[ "func", "(", "svc", "*", "Service", ")", "AddArgs", "(", "args", "string", ")", "{", "svc", ".", "args", "=", "strings", ".", "Fields", "(", "args", ")", "\n", "}" ]
// AddArgs adds the given string as arguments.
[ "AddArgs", "adds", "the", "given", "string", "as", "arguments", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L93-L95
train
google/seesaw
watchdog/service.go
SetPriority
func (svc *Service) SetPriority(priority int) error { if priority < -20 || priority > 19 { return fmt.Errorf("Invalid priority %d - must be between -20 and 19", priority) } svc.priority = priority return nil }
go
func (svc *Service) SetPriority(priority int) error { if priority < -20 || priority > 19 { return fmt.Errorf("Invalid priority %d - must be between -20 and 19", priority) } svc.priority = priority return nil }
[ "func", "(", "svc", "*", "Service", ")", "SetPriority", "(", "priority", "int", ")", "error", "{", "if", "priority", "<", "-", "20", "||", "priority", ">", "19", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "priority", ")", "\n", "}", "\n", "svc", ".", "priority", "=", "priority", "\n", "return", "nil", "\n", "}" ]
// SetPriority sets the process priority for a service.
[ "SetPriority", "sets", "the", "process", "priority", "for", "a", "service", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L98-L104
train
google/seesaw
watchdog/service.go
SetUser
func (svc *Service) SetUser(username string) error { u, err := user.Lookup(username) if err != nil { return err } uid, err := strconv.Atoi(u.Uid) if err != nil { return err } gid, err := strconv.Atoi(u.Gid) if err != nil { return err } svc.uid = uint32(uid) svc.gid = uint32(gid) return nil }
go
func (svc *Service) SetUser(username string) error { u, err := user.Lookup(username) if err != nil { return err } uid, err := strconv.Atoi(u.Uid) if err != nil { return err } gid, err := strconv.Atoi(u.Gid) if err != nil { return err } svc.uid = uint32(uid) svc.gid = uint32(gid) return nil }
[ "func", "(", "svc", "*", "Service", ")", "SetUser", "(", "username", "string", ")", "error", "{", "u", ",", "err", ":=", "user", ".", "Lookup", "(", "username", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "uid", ",", "err", ":=", "strconv", ".", "Atoi", "(", "u", ".", "Uid", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "gid", ",", "err", ":=", "strconv", ".", "Atoi", "(", "u", ".", "Gid", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "svc", ".", "uid", "=", "uint32", "(", "uid", ")", "\n", "svc", ".", "gid", "=", "uint32", "(", "gid", ")", "\n", "return", "nil", "\n", "}" ]
// SetUser sets the user for a service.
[ "SetUser", "sets", "the", "user", "for", "a", "service", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L112-L128
train
google/seesaw
watchdog/service.go
run
func (svc *Service) run() { // Wait for dependencies to start. for _, dep := range svc.dependencies { log.Infof("Service %s waiting for %s to start", svc.name, dep.name) select { case started := <-dep.started: dep.started <- started case <-svc.shutdown: goto done } } for { if svc.failures > 0 { delay := time.Duration(svc.failures) * restartBackoff if delay > restartBackoffMax { delay = restartBackoffMax } log.Infof("Service %s has failed %d times - delaying %s before restart", svc.name, svc.failures, delay) select { case <-time.After(delay): case <-svc.shutdown: goto done } } svc.restarts++ svc.lastRestart = time.Now() svc.runOnce() select { case <-time.After(restartDelay): case <-svc.shutdown: goto done } } done: svc.done <- true }
go
func (svc *Service) run() { // Wait for dependencies to start. for _, dep := range svc.dependencies { log.Infof("Service %s waiting for %s to start", svc.name, dep.name) select { case started := <-dep.started: dep.started <- started case <-svc.shutdown: goto done } } for { if svc.failures > 0 { delay := time.Duration(svc.failures) * restartBackoff if delay > restartBackoffMax { delay = restartBackoffMax } log.Infof("Service %s has failed %d times - delaying %s before restart", svc.name, svc.failures, delay) select { case <-time.After(delay): case <-svc.shutdown: goto done } } svc.restarts++ svc.lastRestart = time.Now() svc.runOnce() select { case <-time.After(restartDelay): case <-svc.shutdown: goto done } } done: svc.done <- true }
[ "func", "(", "svc", "*", "Service", ")", "run", "(", ")", "{", "// Wait for dependencies to start.", "for", "_", ",", "dep", ":=", "range", "svc", ".", "dependencies", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "svc", ".", "name", ",", "dep", ".", "name", ")", "\n", "select", "{", "case", "started", ":=", "<-", "dep", ".", "started", ":", "dep", ".", "started", "<-", "started", "\n", "case", "<-", "svc", ".", "shutdown", ":", "goto", "done", "\n", "}", "\n", "}", "\n\n", "for", "{", "if", "svc", ".", "failures", ">", "0", "{", "delay", ":=", "time", ".", "Duration", "(", "svc", ".", "failures", ")", "*", "restartBackoff", "\n", "if", "delay", ">", "restartBackoffMax", "{", "delay", "=", "restartBackoffMax", "\n", "}", "\n", "log", ".", "Infof", "(", "\"", "\"", ",", "svc", ".", "name", ",", "svc", ".", "failures", ",", "delay", ")", "\n\n", "select", "{", "case", "<-", "time", ".", "After", "(", "delay", ")", ":", "case", "<-", "svc", ".", "shutdown", ":", "goto", "done", "\n", "}", "\n", "}", "\n\n", "svc", ".", "restarts", "++", "\n", "svc", ".", "lastRestart", "=", "time", ".", "Now", "(", ")", "\n", "svc", ".", "runOnce", "(", ")", "\n\n", "select", "{", "case", "<-", "time", ".", "After", "(", "restartDelay", ")", ":", "case", "<-", "svc", ".", "shutdown", ":", "goto", "done", "\n", "}", "\n", "}", "\n", "done", ":", "svc", ".", "done", "<-", "true", "\n", "}" ]
// run runs a service and restarts it upon termination, unless a shutdown // notification has been received.
[ "run", "runs", "a", "service", "and", "restarts", "it", "upon", "termination", "unless", "a", "shutdown", "notification", "has", "been", "received", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L132-L173
train
google/seesaw
watchdog/service.go
logFile
func (svc *Service) logFile() (*os.File, error) { name := "seesaw_" + svc.name t := time.Now() logName := fmt.Sprintf("%s.log.%04d%02d%02d-%02d%02d%02d", name, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) f, err := os.Create(path.Join(logDir, logName)) if err != nil { return nil, err } logLink := path.Join(logDir, name+".log") os.Remove(logLink) os.Symlink(logName, logLink) fmt.Fprintf(f, "Log file for %s (stdout/stderr)\n", name) fmt.Fprintf(f, "Created at: %s\n", t.Format("2006/01/02 15:04:05")) return f, nil }
go
func (svc *Service) logFile() (*os.File, error) { name := "seesaw_" + svc.name t := time.Now() logName := fmt.Sprintf("%s.log.%04d%02d%02d-%02d%02d%02d", name, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) f, err := os.Create(path.Join(logDir, logName)) if err != nil { return nil, err } logLink := path.Join(logDir, name+".log") os.Remove(logLink) os.Symlink(logName, logLink) fmt.Fprintf(f, "Log file for %s (stdout/stderr)\n", name) fmt.Fprintf(f, "Created at: %s\n", t.Format("2006/01/02 15:04:05")) return f, nil }
[ "func", "(", "svc", "*", "Service", ")", "logFile", "(", ")", "(", "*", "os", ".", "File", ",", "error", ")", "{", "name", ":=", "\"", "\"", "+", "svc", ".", "name", "\n", "t", ":=", "time", ".", "Now", "(", ")", "\n", "logName", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "name", ",", "t", ".", "Year", "(", ")", ",", "t", ".", "Month", "(", ")", ",", "t", ".", "Day", "(", ")", ",", "t", ".", "Hour", "(", ")", ",", "t", ".", "Minute", "(", ")", ",", "t", ".", "Second", "(", ")", ")", "\n\n", "f", ",", "err", ":=", "os", ".", "Create", "(", "path", ".", "Join", "(", "logDir", ",", "logName", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "logLink", ":=", "path", ".", "Join", "(", "logDir", ",", "name", "+", "\"", "\"", ")", "\n", "os", ".", "Remove", "(", "logLink", ")", "\n", "os", ".", "Symlink", "(", "logName", ",", "logLink", ")", "\n\n", "fmt", ".", "Fprintf", "(", "f", ",", "\"", "\\n", "\"", ",", "name", ")", "\n", "fmt", ".", "Fprintf", "(", "f", ",", "\"", "\\n", "\"", ",", "t", ".", "Format", "(", "\"", "\"", ")", ")", "\n\n", "return", "f", ",", "nil", "\n", "}" ]
// logFile creates a log file for this service.
[ "logFile", "creates", "a", "log", "file", "for", "this", "service", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L176-L195
train
google/seesaw
watchdog/service.go
logSink
func (svc *Service) logSink(f *os.File, r io.ReadCloser) { _, err := io.Copy(f, r) if err != nil { log.Warningf("Service %s - log sink failed: %v", svc.name, err) } f.Close() r.Close() }
go
func (svc *Service) logSink(f *os.File, r io.ReadCloser) { _, err := io.Copy(f, r) if err != nil { log.Warningf("Service %s - log sink failed: %v", svc.name, err) } f.Close() r.Close() }
[ "func", "(", "svc", "*", "Service", ")", "logSink", "(", "f", "*", "os", ".", "File", ",", "r", "io", ".", "ReadCloser", ")", "{", "_", ",", "err", ":=", "io", ".", "Copy", "(", "f", ",", "r", ")", "\n", "if", "err", "!=", "nil", "{", "log", ".", "Warningf", "(", "\"", "\"", ",", "svc", ".", "name", ",", "err", ")", "\n", "}", "\n", "f", ".", "Close", "(", ")", "\n", "r", ".", "Close", "(", ")", "\n", "}" ]
// logSink copies output from the given reader to a log file, before closing // both the reader and the log file.
[ "logSink", "copies", "output", "from", "the", "given", "reader", "to", "a", "log", "file", "before", "closing", "both", "the", "reader", "and", "the", "log", "file", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L199-L206
train
google/seesaw
watchdog/service.go
signal
func (svc *Service) signal(sig os.Signal) error { svc.lock.Lock() defer svc.lock.Unlock() if svc.process == nil { return nil } return svc.process.Signal(sig) }
go
func (svc *Service) signal(sig os.Signal) error { svc.lock.Lock() defer svc.lock.Unlock() if svc.process == nil { return nil } return svc.process.Signal(sig) }
[ "func", "(", "svc", "*", "Service", ")", "signal", "(", "sig", "os", ".", "Signal", ")", "error", "{", "svc", ".", "lock", ".", "Lock", "(", ")", "\n", "defer", "svc", ".", "lock", ".", "Unlock", "(", ")", "\n", "if", "svc", ".", "process", "==", "nil", "{", "return", "nil", "\n", "}", "\n", "return", "svc", ".", "process", ".", "Signal", "(", "sig", ")", "\n", "}" ]
// signal sends a signal to the service.
[ "signal", "sends", "a", "signal", "to", "the", "service", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L298-L305
train
google/seesaw
watchdog/service.go
stop
func (svc *Service) stop() { // TODO(jsing): Check if it is actually running? log.Infof("Stopping service %s...", svc.name) // Wait for dependents to shutdown. for _, dep := range svc.dependents { log.Infof("Service %s waiting for %s to stop", svc.name, dep.name) stopped := <-dep.stopped dep.stopped <- stopped } svc.shutdown <- true svc.signal(syscall.SIGTERM) select { case <-svc.done: case <-time.After(svc.termTimeout): svc.signal(syscall.SIGKILL) <-svc.done } log.Infof("Service %s stopped", svc.name) svc.stopped <- true }
go
func (svc *Service) stop() { // TODO(jsing): Check if it is actually running? log.Infof("Stopping service %s...", svc.name) // Wait for dependents to shutdown. for _, dep := range svc.dependents { log.Infof("Service %s waiting for %s to stop", svc.name, dep.name) stopped := <-dep.stopped dep.stopped <- stopped } svc.shutdown <- true svc.signal(syscall.SIGTERM) select { case <-svc.done: case <-time.After(svc.termTimeout): svc.signal(syscall.SIGKILL) <-svc.done } log.Infof("Service %s stopped", svc.name) svc.stopped <- true }
[ "func", "(", "svc", "*", "Service", ")", "stop", "(", ")", "{", "// TODO(jsing): Check if it is actually running?", "log", ".", "Infof", "(", "\"", "\"", ",", "svc", ".", "name", ")", "\n\n", "// Wait for dependents to shutdown.", "for", "_", ",", "dep", ":=", "range", "svc", ".", "dependents", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "svc", ".", "name", ",", "dep", ".", "name", ")", "\n", "stopped", ":=", "<-", "dep", ".", "stopped", "\n", "dep", ".", "stopped", "<-", "stopped", "\n", "}", "\n\n", "svc", ".", "shutdown", "<-", "true", "\n", "svc", ".", "signal", "(", "syscall", ".", "SIGTERM", ")", "\n", "select", "{", "case", "<-", "svc", ".", "done", ":", "case", "<-", "time", ".", "After", "(", "svc", ".", "termTimeout", ")", ":", "svc", ".", "signal", "(", "syscall", ".", "SIGKILL", ")", "\n", "<-", "svc", ".", "done", "\n", "}", "\n", "log", ".", "Infof", "(", "\"", "\"", ",", "svc", ".", "name", ")", "\n", "svc", ".", "stopped", "<-", "true", "\n", "}" ]
// stop stops a running service.
[ "stop", "stops", "a", "running", "service", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/watchdog/service.go#L308-L329
train
google/seesaw
ncc/arp.go
bytes
func (m *arpMessage) bytes() ([]byte, error) { buf := new(bytes.Buffer) if err := binary.Write(buf, binary.BigEndian, m.arpHeader); err != nil { return nil, fmt.Errorf("binary write failed: %v", err) } buf.Write(m.senderHardwareAddress) buf.Write(m.senderProtocolAddress) buf.Write(m.targetHardwareAddress) buf.Write(m.targetProtocolAddress) return buf.Bytes(), nil }
go
func (m *arpMessage) bytes() ([]byte, error) { buf := new(bytes.Buffer) if err := binary.Write(buf, binary.BigEndian, m.arpHeader); err != nil { return nil, fmt.Errorf("binary write failed: %v", err) } buf.Write(m.senderHardwareAddress) buf.Write(m.senderProtocolAddress) buf.Write(m.targetHardwareAddress) buf.Write(m.targetProtocolAddress) return buf.Bytes(), nil }
[ "func", "(", "m", "*", "arpMessage", ")", "bytes", "(", ")", "(", "[", "]", "byte", ",", "error", ")", "{", "buf", ":=", "new", "(", "bytes", ".", "Buffer", ")", "\n\n", "if", "err", ":=", "binary", ".", "Write", "(", "buf", ",", "binary", ".", "BigEndian", ",", "m", ".", "arpHeader", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "buf", ".", "Write", "(", "m", ".", "senderHardwareAddress", ")", "\n", "buf", ".", "Write", "(", "m", ".", "senderProtocolAddress", ")", "\n", "buf", ".", "Write", "(", "m", ".", "targetHardwareAddress", ")", "\n", "buf", ".", "Write", "(", "m", ".", "targetProtocolAddress", ")", "\n\n", "return", "buf", ".", "Bytes", "(", ")", ",", "nil", "\n", "}" ]
// bytes returns the wire representation of the ARP message.
[ "bytes", "returns", "the", "wire", "representation", "of", "the", "ARP", "message", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/arp.go#L70-L82
train
google/seesaw
ncc/arp.go
gratuitousARPReply
func gratuitousARPReply(ip net.IP, mac net.HardwareAddr) (*arpMessage, error) { if ip.To4() == nil { return nil, fmt.Errorf("%q is not an IPv4 address", ip) } if len(mac) != hwLen { return nil, fmt.Errorf("%q is not an Ethernet MAC address", mac) } m := &arpMessage{ arpHeader{ 1, // Ethernet 0x0800, // IPv4 hwLen, // 48-bit MAC Address net.IPv4len, // 32-bit IPv4 Address opARPReply, // ARP Reply }, mac, ip.To4(), ethernetBroadcast, net.IPv4bcast, } return m, nil }
go
func gratuitousARPReply(ip net.IP, mac net.HardwareAddr) (*arpMessage, error) { if ip.To4() == nil { return nil, fmt.Errorf("%q is not an IPv4 address", ip) } if len(mac) != hwLen { return nil, fmt.Errorf("%q is not an Ethernet MAC address", mac) } m := &arpMessage{ arpHeader{ 1, // Ethernet 0x0800, // IPv4 hwLen, // 48-bit MAC Address net.IPv4len, // 32-bit IPv4 Address opARPReply, // ARP Reply }, mac, ip.To4(), ethernetBroadcast, net.IPv4bcast, } return m, nil }
[ "func", "gratuitousARPReply", "(", "ip", "net", ".", "IP", ",", "mac", "net", ".", "HardwareAddr", ")", "(", "*", "arpMessage", ",", "error", ")", "{", "if", "ip", ".", "To4", "(", ")", "==", "nil", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "ip", ")", "\n", "}", "\n", "if", "len", "(", "mac", ")", "!=", "hwLen", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "mac", ")", "\n", "}", "\n\n", "m", ":=", "&", "arpMessage", "{", "arpHeader", "{", "1", ",", "// Ethernet", "0x0800", ",", "// IPv4", "hwLen", ",", "// 48-bit MAC Address", "net", ".", "IPv4len", ",", "// 32-bit IPv4 Address", "opARPReply", ",", "// ARP Reply", "}", ",", "mac", ",", "ip", ".", "To4", "(", ")", ",", "ethernetBroadcast", ",", "net", ".", "IPv4bcast", ",", "}", "\n\n", "return", "m", ",", "nil", "\n", "}" ]
// gratuitousARPReply returns an ARP message that contains a gratuitous ARP // reply from the specified sender.
[ "gratuitousARPReply", "returns", "an", "ARP", "message", "that", "contains", "a", "gratuitous", "ARP", "reply", "from", "the", "specified", "sender", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/arp.go#L86-L109
train
google/seesaw
ncc/arp.go
sendARP
func sendARP(iface *net.Interface, m *arpMessage) error { fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_DGRAM, int(htons(syscall.ETH_P_ARP))) if err != nil { return fmt.Errorf("failed to get raw socket: %v", err) } defer syscall.Close(fd) if err := syscall.BindToDevice(fd, iface.Name); err != nil { return fmt.Errorf("failed to bind to device: %v", err) } ll := syscall.SockaddrLinklayer{ Protocol: htons(syscall.ETH_P_ARP), Ifindex: iface.Index, Pkttype: 0, // syscall.PACKET_HOST Hatype: m.hardwareType, Halen: m.hardwareAddressLength, } target := ethernetBroadcast if m.opcode == opARPReply { target = m.targetHardwareAddress } for i := 0; i < len(target); i++ { ll.Addr[i] = target[i] } b, err := m.bytes() if err != nil { return fmt.Errorf("failed to convert ARP message: %v", err) } if err := syscall.Bind(fd, &ll); err != nil { return fmt.Errorf("failed to bind: %v", err) } if err := syscall.Sendto(fd, b, 0, &ll); err != nil { return fmt.Errorf("failed to send: %v", err) } return nil }
go
func sendARP(iface *net.Interface, m *arpMessage) error { fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_DGRAM, int(htons(syscall.ETH_P_ARP))) if err != nil { return fmt.Errorf("failed to get raw socket: %v", err) } defer syscall.Close(fd) if err := syscall.BindToDevice(fd, iface.Name); err != nil { return fmt.Errorf("failed to bind to device: %v", err) } ll := syscall.SockaddrLinklayer{ Protocol: htons(syscall.ETH_P_ARP), Ifindex: iface.Index, Pkttype: 0, // syscall.PACKET_HOST Hatype: m.hardwareType, Halen: m.hardwareAddressLength, } target := ethernetBroadcast if m.opcode == opARPReply { target = m.targetHardwareAddress } for i := 0; i < len(target); i++ { ll.Addr[i] = target[i] } b, err := m.bytes() if err != nil { return fmt.Errorf("failed to convert ARP message: %v", err) } if err := syscall.Bind(fd, &ll); err != nil { return fmt.Errorf("failed to bind: %v", err) } if err := syscall.Sendto(fd, b, 0, &ll); err != nil { return fmt.Errorf("failed to send: %v", err) } return nil }
[ "func", "sendARP", "(", "iface", "*", "net", ".", "Interface", ",", "m", "*", "arpMessage", ")", "error", "{", "fd", ",", "err", ":=", "syscall", ".", "Socket", "(", "syscall", ".", "AF_PACKET", ",", "syscall", ".", "SOCK_DGRAM", ",", "int", "(", "htons", "(", "syscall", ".", "ETH_P_ARP", ")", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "defer", "syscall", ".", "Close", "(", "fd", ")", "\n\n", "if", "err", ":=", "syscall", ".", "BindToDevice", "(", "fd", ",", "iface", ".", "Name", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "ll", ":=", "syscall", ".", "SockaddrLinklayer", "{", "Protocol", ":", "htons", "(", "syscall", ".", "ETH_P_ARP", ")", ",", "Ifindex", ":", "iface", ".", "Index", ",", "Pkttype", ":", "0", ",", "// syscall.PACKET_HOST", "Hatype", ":", "m", ".", "hardwareType", ",", "Halen", ":", "m", ".", "hardwareAddressLength", ",", "}", "\n", "target", ":=", "ethernetBroadcast", "\n", "if", "m", ".", "opcode", "==", "opARPReply", "{", "target", "=", "m", ".", "targetHardwareAddress", "\n", "}", "\n", "for", "i", ":=", "0", ";", "i", "<", "len", "(", "target", ")", ";", "i", "++", "{", "ll", ".", "Addr", "[", "i", "]", "=", "target", "[", "i", "]", "\n", "}", "\n\n", "b", ",", "err", ":=", "m", ".", "bytes", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "if", "err", ":=", "syscall", ".", "Bind", "(", "fd", ",", "&", "ll", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "if", "err", ":=", "syscall", ".", "Sendto", "(", "fd", ",", "b", ",", "0", ",", "&", "ll", ")", ";", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// sendARP sends the given ARP message via the specified interface.
[ "sendARP", "sends", "the", "given", "ARP", "message", "via", "the", "specified", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/arp.go#L112-L151
train
google/seesaw
ncc/arp.go
ARPSendGratuitous
func (ncc *SeesawNCC) ARPSendGratuitous(arp *ncctypes.ARPGratuitous, out *int) error { iface, err := net.InterfaceByName(arp.IfaceName) if err != nil { return fmt.Errorf("failed to get interface %q: %v", arp.IfaceName, err) } log.V(2).Infof("Sending gratuitous ARP for %s (%s) via %s", arp.IP, iface.HardwareAddr, iface.Name) m, err := gratuitousARPReply(arp.IP, iface.HardwareAddr) if err != nil { return err } return sendARP(iface, m) }
go
func (ncc *SeesawNCC) ARPSendGratuitous(arp *ncctypes.ARPGratuitous, out *int) error { iface, err := net.InterfaceByName(arp.IfaceName) if err != nil { return fmt.Errorf("failed to get interface %q: %v", arp.IfaceName, err) } log.V(2).Infof("Sending gratuitous ARP for %s (%s) via %s", arp.IP, iface.HardwareAddr, iface.Name) m, err := gratuitousARPReply(arp.IP, iface.HardwareAddr) if err != nil { return err } return sendARP(iface, m) }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "ARPSendGratuitous", "(", "arp", "*", "ncctypes", ".", "ARPGratuitous", ",", "out", "*", "int", ")", "error", "{", "iface", ",", "err", ":=", "net", ".", "InterfaceByName", "(", "arp", ".", "IfaceName", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "arp", ".", "IfaceName", ",", "err", ")", "\n", "}", "\n", "log", ".", "V", "(", "2", ")", ".", "Infof", "(", "\"", "\"", ",", "arp", ".", "IP", ",", "iface", ".", "HardwareAddr", ",", "iface", ".", "Name", ")", "\n", "m", ",", "err", ":=", "gratuitousARPReply", "(", "arp", ".", "IP", ",", "iface", ".", "HardwareAddr", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "return", "sendARP", "(", "iface", ",", "m", ")", "\n", "}" ]
// ARPSendGratuitous sends a gratuitous ARP message via the specified interface.
[ "ARPSendGratuitous", "sends", "a", "gratuitous", "ARP", "message", "via", "the", "specified", "interface", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/arp.go#L154-L165
train
google/seesaw
healthcheck/radius.go
String
func (rc radiusCode) String() string { if name, ok := rcNames[rc]; ok { return name } return fmt.Sprintf("(unknown %d)", rc) }
go
func (rc radiusCode) String() string { if name, ok := rcNames[rc]; ok { return name } return fmt.Sprintf("(unknown %d)", rc) }
[ "func", "(", "rc", "radiusCode", ")", "String", "(", ")", "string", "{", "if", "name", ",", "ok", ":=", "rcNames", "[", "rc", "]", ";", "ok", "{", "return", "name", "\n", "}", "\n", "return", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "rc", ")", "\n", "}" ]
// String returns the string representation of a RADIUS code.
[ "String", "returns", "the", "string", "representation", "of", "a", "RADIUS", "code", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/radius.go#L81-L86
train
google/seesaw
healthcheck/radius.go
String
func (rat radiusAttributeType) String() string { if name, ok := ratNames[rat]; ok { return name } return fmt.Sprintf("(unknown %d)", rat) }
go
func (rat radiusAttributeType) String() string { if name, ok := ratNames[rat]; ok { return name } return fmt.Sprintf("(unknown %d)", rat) }
[ "func", "(", "rat", "radiusAttributeType", ")", "String", "(", ")", "string", "{", "if", "name", ",", "ok", ":=", "ratNames", "[", "rat", "]", ";", "ok", "{", "return", "name", "\n", "}", "\n", "return", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "rat", ")", "\n", "}" ]
// String returns the string representation of a RADIUS attribute type.
[ "String", "returns", "the", "string", "representation", "of", "a", "RADIUS", "attribute", "type", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/radius.go#L144-L149
train
google/seesaw
healthcheck/radius.go
radiusPassword
func radiusPassword(passwd, secret string, authenticator *radiusAuthenticator) []byte { const blockSize = 16 length := (len(passwd) + 0xf) &^ 0xf if length > 128 { length = 128 } blocks := make([]byte, length) copy(blocks, []byte(passwd)) for i := 0; i < length; i += blockSize { hash := md5.New() hash.Write([]byte(secret)) if i >= blockSize { hash.Write(blocks[i-blockSize : i]) } else { hash.Write(authenticator[:]) } h := hash.Sum(nil) for j := 0; j < blockSize; j++ { blocks[i+j] ^= h[j] } } return blocks }
go
func radiusPassword(passwd, secret string, authenticator *radiusAuthenticator) []byte { const blockSize = 16 length := (len(passwd) + 0xf) &^ 0xf if length > 128 { length = 128 } blocks := make([]byte, length) copy(blocks, []byte(passwd)) for i := 0; i < length; i += blockSize { hash := md5.New() hash.Write([]byte(secret)) if i >= blockSize { hash.Write(blocks[i-blockSize : i]) } else { hash.Write(authenticator[:]) } h := hash.Sum(nil) for j := 0; j < blockSize; j++ { blocks[i+j] ^= h[j] } } return blocks }
[ "func", "radiusPassword", "(", "passwd", ",", "secret", "string", ",", "authenticator", "*", "radiusAuthenticator", ")", "[", "]", "byte", "{", "const", "blockSize", "=", "16", "\n", "length", ":=", "(", "len", "(", "passwd", ")", "+", "0xf", ")", "&^", "0xf", "\n", "if", "length", ">", "128", "{", "length", "=", "128", "\n", "}", "\n", "blocks", ":=", "make", "(", "[", "]", "byte", ",", "length", ")", "\n", "copy", "(", "blocks", ",", "[", "]", "byte", "(", "passwd", ")", ")", "\n", "for", "i", ":=", "0", ";", "i", "<", "length", ";", "i", "+=", "blockSize", "{", "hash", ":=", "md5", ".", "New", "(", ")", "\n", "hash", ".", "Write", "(", "[", "]", "byte", "(", "secret", ")", ")", "\n", "if", "i", ">=", "blockSize", "{", "hash", ".", "Write", "(", "blocks", "[", "i", "-", "blockSize", ":", "i", "]", ")", "\n", "}", "else", "{", "hash", ".", "Write", "(", "authenticator", "[", ":", "]", ")", "\n", "}", "\n", "h", ":=", "hash", ".", "Sum", "(", "nil", ")", "\n", "for", "j", ":=", "0", ";", "j", "<", "blockSize", ";", "j", "++", "{", "blocks", "[", "i", "+", "j", "]", "^=", "h", "[", "j", "]", "\n", "}", "\n", "}", "\n", "return", "blocks", "\n", "}" ]
// radiusPassword encodes a password using the algorithm described in RFC2865 // section 5.2.
[ "radiusPassword", "encodes", "a", "password", "using", "the", "algorithm", "described", "in", "RFC2865", "section", "5", ".", "2", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/radius.go#L241-L263
train
google/seesaw
healthcheck/radius.go
responseAuthenticator
func responseAuthenticator(rp *radiusPacket, requestAuthenticator *radiusAuthenticator, secret string) (*radiusAuthenticator, error) { hash := md5.New() hash.Write([]byte{byte(rp.Code), byte(rp.Identifier)}) if err := binary.Write(hash, binary.BigEndian, rp.Length); err != nil { return nil, err } hash.Write(requestAuthenticator[:]) for _, ra := range rp.attributes { hash.Write(ra.encode()) } hash.Write([]byte(secret)) h := hash.Sum(nil) var authenticator radiusAuthenticator copy(authenticator[:], h[:]) return &authenticator, nil }
go
func responseAuthenticator(rp *radiusPacket, requestAuthenticator *radiusAuthenticator, secret string) (*radiusAuthenticator, error) { hash := md5.New() hash.Write([]byte{byte(rp.Code), byte(rp.Identifier)}) if err := binary.Write(hash, binary.BigEndian, rp.Length); err != nil { return nil, err } hash.Write(requestAuthenticator[:]) for _, ra := range rp.attributes { hash.Write(ra.encode()) } hash.Write([]byte(secret)) h := hash.Sum(nil) var authenticator radiusAuthenticator copy(authenticator[:], h[:]) return &authenticator, nil }
[ "func", "responseAuthenticator", "(", "rp", "*", "radiusPacket", ",", "requestAuthenticator", "*", "radiusAuthenticator", ",", "secret", "string", ")", "(", "*", "radiusAuthenticator", ",", "error", ")", "{", "hash", ":=", "md5", ".", "New", "(", ")", "\n", "hash", ".", "Write", "(", "[", "]", "byte", "{", "byte", "(", "rp", ".", "Code", ")", ",", "byte", "(", "rp", ".", "Identifier", ")", "}", ")", "\n", "if", "err", ":=", "binary", ".", "Write", "(", "hash", ",", "binary", ".", "BigEndian", ",", "rp", ".", "Length", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "hash", ".", "Write", "(", "requestAuthenticator", "[", ":", "]", ")", "\n", "for", "_", ",", "ra", ":=", "range", "rp", ".", "attributes", "{", "hash", ".", "Write", "(", "ra", ".", "encode", "(", ")", ")", "\n", "}", "\n", "hash", ".", "Write", "(", "[", "]", "byte", "(", "secret", ")", ")", "\n", "h", ":=", "hash", ".", "Sum", "(", "nil", ")", "\n", "var", "authenticator", "radiusAuthenticator", "\n", "copy", "(", "authenticator", "[", ":", "]", ",", "h", "[", ":", "]", ")", "\n", "return", "&", "authenticator", ",", "nil", "\n", "}" ]
// responseAuthenticator calculates the response authenticator for a RADIUS // response packet, as per RFC2865 section 3.
[ "responseAuthenticator", "calculates", "the", "response", "authenticator", "for", "a", "RADIUS", "response", "packet", "as", "per", "RFC2865", "section", "3", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/radius.go#L267-L282
train
google/seesaw
healthcheck/radius.go
NewRADIUSChecker
func NewRADIUSChecker(ip net.IP, port int) *RADIUSChecker { return &RADIUSChecker{ Target: Target{ IP: ip, Port: port, Proto: seesaw.IPProtoUDP, }, Response: "accept", } }
go
func NewRADIUSChecker(ip net.IP, port int) *RADIUSChecker { return &RADIUSChecker{ Target: Target{ IP: ip, Port: port, Proto: seesaw.IPProtoUDP, }, Response: "accept", } }
[ "func", "NewRADIUSChecker", "(", "ip", "net", ".", "IP", ",", "port", "int", ")", "*", "RADIUSChecker", "{", "return", "&", "RADIUSChecker", "{", "Target", ":", "Target", "{", "IP", ":", "ip", ",", "Port", ":", "port", ",", "Proto", ":", "seesaw", ".", "IPProtoUDP", ",", "}", ",", "Response", ":", "\"", "\"", ",", "}", "\n", "}" ]
// NewRADIUSChecker returns an initialised RADIUSChecker.
[ "NewRADIUSChecker", "returns", "an", "initialised", "RADIUSChecker", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/radius.go#L294-L303
train
google/seesaw
engine/config/types.go
NewCluster
func NewCluster(site string) *Cluster { return &Cluster{ Site: site, BGPPeers: make(map[string]*seesaw.Host), Nodes: make(map[string]*seesaw.Node), VIPSubnets: make(map[string]*net.IPNet), Vservers: make(map[string]*Vserver), VLANs: make(map[uint16]*seesaw.VLAN), Status: seesaw.ConfigStatus{ Warnings: make([]string, 0), Attributes: make([]seesaw.ConfigMetadata, 0), }, } }
go
func NewCluster(site string) *Cluster { return &Cluster{ Site: site, BGPPeers: make(map[string]*seesaw.Host), Nodes: make(map[string]*seesaw.Node), VIPSubnets: make(map[string]*net.IPNet), Vservers: make(map[string]*Vserver), VLANs: make(map[uint16]*seesaw.VLAN), Status: seesaw.ConfigStatus{ Warnings: make([]string, 0), Attributes: make([]seesaw.ConfigMetadata, 0), }, } }
[ "func", "NewCluster", "(", "site", "string", ")", "*", "Cluster", "{", "return", "&", "Cluster", "{", "Site", ":", "site", ",", "BGPPeers", ":", "make", "(", "map", "[", "string", "]", "*", "seesaw", ".", "Host", ")", ",", "Nodes", ":", "make", "(", "map", "[", "string", "]", "*", "seesaw", ".", "Node", ")", ",", "VIPSubnets", ":", "make", "(", "map", "[", "string", "]", "*", "net", ".", "IPNet", ")", ",", "Vservers", ":", "make", "(", "map", "[", "string", "]", "*", "Vserver", ")", ",", "VLANs", ":", "make", "(", "map", "[", "uint16", "]", "*", "seesaw", ".", "VLAN", ")", ",", "Status", ":", "seesaw", ".", "ConfigStatus", "{", "Warnings", ":", "make", "(", "[", "]", "string", ",", "0", ")", ",", "Attributes", ":", "make", "(", "[", "]", "seesaw", ".", "ConfigMetadata", ",", "0", ")", ",", "}", ",", "}", "\n", "}" ]
// NewCluster returns an initialised Cluster structure.
[ "NewCluster", "returns", "an", "initialised", "Cluster", "structure", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L45-L58
train
google/seesaw
engine/config/types.go
AddBGPPeer
func (c *Cluster) AddBGPPeer(peer *seesaw.Host) error { key := peer.Hostname if _, ok := c.BGPPeers[key]; ok { return fmt.Errorf("Cluster %q already contains peer %q", c.Site, key) } c.BGPPeers[key] = peer return nil }
go
func (c *Cluster) AddBGPPeer(peer *seesaw.Host) error { key := peer.Hostname if _, ok := c.BGPPeers[key]; ok { return fmt.Errorf("Cluster %q already contains peer %q", c.Site, key) } c.BGPPeers[key] = peer return nil }
[ "func", "(", "c", "*", "Cluster", ")", "AddBGPPeer", "(", "peer", "*", "seesaw", ".", "Host", ")", "error", "{", "key", ":=", "peer", ".", "Hostname", "\n", "if", "_", ",", "ok", ":=", "c", ".", "BGPPeers", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "c", ".", "Site", ",", "key", ")", "\n", "}", "\n", "c", ".", "BGPPeers", "[", "key", "]", "=", "peer", "\n", "return", "nil", "\n", "}" ]
// AddBGPPeer adds a BGP peer to a Seesaw Cluster.
[ "AddBGPPeer", "adds", "a", "BGP", "peer", "to", "a", "Seesaw", "Cluster", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L61-L68
train
google/seesaw
engine/config/types.go
AddNode
func (c *Cluster) AddNode(node *seesaw.Node) error { key := node.Key() if _, ok := c.Nodes[key]; ok { return fmt.Errorf("Cluster %q already contains Node %q", c.Site, key) } c.Nodes[key] = node return nil }
go
func (c *Cluster) AddNode(node *seesaw.Node) error { key := node.Key() if _, ok := c.Nodes[key]; ok { return fmt.Errorf("Cluster %q already contains Node %q", c.Site, key) } c.Nodes[key] = node return nil }
[ "func", "(", "c", "*", "Cluster", ")", "AddNode", "(", "node", "*", "seesaw", ".", "Node", ")", "error", "{", "key", ":=", "node", ".", "Key", "(", ")", "\n", "if", "_", ",", "ok", ":=", "c", ".", "Nodes", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "c", ".", "Site", ",", "key", ")", "\n", "}", "\n", "c", ".", "Nodes", "[", "key", "]", "=", "node", "\n", "return", "nil", "\n", "}" ]
// AddNode adds a Seesaw Node to a Seesaw Cluster.
[ "AddNode", "adds", "a", "Seesaw", "Node", "to", "a", "Seesaw", "Cluster", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L71-L78
train
google/seesaw
engine/config/types.go
AddVIPSubnet
func (c *Cluster) AddVIPSubnet(subnet *net.IPNet) error { key := subnet.String() if _, ok := c.VIPSubnets[key]; ok { return fmt.Errorf("Cluster %q already contains VIP Subnet %q", c.Site, key) } c.VIPSubnets[key] = subnet return nil }
go
func (c *Cluster) AddVIPSubnet(subnet *net.IPNet) error { key := subnet.String() if _, ok := c.VIPSubnets[key]; ok { return fmt.Errorf("Cluster %q already contains VIP Subnet %q", c.Site, key) } c.VIPSubnets[key] = subnet return nil }
[ "func", "(", "c", "*", "Cluster", ")", "AddVIPSubnet", "(", "subnet", "*", "net", ".", "IPNet", ")", "error", "{", "key", ":=", "subnet", ".", "String", "(", ")", "\n", "if", "_", ",", "ok", ":=", "c", ".", "VIPSubnets", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "c", ".", "Site", ",", "key", ")", "\n", "}", "\n", "c", ".", "VIPSubnets", "[", "key", "]", "=", "subnet", "\n", "return", "nil", "\n", "}" ]
// AddVIPSubnet adds a VIP Subnet to a Seesaw Cluster.
[ "AddVIPSubnet", "adds", "a", "VIP", "Subnet", "to", "a", "Seesaw", "Cluster", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L81-L88
train
google/seesaw
engine/config/types.go
AddVserver
func (c *Cluster) AddVserver(vserver *Vserver) error { key := vserver.Key() if _, ok := c.Vservers[key]; ok { return fmt.Errorf("Cluster %q already contains Vserver %q", c.Site, key) } c.Vservers[key] = vserver return nil }
go
func (c *Cluster) AddVserver(vserver *Vserver) error { key := vserver.Key() if _, ok := c.Vservers[key]; ok { return fmt.Errorf("Cluster %q already contains Vserver %q", c.Site, key) } c.Vservers[key] = vserver return nil }
[ "func", "(", "c", "*", "Cluster", ")", "AddVserver", "(", "vserver", "*", "Vserver", ")", "error", "{", "key", ":=", "vserver", ".", "Key", "(", ")", "\n", "if", "_", ",", "ok", ":=", "c", ".", "Vservers", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "c", ".", "Site", ",", "key", ")", "\n", "}", "\n", "c", ".", "Vservers", "[", "key", "]", "=", "vserver", "\n", "return", "nil", "\n", "}" ]
// AddVserver adds a Vserver to a Seesaw Cluster.
[ "AddVserver", "adds", "a", "Vserver", "to", "a", "Seesaw", "Cluster", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L91-L98
train
google/seesaw
engine/config/types.go
AddVLAN
func (c *Cluster) AddVLAN(vlan *seesaw.VLAN) error { key := vlan.Key() if _, ok := c.VLANs[key]; ok { return fmt.Errorf("Cluster %q already contains VLAN %q", c.Site, key) } c.VLANs[key] = vlan return nil }
go
func (c *Cluster) AddVLAN(vlan *seesaw.VLAN) error { key := vlan.Key() if _, ok := c.VLANs[key]; ok { return fmt.Errorf("Cluster %q already contains VLAN %q", c.Site, key) } c.VLANs[key] = vlan return nil }
[ "func", "(", "c", "*", "Cluster", ")", "AddVLAN", "(", "vlan", "*", "seesaw", ".", "VLAN", ")", "error", "{", "key", ":=", "vlan", ".", "Key", "(", ")", "\n", "if", "_", ",", "ok", ":=", "c", ".", "VLANs", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "c", ".", "Site", ",", "key", ")", "\n", "}", "\n", "c", ".", "VLANs", "[", "key", "]", "=", "vlan", "\n", "return", "nil", "\n", "}" ]
// AddVLAN adds a VLAN to a Seesaw Cluster.
[ "AddVLAN", "adds", "a", "VLAN", "to", "a", "Seesaw", "Cluster", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L101-L108
train
google/seesaw
engine/config/types.go
Equal
func (c *Cluster) Equal(other *Cluster) bool { return reflect.DeepEqual(c, other) }
go
func (c *Cluster) Equal(other *Cluster) bool { return reflect.DeepEqual(c, other) }
[ "func", "(", "c", "*", "Cluster", ")", "Equal", "(", "other", "*", "Cluster", ")", "bool", "{", "return", "reflect", ".", "DeepEqual", "(", "c", ",", "other", ")", "\n", "}" ]
// Equal reports whether this cluster is equal to the given cluster.
[ "Equal", "reports", "whether", "this", "cluster", "is", "equal", "to", "the", "given", "cluster", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L111-L113
train
google/seesaw
engine/config/types.go
NewVserver
func NewVserver(name string, host seesaw.Host) *Vserver { return &Vserver{ Name: name, Host: host, Entries: make(map[string]*VserverEntry), Backends: make(map[string]*seesaw.Backend), Healthchecks: make(map[string]*Healthcheck), VIPs: make(map[string]*seesaw.VIP), Warnings: make([]string, 0), } }
go
func NewVserver(name string, host seesaw.Host) *Vserver { return &Vserver{ Name: name, Host: host, Entries: make(map[string]*VserverEntry), Backends: make(map[string]*seesaw.Backend), Healthchecks: make(map[string]*Healthcheck), VIPs: make(map[string]*seesaw.VIP), Warnings: make([]string, 0), } }
[ "func", "NewVserver", "(", "name", "string", ",", "host", "seesaw", ".", "Host", ")", "*", "Vserver", "{", "return", "&", "Vserver", "{", "Name", ":", "name", ",", "Host", ":", "host", ",", "Entries", ":", "make", "(", "map", "[", "string", "]", "*", "VserverEntry", ")", ",", "Backends", ":", "make", "(", "map", "[", "string", "]", "*", "seesaw", ".", "Backend", ")", ",", "Healthchecks", ":", "make", "(", "map", "[", "string", "]", "*", "Healthcheck", ")", ",", "VIPs", ":", "make", "(", "map", "[", "string", "]", "*", "seesaw", ".", "VIP", ")", ",", "Warnings", ":", "make", "(", "[", "]", "string", ",", "0", ")", ",", "}", "\n", "}" ]
// NewVserver creates a new, initialised Vserver structure.
[ "NewVserver", "creates", "a", "new", "initialised", "Vserver", "structure", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L129-L139
train
google/seesaw
engine/config/types.go
AddVserverEntry
func (v *Vserver) AddVserverEntry(e *VserverEntry) error { key := e.Key() if _, ok := v.Entries[key]; ok { return fmt.Errorf("Vserver %q already contains VserverEntry %q", v.Name, key) } v.Entries[key] = e return nil }
go
func (v *Vserver) AddVserverEntry(e *VserverEntry) error { key := e.Key() if _, ok := v.Entries[key]; ok { return fmt.Errorf("Vserver %q already contains VserverEntry %q", v.Name, key) } v.Entries[key] = e return nil }
[ "func", "(", "v", "*", "Vserver", ")", "AddVserverEntry", "(", "e", "*", "VserverEntry", ")", "error", "{", "key", ":=", "e", ".", "Key", "(", ")", "\n", "if", "_", ",", "ok", ":=", "v", ".", "Entries", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "v", ".", "Name", ",", "key", ")", "\n", "}", "\n", "v", ".", "Entries", "[", "key", "]", "=", "e", "\n", "return", "nil", "\n", "}" ]
// AddVserverEntry adds an VserverEntry to a Vserver.
[ "AddVserverEntry", "adds", "an", "VserverEntry", "to", "a", "Vserver", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L147-L154
train
google/seesaw
engine/config/types.go
AddBackend
func (v *Vserver) AddBackend(backend *seesaw.Backend) error { key := backend.Key() if _, ok := v.Backends[key]; ok { return fmt.Errorf("Vserver %q already contains Backend %q", v.Name, key) } v.Backends[key] = backend return nil }
go
func (v *Vserver) AddBackend(backend *seesaw.Backend) error { key := backend.Key() if _, ok := v.Backends[key]; ok { return fmt.Errorf("Vserver %q already contains Backend %q", v.Name, key) } v.Backends[key] = backend return nil }
[ "func", "(", "v", "*", "Vserver", ")", "AddBackend", "(", "backend", "*", "seesaw", ".", "Backend", ")", "error", "{", "key", ":=", "backend", ".", "Key", "(", ")", "\n", "if", "_", ",", "ok", ":=", "v", ".", "Backends", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "v", ".", "Name", ",", "key", ")", "\n", "}", "\n", "v", ".", "Backends", "[", "key", "]", "=", "backend", "\n", "return", "nil", "\n", "}" ]
// AddBackend adds a Backend to a Vserver.
[ "AddBackend", "adds", "a", "Backend", "to", "a", "Vserver", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L157-L164
train
google/seesaw
engine/config/types.go
AddHealthcheck
func (v *Vserver) AddHealthcheck(h *Healthcheck) error { key := h.Key() if _, ok := v.Healthchecks[key]; ok { return fmt.Errorf("Vserver %q already contains Healthcheck %q", v.Name, key) } v.Healthchecks[key] = h return nil }
go
func (v *Vserver) AddHealthcheck(h *Healthcheck) error { key := h.Key() if _, ok := v.Healthchecks[key]; ok { return fmt.Errorf("Vserver %q already contains Healthcheck %q", v.Name, key) } v.Healthchecks[key] = h return nil }
[ "func", "(", "v", "*", "Vserver", ")", "AddHealthcheck", "(", "h", "*", "Healthcheck", ")", "error", "{", "key", ":=", "h", ".", "Key", "(", ")", "\n", "if", "_", ",", "ok", ":=", "v", ".", "Healthchecks", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "v", ".", "Name", ",", "key", ")", "\n", "}", "\n", "v", ".", "Healthchecks", "[", "key", "]", "=", "h", "\n", "return", "nil", "\n", "}" ]
// AddHealthcheck adds a Healthcheck to a Vserver.
[ "AddHealthcheck", "adds", "a", "Healthcheck", "to", "a", "Vserver", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L167-L174
train
google/seesaw
engine/config/types.go
AddVIP
func (v *Vserver) AddVIP(vip *seesaw.VIP) error { key := vip.String() if _, ok := v.VIPs[key]; ok { return fmt.Errorf("Vserver %q already contains VIP %q", v.Name, key) } v.VIPs[key] = vip return nil }
go
func (v *Vserver) AddVIP(vip *seesaw.VIP) error { key := vip.String() if _, ok := v.VIPs[key]; ok { return fmt.Errorf("Vserver %q already contains VIP %q", v.Name, key) } v.VIPs[key] = vip return nil }
[ "func", "(", "v", "*", "Vserver", ")", "AddVIP", "(", "vip", "*", "seesaw", ".", "VIP", ")", "error", "{", "key", ":=", "vip", ".", "String", "(", ")", "\n", "if", "_", ",", "ok", ":=", "v", ".", "VIPs", "[", "key", "]", ";", "ok", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "v", ".", "Name", ",", "key", ")", "\n", "}", "\n", "v", ".", "VIPs", "[", "key", "]", "=", "vip", "\n", "return", "nil", "\n", "}" ]
// AddVIP adds a VIP to a Vserver.
[ "AddVIP", "adds", "a", "VIP", "to", "a", "Vserver", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L177-L184
train
google/seesaw
engine/config/types.go
NewVserverEntry
func NewVserverEntry(port uint16, proto seesaw.IPProto) *VserverEntry { return &VserverEntry{ Port: port, Proto: proto, Healthchecks: make(map[string]*Healthcheck), } }
go
func NewVserverEntry(port uint16, proto seesaw.IPProto) *VserverEntry { return &VserverEntry{ Port: port, Proto: proto, Healthchecks: make(map[string]*Healthcheck), } }
[ "func", "NewVserverEntry", "(", "port", "uint16", ",", "proto", "seesaw", ".", "IPProto", ")", "*", "VserverEntry", "{", "return", "&", "VserverEntry", "{", "Port", ":", "port", ",", "Proto", ":", "proto", ",", "Healthchecks", ":", "make", "(", "map", "[", "string", "]", "*", "Healthcheck", ")", ",", "}", "\n", "}" ]
// NewVserverEntry creates a new, initialised VserverEntry structure.
[ "NewVserverEntry", "creates", "a", "new", "initialised", "VserverEntry", "structure", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L203-L209
train
google/seesaw
engine/config/types.go
Key
func (v *VserverEntry) Key() string { return fmt.Sprintf("%d/%s", v.Port, v.Proto) }
go
func (v *VserverEntry) Key() string { return fmt.Sprintf("%d/%s", v.Port, v.Proto) }
[ "func", "(", "v", "*", "VserverEntry", ")", "Key", "(", ")", "string", "{", "return", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "v", ".", "Port", ",", "v", ".", "Proto", ")", "\n", "}" ]
// Key returns the unique identifier for a VserverEntry.
[ "Key", "returns", "the", "unique", "identifier", "for", "a", "VserverEntry", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L222-L224
train
google/seesaw
engine/config/types.go
Snapshot
func (v *VserverEntry) Snapshot() *seesaw.VserverEntry { return &seesaw.VserverEntry{ Port: v.Port, Proto: v.Proto, Scheduler: v.Scheduler, Mode: v.Mode, Persistence: v.Persistence, OnePacket: v.OnePacket, HighWatermark: v.HighWatermark, LowWatermark: v.LowWatermark, LThreshold: v.LThreshold, UThreshold: v.UThreshold, } }
go
func (v *VserverEntry) Snapshot() *seesaw.VserverEntry { return &seesaw.VserverEntry{ Port: v.Port, Proto: v.Proto, Scheduler: v.Scheduler, Mode: v.Mode, Persistence: v.Persistence, OnePacket: v.OnePacket, HighWatermark: v.HighWatermark, LowWatermark: v.LowWatermark, LThreshold: v.LThreshold, UThreshold: v.UThreshold, } }
[ "func", "(", "v", "*", "VserverEntry", ")", "Snapshot", "(", ")", "*", "seesaw", ".", "VserverEntry", "{", "return", "&", "seesaw", ".", "VserverEntry", "{", "Port", ":", "v", ".", "Port", ",", "Proto", ":", "v", ".", "Proto", ",", "Scheduler", ":", "v", ".", "Scheduler", ",", "Mode", ":", "v", ".", "Mode", ",", "Persistence", ":", "v", ".", "Persistence", ",", "OnePacket", ":", "v", ".", "OnePacket", ",", "HighWatermark", ":", "v", ".", "HighWatermark", ",", "LowWatermark", ":", "v", ".", "LowWatermark", ",", "LThreshold", ":", "v", ".", "LThreshold", ",", "UThreshold", ":", "v", ".", "UThreshold", ",", "}", "\n", "}" ]
// Snapshot returns a snapshot for a VserverEntry.
[ "Snapshot", "returns", "a", "snapshot", "for", "a", "VserverEntry", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L227-L240
train
google/seesaw
engine/config/types.go
NewHealthcheck
func NewHealthcheck(m seesaw.HealthcheckMode, t seesaw.HealthcheckType, port uint16) *Healthcheck { return &Healthcheck{ Mode: m, Type: t, Port: port, } }
go
func NewHealthcheck(m seesaw.HealthcheckMode, t seesaw.HealthcheckType, port uint16) *Healthcheck { return &Healthcheck{ Mode: m, Type: t, Port: port, } }
[ "func", "NewHealthcheck", "(", "m", "seesaw", ".", "HealthcheckMode", ",", "t", "seesaw", ".", "HealthcheckType", ",", "port", "uint16", ")", "*", "Healthcheck", "{", "return", "&", "Healthcheck", "{", "Mode", ":", "m", ",", "Type", ":", "t", ",", "Port", ":", "port", ",", "}", "\n", "}" ]
// NewHealthcheck creates a new, initialised Healthcheck structure.
[ "NewHealthcheck", "creates", "a", "new", "initialised", "Healthcheck", "structure", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/config/types.go#L261-L267
train
google/seesaw
ncc/bgp.go
quaggaBGP
func quaggaBGP(asn uint32) (*quagga.BGP, error) { bgp := quagga.NewBGP("", asn) if err := bgp.Dial(); err != nil { return nil, err } if err := bgp.Enable(); err != nil { bgp.Close() return nil, err } return bgp, nil }
go
func quaggaBGP(asn uint32) (*quagga.BGP, error) { bgp := quagga.NewBGP("", asn) if err := bgp.Dial(); err != nil { return nil, err } if err := bgp.Enable(); err != nil { bgp.Close() return nil, err } return bgp, nil }
[ "func", "quaggaBGP", "(", "asn", "uint32", ")", "(", "*", "quagga", ".", "BGP", ",", "error", ")", "{", "bgp", ":=", "quagga", ".", "NewBGP", "(", "\"", "\"", ",", "asn", ")", "\n", "if", "err", ":=", "bgp", ".", "Dial", "(", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "if", "err", ":=", "bgp", ".", "Enable", "(", ")", ";", "err", "!=", "nil", "{", "bgp", ".", "Close", "(", ")", "\n", "return", "nil", ",", "err", "\n", "}", "\n", "return", "bgp", ",", "nil", "\n", "}" ]
// quaggaBGP establishes a connection with the Quagga BGP daemon.
[ "quaggaBGP", "establishes", "a", "connection", "with", "the", "Quagga", "BGP", "daemon", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/bgp.go#L36-L46
train
google/seesaw
ncc/bgp.go
BGPConfig
func (ncc *SeesawNCC) BGPConfig(unused int, cfg *ncctypes.BGPConfig) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() bgpCfg, err := bgp.Configuration() if err != nil { return err } if cfg != nil { cfg.Config = bgpCfg } return nil }
go
func (ncc *SeesawNCC) BGPConfig(unused int, cfg *ncctypes.BGPConfig) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() bgpCfg, err := bgp.Configuration() if err != nil { return err } if cfg != nil { cfg.Config = bgpCfg } return nil }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "BGPConfig", "(", "unused", "int", ",", "cfg", "*", "ncctypes", ".", "BGPConfig", ")", "error", "{", "bgp", ",", "err", ":=", "quaggaBGP", "(", "seesawASN", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "defer", "bgp", ".", "Close", "(", ")", "\n", "bgpCfg", ",", "err", ":=", "bgp", ".", "Configuration", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "if", "cfg", "!=", "nil", "{", "cfg", ".", "Config", "=", "bgpCfg", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// BGPConfig returns the current configuration for the Quagga BGP daemon.
[ "BGPConfig", "returns", "the", "current", "configuration", "for", "the", "Quagga", "BGP", "daemon", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/bgp.go#L49-L63
train
google/seesaw
ncc/bgp.go
BGPNeighbors
func (ncc *SeesawNCC) BGPNeighbors(unused int, neighbors *ncctypes.BGPNeighbors) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() bgpNeighbors, err := bgp.Neighbors() if err != nil { return err } if neighbors != nil { neighbors.Neighbors = bgpNeighbors } return nil }
go
func (ncc *SeesawNCC) BGPNeighbors(unused int, neighbors *ncctypes.BGPNeighbors) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() bgpNeighbors, err := bgp.Neighbors() if err != nil { return err } if neighbors != nil { neighbors.Neighbors = bgpNeighbors } return nil }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "BGPNeighbors", "(", "unused", "int", ",", "neighbors", "*", "ncctypes", ".", "BGPNeighbors", ")", "error", "{", "bgp", ",", "err", ":=", "quaggaBGP", "(", "seesawASN", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "defer", "bgp", ".", "Close", "(", ")", "\n", "bgpNeighbors", ",", "err", ":=", "bgp", ".", "Neighbors", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "if", "neighbors", "!=", "nil", "{", "neighbors", ".", "Neighbors", "=", "bgpNeighbors", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// BGPNeighbors returns the current BGP neighbors for the Quagga BGP daemon.
[ "BGPNeighbors", "returns", "the", "current", "BGP", "neighbors", "for", "the", "Quagga", "BGP", "daemon", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/bgp.go#L66-L80
train
google/seesaw
ncc/bgp.go
hostMask
func hostMask(ip net.IP) net.IPMask { var hl int if ip.To4() != nil { hl = net.IPv4len * 8 } else { hl = net.IPv6len * 8 } return net.CIDRMask(hl, hl) }
go
func hostMask(ip net.IP) net.IPMask { var hl int if ip.To4() != nil { hl = net.IPv4len * 8 } else { hl = net.IPv6len * 8 } return net.CIDRMask(hl, hl) }
[ "func", "hostMask", "(", "ip", "net", ".", "IP", ")", "net", ".", "IPMask", "{", "var", "hl", "int", "\n", "if", "ip", ".", "To4", "(", ")", "!=", "nil", "{", "hl", "=", "net", ".", "IPv4len", "*", "8", "\n", "}", "else", "{", "hl", "=", "net", ".", "IPv6len", "*", "8", "\n", "}", "\n", "return", "net", ".", "CIDRMask", "(", "hl", ",", "hl", ")", "\n", "}" ]
// hostMask returns an IP mask that corresponds with a host prefix.
[ "hostMask", "returns", "an", "IP", "mask", "that", "corresponds", "with", "a", "host", "prefix", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/bgp.go#L83-L91
train
google/seesaw
ncc/bgp.go
BGPWithdrawAll
func (ncc *SeesawNCC) BGPWithdrawAll(unused int, reply *int) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() bgpCfg, err := bgp.Configuration() if err != nil { return err } // Find the network statements within the router bgp section. bgpStr := fmt.Sprintf("router bgp %d", seesawASN) found := false for _, line := range bgpCfg { if line == bgpStr { found = true continue } if !found || line == "!" { continue } if !strings.HasPrefix(line, " ") { break } if strings.HasPrefix(line, " network ") { n := strings.Replace(line, " network ", "", 1) vip, ipNet, err := net.ParseCIDR(n) if err != nil { return err } err = bgp.Withdraw(&net.IPNet{IP: vip, Mask: ipNet.Mask}) if err != nil { return err } } } return nil }
go
func (ncc *SeesawNCC) BGPWithdrawAll(unused int, reply *int) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() bgpCfg, err := bgp.Configuration() if err != nil { return err } // Find the network statements within the router bgp section. bgpStr := fmt.Sprintf("router bgp %d", seesawASN) found := false for _, line := range bgpCfg { if line == bgpStr { found = true continue } if !found || line == "!" { continue } if !strings.HasPrefix(line, " ") { break } if strings.HasPrefix(line, " network ") { n := strings.Replace(line, " network ", "", 1) vip, ipNet, err := net.ParseCIDR(n) if err != nil { return err } err = bgp.Withdraw(&net.IPNet{IP: vip, Mask: ipNet.Mask}) if err != nil { return err } } } return nil }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "BGPWithdrawAll", "(", "unused", "int", ",", "reply", "*", "int", ")", "error", "{", "bgp", ",", "err", ":=", "quaggaBGP", "(", "seesawASN", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n\n", "defer", "bgp", ".", "Close", "(", ")", "\n", "bgpCfg", ",", "err", ":=", "bgp", ".", "Configuration", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n\n", "// Find the network statements within the router bgp section.", "bgpStr", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "seesawASN", ")", "\n", "found", ":=", "false", "\n", "for", "_", ",", "line", ":=", "range", "bgpCfg", "{", "if", "line", "==", "bgpStr", "{", "found", "=", "true", "\n", "continue", "\n", "}", "\n", "if", "!", "found", "||", "line", "==", "\"", "\"", "{", "continue", "\n", "}", "\n", "if", "!", "strings", ".", "HasPrefix", "(", "line", ",", "\"", "\"", ")", "{", "break", "\n", "}", "\n", "if", "strings", ".", "HasPrefix", "(", "line", ",", "\"", "\"", ")", "{", "n", ":=", "strings", ".", "Replace", "(", "line", ",", "\"", "\"", ",", "\"", "\"", ",", "1", ")", "\n", "vip", ",", "ipNet", ",", "err", ":=", "net", ".", "ParseCIDR", "(", "n", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "err", "=", "bgp", ".", "Withdraw", "(", "&", "net", ".", "IPNet", "{", "IP", ":", "vip", ",", "Mask", ":", "ipNet", ".", "Mask", "}", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "}", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// BGPWithdrawAll removes all network advertisements from the Quagga BGP daemon.
[ "BGPWithdrawAll", "removes", "all", "network", "advertisements", "from", "the", "Quagga", "BGP", "daemon", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/bgp.go#L94-L134
train
google/seesaw
ncc/bgp.go
BGPAdvertiseVIP
func (ncc *SeesawNCC) BGPAdvertiseVIP(vip net.IP, unused *int) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() return bgp.Advertise(&net.IPNet{IP: vip, Mask: hostMask(vip)}) }
go
func (ncc *SeesawNCC) BGPAdvertiseVIP(vip net.IP, unused *int) error { bgp, err := quaggaBGP(seesawASN) if err != nil { return err } defer bgp.Close() return bgp.Advertise(&net.IPNet{IP: vip, Mask: hostMask(vip)}) }
[ "func", "(", "ncc", "*", "SeesawNCC", ")", "BGPAdvertiseVIP", "(", "vip", "net", ".", "IP", ",", "unused", "*", "int", ")", "error", "{", "bgp", ",", "err", ":=", "quaggaBGP", "(", "seesawASN", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "defer", "bgp", ".", "Close", "(", ")", "\n", "return", "bgp", ".", "Advertise", "(", "&", "net", ".", "IPNet", "{", "IP", ":", "vip", ",", "Mask", ":", "hostMask", "(", "vip", ")", "}", ")", "\n", "}" ]
// BGPAdvertiseVIP requests the Quagga BGP daemon to advertise the given VIP.
[ "BGPAdvertiseVIP", "requests", "the", "Quagga", "BGP", "daemon", "to", "advertise", "the", "given", "VIP", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/ncc/bgp.go#L137-L144
train
google/seesaw
engine/ha.go
newHAManager
func newHAManager(engine *Engine, timeout time.Duration) *haManager { now := time.Now() return &haManager{ engine: engine, status: seesaw.HAStatus{ LastUpdate: now, Since: now, State: seesaw.HAUnknown, }, timeout: timeout, stateChan: make(chan seesaw.HAState, 1), statusChan: make(chan seesaw.HAStatus, 1), } }
go
func newHAManager(engine *Engine, timeout time.Duration) *haManager { now := time.Now() return &haManager{ engine: engine, status: seesaw.HAStatus{ LastUpdate: now, Since: now, State: seesaw.HAUnknown, }, timeout: timeout, stateChan: make(chan seesaw.HAState, 1), statusChan: make(chan seesaw.HAStatus, 1), } }
[ "func", "newHAManager", "(", "engine", "*", "Engine", ",", "timeout", "time", ".", "Duration", ")", "*", "haManager", "{", "now", ":=", "time", ".", "Now", "(", ")", "\n", "return", "&", "haManager", "{", "engine", ":", "engine", ",", "status", ":", "seesaw", ".", "HAStatus", "{", "LastUpdate", ":", "now", ",", "Since", ":", "now", ",", "State", ":", "seesaw", ".", "HAUnknown", ",", "}", ",", "timeout", ":", "timeout", ",", "stateChan", ":", "make", "(", "chan", "seesaw", ".", "HAState", ",", "1", ")", ",", "statusChan", ":", "make", "(", "chan", "seesaw", ".", "HAStatus", ",", "1", ")", ",", "}", "\n", "}" ]
// newHAManager creates a new haManager with the given HA state timeout.
[ "newHAManager", "creates", "a", "new", "haManager", "with", "the", "given", "HA", "state", "timeout", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L45-L58
train
google/seesaw
engine/ha.go
state
func (h *haManager) state() seesaw.HAState { h.statusLock.RLock() defer h.statusLock.RUnlock() return h.status.State }
go
func (h *haManager) state() seesaw.HAState { h.statusLock.RLock() defer h.statusLock.RUnlock() return h.status.State }
[ "func", "(", "h", "*", "haManager", ")", "state", "(", ")", "seesaw", ".", "HAState", "{", "h", ".", "statusLock", ".", "RLock", "(", ")", "\n", "defer", "h", ".", "statusLock", ".", "RUnlock", "(", ")", "\n", "return", "h", ".", "status", ".", "State", "\n", "}" ]
// state returns the current HA state known by the engine.
[ "state", "returns", "the", "current", "HA", "state", "known", "by", "the", "engine", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L61-L65
train
google/seesaw
engine/ha.go
enable
func (h *haManager) enable() { if h.state() == seesaw.HADisabled { h.setState(seesaw.HAUnknown) } }
go
func (h *haManager) enable() { if h.state() == seesaw.HADisabled { h.setState(seesaw.HAUnknown) } }
[ "func", "(", "h", "*", "haManager", ")", "enable", "(", ")", "{", "if", "h", ".", "state", "(", ")", "==", "seesaw", ".", "HADisabled", "{", "h", ".", "setState", "(", "seesaw", ".", "HAUnknown", ")", "\n", "}", "\n", "}" ]
// enable enables HA peering for the node on which the engine is running.
[ "enable", "enables", "HA", "peering", "for", "the", "node", "on", "which", "the", "engine", "is", "running", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L68-L72
train
google/seesaw
engine/ha.go
failover
func (h *haManager) failover() bool { h.failoverLock.Lock() pending := h.failoverPending h.failoverPending = false h.failoverLock.Unlock() return pending }
go
func (h *haManager) failover() bool { h.failoverLock.Lock() pending := h.failoverPending h.failoverPending = false h.failoverLock.Unlock() return pending }
[ "func", "(", "h", "*", "haManager", ")", "failover", "(", ")", "bool", "{", "h", ".", "failoverLock", ".", "Lock", "(", ")", "\n", "pending", ":=", "h", ".", "failoverPending", "\n", "h", ".", "failoverPending", "=", "false", "\n", "h", ".", "failoverLock", ".", "Unlock", "(", ")", "\n", "return", "pending", "\n", "}" ]
// failover returns true if the HA component should relinquish master state.
[ "failover", "returns", "true", "if", "the", "HA", "component", "should", "relinquish", "master", "state", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L80-L86
train
google/seesaw
engine/ha.go
requestFailover
func (h *haManager) requestFailover(peer bool) error { state := h.state() if state == seesaw.HAMaster { h.failoverLock.Lock() defer h.failoverLock.Unlock() if h.failoverPending { return fmt.Errorf("Failover request already pending") } h.failoverPending = true return nil } if peer { return fmt.Errorf("Node is not master (current state is %v)", state) } if err := h.engine.syncClient.failover(); err != nil { return err } return nil }
go
func (h *haManager) requestFailover(peer bool) error { state := h.state() if state == seesaw.HAMaster { h.failoverLock.Lock() defer h.failoverLock.Unlock() if h.failoverPending { return fmt.Errorf("Failover request already pending") } h.failoverPending = true return nil } if peer { return fmt.Errorf("Node is not master (current state is %v)", state) } if err := h.engine.syncClient.failover(); err != nil { return err } return nil }
[ "func", "(", "h", "*", "haManager", ")", "requestFailover", "(", "peer", "bool", ")", "error", "{", "state", ":=", "h", ".", "state", "(", ")", "\n", "if", "state", "==", "seesaw", ".", "HAMaster", "{", "h", ".", "failoverLock", ".", "Lock", "(", ")", "\n", "defer", "h", ".", "failoverLock", ".", "Unlock", "(", ")", "\n", "if", "h", ".", "failoverPending", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ")", "\n", "}", "\n", "h", ".", "failoverPending", "=", "true", "\n", "return", "nil", "\n", "}", "\n\n", "if", "peer", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "state", ")", "\n", "}", "\n\n", "if", "err", ":=", "h", ".", "engine", ".", "syncClient", ".", "failover", "(", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// requestFailover requests the node to initiate a failover.
[ "requestFailover", "requests", "the", "node", "to", "initiate", "a", "failover", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L89-L110
train
google/seesaw
engine/ha.go
setState
func (h *haManager) setState(s seesaw.HAState) { state := h.state() if state == seesaw.HADisabled && s != seesaw.HAUnknown { log.Warningf("Invalid HA state transition %v -> %v", state, s) return } if state != s { log.Infof("HA state transition %v -> %v starting", state, s) if s == seesaw.HAMaster { h.engine.becomeMaster() } else if state == seesaw.HAMaster || s == seesaw.HABackup { h.engine.becomeBackup() } log.Infof("HA state transition %v -> %v complete", state, s) } now := time.Now() h.statusLock.Lock() h.status.State = s h.status.Since = now h.status.LastUpdate = now h.statusLock.Unlock() }
go
func (h *haManager) setState(s seesaw.HAState) { state := h.state() if state == seesaw.HADisabled && s != seesaw.HAUnknown { log.Warningf("Invalid HA state transition %v -> %v", state, s) return } if state != s { log.Infof("HA state transition %v -> %v starting", state, s) if s == seesaw.HAMaster { h.engine.becomeMaster() } else if state == seesaw.HAMaster || s == seesaw.HABackup { h.engine.becomeBackup() } log.Infof("HA state transition %v -> %v complete", state, s) } now := time.Now() h.statusLock.Lock() h.status.State = s h.status.Since = now h.status.LastUpdate = now h.statusLock.Unlock() }
[ "func", "(", "h", "*", "haManager", ")", "setState", "(", "s", "seesaw", ".", "HAState", ")", "{", "state", ":=", "h", ".", "state", "(", ")", "\n\n", "if", "state", "==", "seesaw", ".", "HADisabled", "&&", "s", "!=", "seesaw", ".", "HAUnknown", "{", "log", ".", "Warningf", "(", "\"", "\"", ",", "state", ",", "s", ")", "\n", "return", "\n", "}", "\n\n", "if", "state", "!=", "s", "{", "log", ".", "Infof", "(", "\"", "\"", ",", "state", ",", "s", ")", "\n", "if", "s", "==", "seesaw", ".", "HAMaster", "{", "h", ".", "engine", ".", "becomeMaster", "(", ")", "\n", "}", "else", "if", "state", "==", "seesaw", ".", "HAMaster", "||", "s", "==", "seesaw", ".", "HABackup", "{", "h", ".", "engine", ".", "becomeBackup", "(", ")", "\n", "}", "\n", "log", ".", "Infof", "(", "\"", "\"", ",", "state", ",", "s", ")", "\n", "}", "\n\n", "now", ":=", "time", ".", "Now", "(", ")", "\n\n", "h", ".", "statusLock", ".", "Lock", "(", ")", "\n", "h", ".", "status", ".", "State", "=", "s", "\n", "h", ".", "status", ".", "Since", "=", "now", "\n", "h", ".", "status", ".", "LastUpdate", "=", "now", "\n", "h", ".", "statusLock", ".", "Unlock", "(", ")", "\n", "}" ]
// setState sets the HAState of the engine and dispatches events when the state // changes.
[ "setState", "sets", "the", "HAState", "of", "the", "engine", "and", "dispatches", "events", "when", "the", "state", "changes", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L114-L139
train
google/seesaw
engine/ha.go
setStatus
func (h *haManager) setStatus(s seesaw.HAStatus) { h.setState(s.State) h.statusLock.Lock() h.status.Since = s.Since h.status.Sent = s.Sent h.status.Received = s.Received h.status.Transitions = s.Transitions h.statusLock.Unlock() }
go
func (h *haManager) setStatus(s seesaw.HAStatus) { h.setState(s.State) h.statusLock.Lock() h.status.Since = s.Since h.status.Sent = s.Sent h.status.Received = s.Received h.status.Transitions = s.Transitions h.statusLock.Unlock() }
[ "func", "(", "h", "*", "haManager", ")", "setStatus", "(", "s", "seesaw", ".", "HAStatus", ")", "{", "h", ".", "setState", "(", "s", ".", "State", ")", "\n\n", "h", ".", "statusLock", ".", "Lock", "(", ")", "\n", "h", ".", "status", ".", "Since", "=", "s", ".", "Since", "\n", "h", ".", "status", ".", "Sent", "=", "s", ".", "Sent", "\n", "h", ".", "status", ".", "Received", "=", "s", ".", "Received", "\n", "h", ".", "status", ".", "Transitions", "=", "s", ".", "Transitions", "\n", "h", ".", "statusLock", ".", "Unlock", "(", ")", "\n", "}" ]
// setStatus updates the engine HAStatus.
[ "setStatus", "updates", "the", "engine", "HAStatus", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L142-L151
train
google/seesaw
engine/ha.go
timer
func (h *haManager) timer() <-chan time.Time { if s := h.state(); s == seesaw.HADisabled || s == seesaw.HAUnknown { return make(chan time.Time) } // TODO(angusc): Make this clock-jump safe. h.statusLock.RLock() deadline := h.status.LastUpdate.Add(h.timeout) h.statusLock.RUnlock() return time.After(deadline.Sub(time.Now())) }
go
func (h *haManager) timer() <-chan time.Time { if s := h.state(); s == seesaw.HADisabled || s == seesaw.HAUnknown { return make(chan time.Time) } // TODO(angusc): Make this clock-jump safe. h.statusLock.RLock() deadline := h.status.LastUpdate.Add(h.timeout) h.statusLock.RUnlock() return time.After(deadline.Sub(time.Now())) }
[ "func", "(", "h", "*", "haManager", ")", "timer", "(", ")", "<-", "chan", "time", ".", "Time", "{", "if", "s", ":=", "h", ".", "state", "(", ")", ";", "s", "==", "seesaw", ".", "HADisabled", "||", "s", "==", "seesaw", ".", "HAUnknown", "{", "return", "make", "(", "chan", "time", ".", "Time", ")", "\n", "}", "\n", "// TODO(angusc): Make this clock-jump safe.", "h", ".", "statusLock", ".", "RLock", "(", ")", "\n", "deadline", ":=", "h", ".", "status", ".", "LastUpdate", ".", "Add", "(", "h", ".", "timeout", ")", "\n", "h", ".", "statusLock", ".", "RUnlock", "(", ")", "\n", "return", "time", ".", "After", "(", "deadline", ".", "Sub", "(", "time", ".", "Now", "(", ")", ")", ")", "\n", "}" ]
// timer returns a channel that receives a Time object when the current HA state // expires.
[ "timer", "returns", "a", "channel", "that", "receives", "a", "Time", "object", "when", "the", "current", "HA", "state", "expires", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/engine/ha.go#L155-L164
train
google/seesaw
cli/core.go
Execute
func (cli *SeesawCLI) Execute(cmdline string) error { cmd, subcmds, _, args := FindCommand(cmdline) if cmd != nil { return cmd.function(cli, args) } if subcmds != nil { return errors.New("Incomplete command.") } return errors.New("Unknown command.") }
go
func (cli *SeesawCLI) Execute(cmdline string) error { cmd, subcmds, _, args := FindCommand(cmdline) if cmd != nil { return cmd.function(cli, args) } if subcmds != nil { return errors.New("Incomplete command.") } return errors.New("Unknown command.") }
[ "func", "(", "cli", "*", "SeesawCLI", ")", "Execute", "(", "cmdline", "string", ")", "error", "{", "cmd", ",", "subcmds", ",", "_", ",", "args", ":=", "FindCommand", "(", "cmdline", ")", "\n", "if", "cmd", "!=", "nil", "{", "return", "cmd", ".", "function", "(", "cli", ",", "args", ")", "\n", "}", "\n", "if", "subcmds", "!=", "nil", "{", "return", "errors", ".", "New", "(", "\"", "\"", ")", "\n", "}", "\n", "return", "errors", ".", "New", "(", "\"", "\"", ")", "\n", "}" ]
// Execute executes the given command line.
[ "Execute", "executes", "the", "given", "command", "line", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/cli/core.go#L40-L49
train
google/seesaw
cli/core.go
FindCommand
func FindCommand(cmdline string) (*Command, *[]Command, []*Command, []string) { var chain []*Command var matches []Command var next *Command cmds := &commands // Tokenise string. cmdstr := strings.Fields(cmdline) if len(cmdstr) == 0 { return nil, cmds, chain, nil } for idx, subcmd := range cmdstr { matches = make([]Command, 0) next = nil for i := range *cmds { cmd := (*cmds)[i] if strings.HasPrefix(cmd.Command, subcmd) { matches = append(matches, cmd) next = &cmd } } if len(matches) == 0 { // Sub command not found. return nil, nil, chain, cmdstr[idx:] } else if len(matches) > 1 { // Ambiguious command. return nil, &matches, chain, cmdstr[idx:] } chain = append(chain, next) if next.function != nil { // We've reached a function. return next, nil, chain, cmdstr[idx+1:] } else { cmds = next.Subcommands } } if next != nil { return nil, next.Subcommands, chain, nil } return nil, nil, chain, nil }
go
func FindCommand(cmdline string) (*Command, *[]Command, []*Command, []string) { var chain []*Command var matches []Command var next *Command cmds := &commands // Tokenise string. cmdstr := strings.Fields(cmdline) if len(cmdstr) == 0 { return nil, cmds, chain, nil } for idx, subcmd := range cmdstr { matches = make([]Command, 0) next = nil for i := range *cmds { cmd := (*cmds)[i] if strings.HasPrefix(cmd.Command, subcmd) { matches = append(matches, cmd) next = &cmd } } if len(matches) == 0 { // Sub command not found. return nil, nil, chain, cmdstr[idx:] } else if len(matches) > 1 { // Ambiguious command. return nil, &matches, chain, cmdstr[idx:] } chain = append(chain, next) if next.function != nil { // We've reached a function. return next, nil, chain, cmdstr[idx+1:] } else { cmds = next.Subcommands } } if next != nil { return nil, next.Subcommands, chain, nil } return nil, nil, chain, nil }
[ "func", "FindCommand", "(", "cmdline", "string", ")", "(", "*", "Command", ",", "*", "[", "]", "Command", ",", "[", "]", "*", "Command", ",", "[", "]", "string", ")", "{", "var", "chain", "[", "]", "*", "Command", "\n", "var", "matches", "[", "]", "Command", "\n", "var", "next", "*", "Command", "\n", "cmds", ":=", "&", "commands", "\n\n", "// Tokenise string.", "cmdstr", ":=", "strings", ".", "Fields", "(", "cmdline", ")", "\n", "if", "len", "(", "cmdstr", ")", "==", "0", "{", "return", "nil", ",", "cmds", ",", "chain", ",", "nil", "\n", "}", "\n\n", "for", "idx", ",", "subcmd", ":=", "range", "cmdstr", "{", "matches", "=", "make", "(", "[", "]", "Command", ",", "0", ")", "\n", "next", "=", "nil", "\n", "for", "i", ":=", "range", "*", "cmds", "{", "cmd", ":=", "(", "*", "cmds", ")", "[", "i", "]", "\n", "if", "strings", ".", "HasPrefix", "(", "cmd", ".", "Command", ",", "subcmd", ")", "{", "matches", "=", "append", "(", "matches", ",", "cmd", ")", "\n", "next", "=", "&", "cmd", "\n", "}", "\n", "}", "\n", "if", "len", "(", "matches", ")", "==", "0", "{", "// Sub command not found.", "return", "nil", ",", "nil", ",", "chain", ",", "cmdstr", "[", "idx", ":", "]", "\n", "}", "else", "if", "len", "(", "matches", ")", ">", "1", "{", "// Ambiguious command.", "return", "nil", ",", "&", "matches", ",", "chain", ",", "cmdstr", "[", "idx", ":", "]", "\n", "}", "\n", "chain", "=", "append", "(", "chain", ",", "next", ")", "\n", "if", "next", ".", "function", "!=", "nil", "{", "// We've reached a function.", "return", "next", ",", "nil", ",", "chain", ",", "cmdstr", "[", "idx", "+", "1", ":", "]", "\n", "}", "else", "{", "cmds", "=", "next", ".", "Subcommands", "\n", "}", "\n", "}", "\n", "if", "next", "!=", "nil", "{", "return", "nil", ",", "next", ".", "Subcommands", ",", "chain", ",", "nil", "\n", "}", "\n\n", "return", "nil", ",", "nil", ",", "chain", ",", "nil", "\n", "}" ]
// FindCommand tokenises a command line and attempts to locate the // corresponding Command. If a matching command is found it is returned, // along with the remaining arguments. If the command has sub-commands then // the list of sub-commands is returned instead. A chain of matched commands // is also returned, along with the slice of remaining arguments.
[ "FindCommand", "tokenises", "a", "command", "line", "and", "attempts", "to", "locate", "the", "corresponding", "Command", ".", "If", "a", "matching", "command", "is", "found", "it", "is", "returned", "along", "with", "the", "remaining", "arguments", ".", "If", "the", "command", "has", "sub", "-", "commands", "then", "the", "list", "of", "sub", "-", "commands", "is", "returned", "instead", ".", "A", "chain", "of", "matched", "commands", "is", "also", "returned", "along", "with", "the", "slice", "of", "remaining", "arguments", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/cli/core.go#L113-L155
train
google/seesaw
quagga/bgp.go
String
func (s BGPState) String() string { if name, ok := stateNames[s]; ok { return name } return "Unknown" }
go
func (s BGPState) String() string { if name, ok := stateNames[s]; ok { return name } return "Unknown" }
[ "func", "(", "s", "BGPState", ")", "String", "(", ")", "string", "{", "if", "name", ",", "ok", ":=", "stateNames", "[", "s", "]", ";", "ok", "{", "return", "name", "\n", "}", "\n", "return", "\"", "\"", "\n", "}" ]
// String returns a string representation of the given BGP state.
[ "String", "returns", "a", "string", "representation", "of", "the", "given", "BGP", "state", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L60-L65
train
google/seesaw
quagga/bgp.go
BGPStateByName
func BGPStateByName(name string) BGPState { name = strings.ToLower(name) for s := range stateNames { if strings.ToLower(stateNames[s]) == name { return s } } return BGPStateUnknown }
go
func BGPStateByName(name string) BGPState { name = strings.ToLower(name) for s := range stateNames { if strings.ToLower(stateNames[s]) == name { return s } } return BGPStateUnknown }
[ "func", "BGPStateByName", "(", "name", "string", ")", "BGPState", "{", "name", "=", "strings", ".", "ToLower", "(", "name", ")", "\n", "for", "s", ":=", "range", "stateNames", "{", "if", "strings", ".", "ToLower", "(", "stateNames", "[", "s", "]", ")", "==", "name", "{", "return", "s", "\n", "}", "\n", "}", "\n", "return", "BGPStateUnknown", "\n", "}" ]
// BGPStateByName returns the BGP state that corresponds to the given name.
[ "BGPStateByName", "returns", "the", "BGP", "state", "that", "corresponds", "to", "the", "given", "name", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L68-L76
train
google/seesaw
quagga/bgp.go
NewBGP
func NewBGP(socket string, asn uint32) *BGP { if socket == "" { socket = BGPSocketPath } return &BGP{ vty: NewVTY(socket), asn: asn, } }
go
func NewBGP(socket string, asn uint32) *BGP { if socket == "" { socket = BGPSocketPath } return &BGP{ vty: NewVTY(socket), asn: asn, } }
[ "func", "NewBGP", "(", "socket", "string", ",", "asn", "uint32", ")", "*", "BGP", "{", "if", "socket", "==", "\"", "\"", "{", "socket", "=", "BGPSocketPath", "\n", "}", "\n", "return", "&", "BGP", "{", "vty", ":", "NewVTY", "(", "socket", ")", ",", "asn", ":", "asn", ",", "}", "\n", "}" ]
// NewBGP returns an initialised BGP structure.
[ "NewBGP", "returns", "an", "initialised", "BGP", "structure", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L156-L164
train
google/seesaw
quagga/bgp.go
Enable
func (b *BGP) Enable() error { _, err := b.vty.Command("enable") return err }
go
func (b *BGP) Enable() error { _, err := b.vty.Command("enable") return err }
[ "func", "(", "b", "*", "BGP", ")", "Enable", "(", ")", "error", "{", "_", ",", "err", ":=", "b", ".", "vty", ".", "Command", "(", "\"", "\"", ")", "\n", "return", "err", "\n", "}" ]
// Enable issues an "enable" command to the BGP daemon.
[ "Enable", "issues", "an", "enable", "command", "to", "the", "BGP", "daemon", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L177-L180
train
google/seesaw
quagga/bgp.go
Configuration
func (b *BGP) Configuration() ([]string, error) { cfg, err := b.vty.Command("write terminal") if err != nil { return nil, err } return strings.Split(cfg, "\n"), nil }
go
func (b *BGP) Configuration() ([]string, error) { cfg, err := b.vty.Command("write terminal") if err != nil { return nil, err } return strings.Split(cfg, "\n"), nil }
[ "func", "(", "b", "*", "BGP", ")", "Configuration", "(", ")", "(", "[", "]", "string", ",", "error", ")", "{", "cfg", ",", "err", ":=", "b", ".", "vty", ".", "Command", "(", "\"", "\"", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "return", "strings", ".", "Split", "(", "cfg", ",", "\"", "\\n", "\"", ")", ",", "nil", "\n", "}" ]
// Configuration returns the current running configuration from the BGP daemon, // as a slice of strings.
[ "Configuration", "returns", "the", "current", "running", "configuration", "from", "the", "BGP", "daemon", "as", "a", "slice", "of", "strings", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L184-L190
train
google/seesaw
quagga/bgp.go
Neighbors
func (b *BGP) Neighbors() ([]*Neighbor, error) { ni, err := b.vty.Command("show ip bgp neighbors") if err != nil { return nil, err } // TODO(jsing): Include details for advertised/received routes. return parseNeighbors(ni), nil }
go
func (b *BGP) Neighbors() ([]*Neighbor, error) { ni, err := b.vty.Command("show ip bgp neighbors") if err != nil { return nil, err } // TODO(jsing): Include details for advertised/received routes. return parseNeighbors(ni), nil }
[ "func", "(", "b", "*", "BGP", ")", "Neighbors", "(", ")", "(", "[", "]", "*", "Neighbor", ",", "error", ")", "{", "ni", ",", "err", ":=", "b", ".", "vty", ".", "Command", "(", "\"", "\"", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "// TODO(jsing): Include details for advertised/received routes.", "return", "parseNeighbors", "(", "ni", ")", ",", "nil", "\n", "}" ]
// Neighbors returns a list of BGP neighbors that we are currently peering with.
[ "Neighbors", "returns", "a", "list", "of", "BGP", "neighbors", "that", "we", "are", "currently", "peering", "with", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L193-L200
train
google/seesaw
quagga/bgp.go
network
func (b *BGP) network(n *net.IPNet, advertise bool) error { var prefix string if !advertise { prefix = "no " } family := "ipv4 unicast" if n.IP.To4() == nil { family = "ipv6" } prefixLen, _ := n.Mask.Size() cmds := []string{ "configure terminal", fmt.Sprintf("router bgp %d", b.asn), fmt.Sprintf("address-family %s", family), fmt.Sprintf("%snetwork %s/%d", prefix, n.IP, prefixLen), "end", } bgpConfigLock.Lock() defer bgpConfigLock.Unlock() return b.vty.Commands(cmds) }
go
func (b *BGP) network(n *net.IPNet, advertise bool) error { var prefix string if !advertise { prefix = "no " } family := "ipv4 unicast" if n.IP.To4() == nil { family = "ipv6" } prefixLen, _ := n.Mask.Size() cmds := []string{ "configure terminal", fmt.Sprintf("router bgp %d", b.asn), fmt.Sprintf("address-family %s", family), fmt.Sprintf("%snetwork %s/%d", prefix, n.IP, prefixLen), "end", } bgpConfigLock.Lock() defer bgpConfigLock.Unlock() return b.vty.Commands(cmds) }
[ "func", "(", "b", "*", "BGP", ")", "network", "(", "n", "*", "net", ".", "IPNet", ",", "advertise", "bool", ")", "error", "{", "var", "prefix", "string", "\n", "if", "!", "advertise", "{", "prefix", "=", "\"", "\"", "\n", "}", "\n", "family", ":=", "\"", "\"", "\n", "if", "n", ".", "IP", ".", "To4", "(", ")", "==", "nil", "{", "family", "=", "\"", "\"", "\n", "}", "\n", "prefixLen", ",", "_", ":=", "n", ".", "Mask", ".", "Size", "(", ")", "\n", "cmds", ":=", "[", "]", "string", "{", "\"", "\"", ",", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "b", ".", "asn", ")", ",", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "family", ")", ",", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "prefix", ",", "n", ".", "IP", ",", "prefixLen", ")", ",", "\"", "\"", ",", "}", "\n", "bgpConfigLock", ".", "Lock", "(", ")", "\n", "defer", "bgpConfigLock", ".", "Unlock", "(", ")", "\n", "return", "b", ".", "vty", ".", "Commands", "(", "cmds", ")", "\n", "}" ]
// network adds or removes a network statement from the BGP configuration.
[ "network", "adds", "or", "removes", "a", "network", "statement", "from", "the", "BGP", "configuration", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L203-L223
train
google/seesaw
quagga/bgp.go
Advertise
func (b *BGP) Advertise(n *net.IPNet) error { return b.network(n, true) }
go
func (b *BGP) Advertise(n *net.IPNet) error { return b.network(n, true) }
[ "func", "(", "b", "*", "BGP", ")", "Advertise", "(", "n", "*", "net", ".", "IPNet", ")", "error", "{", "return", "b", ".", "network", "(", "n", ",", "true", ")", "\n", "}" ]
// Advertise requests the BGP daemon to advertise the specified network.
[ "Advertise", "requests", "the", "BGP", "daemon", "to", "advertise", "the", "specified", "network", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L226-L228
train
google/seesaw
quagga/bgp.go
Withdraw
func (b *BGP) Withdraw(n *net.IPNet) error { return b.network(n, false) }
go
func (b *BGP) Withdraw(n *net.IPNet) error { return b.network(n, false) }
[ "func", "(", "b", "*", "BGP", ")", "Withdraw", "(", "n", "*", "net", ".", "IPNet", ")", "error", "{", "return", "b", ".", "network", "(", "n", ",", "false", ")", "\n", "}" ]
// Withdraw requests the BGP daemon to withdraw advertisements for the // specified network.
[ "Withdraw", "requests", "the", "BGP", "daemon", "to", "withdraw", "advertisements", "for", "the", "specified", "network", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L232-L234
train
google/seesaw
quagga/bgp.go
parseNeighbors
func parseNeighbors(sn string) []*Neighbor { neighbors := make([]*Neighbor, 0) var neighbor *Neighbor var msgStats bool for _, s := range strings.Split(sn, "\n") { if nm := neighborRE.FindStringSubmatch(s); nm != nil { asn, _ := strconv.ParseUint(nm[2], 10, 32) neighbor = &Neighbor{ IP: net.ParseIP(nm[1]), ASN: uint32(asn), } neighbors = append(neighbors, neighbor) } if neighbor == nil { continue } if msgStats { if nm := neighborStatsRE.FindStringSubmatch(s); nm != nil { var ms *MessageStat switch nm[1] { case "Opens": ms = &neighbor.Opens case "Notifications": ms = &neighbor.Notifications case "Updates": ms = &neighbor.Updates case "Keepalives": ms = &neighbor.Keepalives case "Route Refresh": ms = &neighbor.RouteRefresh case "Capability": ms = &neighbor.Capability case "Total": ms = &neighbor.Total msgStats = false } if ms != nil { sent, _ := strconv.ParseUint(nm[2], 10, 0) ms.Sent = sent rcvd, _ := strconv.ParseUint(nm[3], 10, 0) ms.Rcvd = rcvd } } } if nm := neighborDescRE.FindStringSubmatch(s); nm != nil { neighbor.Description = nm[1] } else if nm := neighborStateRE.FindStringSubmatch(s); nm != nil { neighbor.BGPState = BGPStateByName(nm[1]) neighbor.Uptime = ParseUptime(nm[3]) } else if nm := neighborVersionRE.FindStringSubmatch(s); nm != nil { neighbor.RouterID = net.ParseIP(nm[2]) } else if s == " Message statistics:" { msgStats = true } } return neighbors }
go
func parseNeighbors(sn string) []*Neighbor { neighbors := make([]*Neighbor, 0) var neighbor *Neighbor var msgStats bool for _, s := range strings.Split(sn, "\n") { if nm := neighborRE.FindStringSubmatch(s); nm != nil { asn, _ := strconv.ParseUint(nm[2], 10, 32) neighbor = &Neighbor{ IP: net.ParseIP(nm[1]), ASN: uint32(asn), } neighbors = append(neighbors, neighbor) } if neighbor == nil { continue } if msgStats { if nm := neighborStatsRE.FindStringSubmatch(s); nm != nil { var ms *MessageStat switch nm[1] { case "Opens": ms = &neighbor.Opens case "Notifications": ms = &neighbor.Notifications case "Updates": ms = &neighbor.Updates case "Keepalives": ms = &neighbor.Keepalives case "Route Refresh": ms = &neighbor.RouteRefresh case "Capability": ms = &neighbor.Capability case "Total": ms = &neighbor.Total msgStats = false } if ms != nil { sent, _ := strconv.ParseUint(nm[2], 10, 0) ms.Sent = sent rcvd, _ := strconv.ParseUint(nm[3], 10, 0) ms.Rcvd = rcvd } } } if nm := neighborDescRE.FindStringSubmatch(s); nm != nil { neighbor.Description = nm[1] } else if nm := neighborStateRE.FindStringSubmatch(s); nm != nil { neighbor.BGPState = BGPStateByName(nm[1]) neighbor.Uptime = ParseUptime(nm[3]) } else if nm := neighborVersionRE.FindStringSubmatch(s); nm != nil { neighbor.RouterID = net.ParseIP(nm[2]) } else if s == " Message statistics:" { msgStats = true } } return neighbors }
[ "func", "parseNeighbors", "(", "sn", "string", ")", "[", "]", "*", "Neighbor", "{", "neighbors", ":=", "make", "(", "[", "]", "*", "Neighbor", ",", "0", ")", "\n", "var", "neighbor", "*", "Neighbor", "\n", "var", "msgStats", "bool", "\n", "for", "_", ",", "s", ":=", "range", "strings", ".", "Split", "(", "sn", ",", "\"", "\\n", "\"", ")", "{", "if", "nm", ":=", "neighborRE", ".", "FindStringSubmatch", "(", "s", ")", ";", "nm", "!=", "nil", "{", "asn", ",", "_", ":=", "strconv", ".", "ParseUint", "(", "nm", "[", "2", "]", ",", "10", ",", "32", ")", "\n", "neighbor", "=", "&", "Neighbor", "{", "IP", ":", "net", ".", "ParseIP", "(", "nm", "[", "1", "]", ")", ",", "ASN", ":", "uint32", "(", "asn", ")", ",", "}", "\n", "neighbors", "=", "append", "(", "neighbors", ",", "neighbor", ")", "\n", "}", "\n", "if", "neighbor", "==", "nil", "{", "continue", "\n", "}", "\n\n", "if", "msgStats", "{", "if", "nm", ":=", "neighborStatsRE", ".", "FindStringSubmatch", "(", "s", ")", ";", "nm", "!=", "nil", "{", "var", "ms", "*", "MessageStat", "\n", "switch", "nm", "[", "1", "]", "{", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "Opens", "\n", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "Notifications", "\n", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "Updates", "\n", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "Keepalives", "\n", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "RouteRefresh", "\n", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "Capability", "\n", "case", "\"", "\"", ":", "ms", "=", "&", "neighbor", ".", "Total", "\n", "msgStats", "=", "false", "\n", "}", "\n", "if", "ms", "!=", "nil", "{", "sent", ",", "_", ":=", "strconv", ".", "ParseUint", "(", "nm", "[", "2", "]", ",", "10", ",", "0", ")", "\n", "ms", ".", "Sent", "=", "sent", "\n", "rcvd", ",", "_", ":=", "strconv", ".", "ParseUint", "(", "nm", "[", "3", "]", ",", "10", ",", "0", ")", "\n", "ms", ".", "Rcvd", "=", "rcvd", "\n", "}", "\n", "}", "\n", "}", "\n\n", "if", "nm", ":=", "neighborDescRE", ".", "FindStringSubmatch", "(", "s", ")", ";", "nm", "!=", "nil", "{", "neighbor", ".", "Description", "=", "nm", "[", "1", "]", "\n", "}", "else", "if", "nm", ":=", "neighborStateRE", ".", "FindStringSubmatch", "(", "s", ")", ";", "nm", "!=", "nil", "{", "neighbor", ".", "BGPState", "=", "BGPStateByName", "(", "nm", "[", "1", "]", ")", "\n", "neighbor", ".", "Uptime", "=", "ParseUptime", "(", "nm", "[", "3", "]", ")", "\n", "}", "else", "if", "nm", ":=", "neighborVersionRE", ".", "FindStringSubmatch", "(", "s", ")", ";", "nm", "!=", "nil", "{", "neighbor", ".", "RouterID", "=", "net", ".", "ParseIP", "(", "nm", "[", "2", "]", ")", "\n", "}", "else", "if", "s", "==", "\"", "\"", "{", "msgStats", "=", "true", "\n", "}", "\n", "}", "\n", "return", "neighbors", "\n", "}" ]
// parseNeighbors parses the "show ip bgp neighbors" output from the Quagga // BGP daemon and returns a slice of Neighbor structs.
[ "parseNeighbors", "parses", "the", "show", "ip", "bgp", "neighbors", "output", "from", "the", "Quagga", "BGP", "daemon", "and", "returns", "a", "slice", "of", "Neighbor", "structs", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/quagga/bgp.go#L246-L304
train
google/seesaw
healthcheck/udp.go
NewUDPChecker
func NewUDPChecker(ip net.IP, port int) *UDPChecker { return &UDPChecker{ Target: Target{ IP: ip, Port: port, Proto: seesaw.IPProtoUDP, }, } }
go
func NewUDPChecker(ip net.IP, port int) *UDPChecker { return &UDPChecker{ Target: Target{ IP: ip, Port: port, Proto: seesaw.IPProtoUDP, }, } }
[ "func", "NewUDPChecker", "(", "ip", "net", ".", "IP", ",", "port", "int", ")", "*", "UDPChecker", "{", "return", "&", "UDPChecker", "{", "Target", ":", "Target", "{", "IP", ":", "ip", ",", "Port", ":", "port", ",", "Proto", ":", "seesaw", ".", "IPProtoUDP", ",", "}", ",", "}", "\n", "}" ]
// NewUDPChecker returns an initialised UDPChecker.
[ "NewUDPChecker", "returns", "an", "initialised", "UDPChecker", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/udp.go#L41-L49
train
google/seesaw
healthcheck/udp.go
Check
func (hc *UDPChecker) Check(timeout time.Duration) *Result { msg := fmt.Sprintf("UDP check to %s", hc.addr()) start := time.Now() if timeout == time.Duration(0) { timeout = defaultUDPTimeout } deadline := start.Add(timeout) conn, err := dialUDP(hc.network(), hc.addr(), timeout, hc.Mark) if err != nil { msg = fmt.Sprintf("%s; failed to create socket", msg) return complete(start, msg, false, err) } defer conn.Close() err = conn.SetDeadline(deadline) if err != nil { msg = fmt.Sprintf("%s; failed to set deadline", msg) return complete(start, msg, false, err) } if _, err = conn.Write([]byte(hc.Send)); err != nil { msg = fmt.Sprintf("%s; failed to send request", msg) return complete(start, msg, false, err) } buf := make([]byte, len(hc.Receive)) n, _, err := conn.ReadFrom(buf) if err != nil { msg = fmt.Sprintf("%s; failed to read response", msg) return complete(start, msg, false, err) } got := string(buf[0:n]) if got != hc.Receive { msg = fmt.Sprintf("%s; unexpected response - %q", msg, got) return complete(start, msg, false, err) } return complete(start, msg, true, err) }
go
func (hc *UDPChecker) Check(timeout time.Duration) *Result { msg := fmt.Sprintf("UDP check to %s", hc.addr()) start := time.Now() if timeout == time.Duration(0) { timeout = defaultUDPTimeout } deadline := start.Add(timeout) conn, err := dialUDP(hc.network(), hc.addr(), timeout, hc.Mark) if err != nil { msg = fmt.Sprintf("%s; failed to create socket", msg) return complete(start, msg, false, err) } defer conn.Close() err = conn.SetDeadline(deadline) if err != nil { msg = fmt.Sprintf("%s; failed to set deadline", msg) return complete(start, msg, false, err) } if _, err = conn.Write([]byte(hc.Send)); err != nil { msg = fmt.Sprintf("%s; failed to send request", msg) return complete(start, msg, false, err) } buf := make([]byte, len(hc.Receive)) n, _, err := conn.ReadFrom(buf) if err != nil { msg = fmt.Sprintf("%s; failed to read response", msg) return complete(start, msg, false, err) } got := string(buf[0:n]) if got != hc.Receive { msg = fmt.Sprintf("%s; unexpected response - %q", msg, got) return complete(start, msg, false, err) } return complete(start, msg, true, err) }
[ "func", "(", "hc", "*", "UDPChecker", ")", "Check", "(", "timeout", "time", ".", "Duration", ")", "*", "Result", "{", "msg", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "hc", ".", "addr", "(", ")", ")", "\n", "start", ":=", "time", ".", "Now", "(", ")", "\n", "if", "timeout", "==", "time", ".", "Duration", "(", "0", ")", "{", "timeout", "=", "defaultUDPTimeout", "\n", "}", "\n", "deadline", ":=", "start", ".", "Add", "(", "timeout", ")", "\n\n", "conn", ",", "err", ":=", "dialUDP", "(", "hc", ".", "network", "(", ")", ",", "hc", ".", "addr", "(", ")", ",", "timeout", ",", "hc", ".", "Mark", ")", "\n", "if", "err", "!=", "nil", "{", "msg", "=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "msg", ")", "\n", "return", "complete", "(", "start", ",", "msg", ",", "false", ",", "err", ")", "\n", "}", "\n", "defer", "conn", ".", "Close", "(", ")", "\n\n", "err", "=", "conn", ".", "SetDeadline", "(", "deadline", ")", "\n", "if", "err", "!=", "nil", "{", "msg", "=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "msg", ")", "\n", "return", "complete", "(", "start", ",", "msg", ",", "false", ",", "err", ")", "\n", "}", "\n\n", "if", "_", ",", "err", "=", "conn", ".", "Write", "(", "[", "]", "byte", "(", "hc", ".", "Send", ")", ")", ";", "err", "!=", "nil", "{", "msg", "=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "msg", ")", "\n", "return", "complete", "(", "start", ",", "msg", ",", "false", ",", "err", ")", "\n", "}", "\n\n", "buf", ":=", "make", "(", "[", "]", "byte", ",", "len", "(", "hc", ".", "Receive", ")", ")", "\n", "n", ",", "_", ",", "err", ":=", "conn", ".", "ReadFrom", "(", "buf", ")", "\n", "if", "err", "!=", "nil", "{", "msg", "=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "msg", ")", "\n", "return", "complete", "(", "start", ",", "msg", ",", "false", ",", "err", ")", "\n", "}", "\n\n", "got", ":=", "string", "(", "buf", "[", "0", ":", "n", "]", ")", "\n", "if", "got", "!=", "hc", ".", "Receive", "{", "msg", "=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "msg", ",", "got", ")", "\n", "return", "complete", "(", "start", ",", "msg", ",", "false", ",", "err", ")", "\n", "}", "\n", "return", "complete", "(", "start", ",", "msg", ",", "true", ",", "err", ")", "\n", "}" ]
// Check executes a UDP healthcheck.
[ "Check", "executes", "a", "UDP", "healthcheck", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/healthcheck/udp.go#L57-L96
train
google/seesaw
binaries/seesaw_engine/main.go
cfgOpt
func cfgOpt(cfg *conf.ConfigFile, section, option string) string { if !cfg.HasOption(section, option) { return "" } s, err := cfg.GetString(section, option) if err != nil { log.Exitf("Failed to get %s for %s: %v", option, section, err) } return s }
go
func cfgOpt(cfg *conf.ConfigFile, section, option string) string { if !cfg.HasOption(section, option) { return "" } s, err := cfg.GetString(section, option) if err != nil { log.Exitf("Failed to get %s for %s: %v", option, section, err) } return s }
[ "func", "cfgOpt", "(", "cfg", "*", "conf", ".", "ConfigFile", ",", "section", ",", "option", "string", ")", "string", "{", "if", "!", "cfg", ".", "HasOption", "(", "section", ",", "option", ")", "{", "return", "\"", "\"", "\n", "}", "\n", "s", ",", "err", ":=", "cfg", ".", "GetString", "(", "section", ",", "option", ")", "\n", "if", "err", "!=", "nil", "{", "log", ".", "Exitf", "(", "\"", "\"", ",", "option", ",", "section", ",", "err", ")", "\n", "}", "\n", "return", "s", "\n", "}" ]
// cfgOpt returns the configuration option from the specified section. If the // option does not exist an empty string is returned.
[ "cfgOpt", "returns", "the", "configuration", "option", "from", "the", "specified", "section", ".", "If", "the", "option", "does", "not", "exist", "an", "empty", "string", "is", "returned", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/binaries/seesaw_engine/main.go#L49-L58
train
google/seesaw
binaries/seesaw_engine/main.go
cfgIP
func cfgIP(cfg *conf.ConfigFile, section, option string) (net.IP, error) { ipStr := cfgOpt(cfg, section, option) if ipStr == "" { return nil, nil } ip := net.ParseIP(ipStr) if ip == nil { return nil, fmt.Errorf("%s: %q is not a valid IP address", option, ipStr) } return ip, nil }
go
func cfgIP(cfg *conf.ConfigFile, section, option string) (net.IP, error) { ipStr := cfgOpt(cfg, section, option) if ipStr == "" { return nil, nil } ip := net.ParseIP(ipStr) if ip == nil { return nil, fmt.Errorf("%s: %q is not a valid IP address", option, ipStr) } return ip, nil }
[ "func", "cfgIP", "(", "cfg", "*", "conf", ".", "ConfigFile", ",", "section", ",", "option", "string", ")", "(", "net", ".", "IP", ",", "error", ")", "{", "ipStr", ":=", "cfgOpt", "(", "cfg", ",", "section", ",", "option", ")", "\n", "if", "ipStr", "==", "\"", "\"", "{", "return", "nil", ",", "nil", "\n", "}", "\n", "ip", ":=", "net", ".", "ParseIP", "(", "ipStr", ")", "\n", "if", "ip", "==", "nil", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "option", ",", "ipStr", ")", "\n", "}", "\n", "return", "ip", ",", "nil", "\n", "}" ]
// cfgIP returns configuration option from the specified section, as an IP // address. If the option does not exist or is blank, a nil IP is returned.
[ "cfgIP", "returns", "configuration", "option", "from", "the", "specified", "section", "as", "an", "IP", "address", ".", "If", "the", "option", "does", "not", "exist", "or", "is", "blank", "a", "nil", "IP", "is", "returned", "." ]
34716af0775ecb1fad239a726390d63d6b0742dd
https://github.com/google/seesaw/blob/34716af0775ecb1fad239a726390d63d6b0742dd/binaries/seesaw_engine/main.go#L62-L72
train
nats-io/nats-streaming-server
server/raft_log.go
Close
func (r *raftLog) Close() error { r.Lock() if r.closed { r.Unlock() return nil } r.closed = true err := r.conn.Close() r.Unlock() return err }
go
func (r *raftLog) Close() error { r.Lock() if r.closed { r.Unlock() return nil } r.closed = true err := r.conn.Close() r.Unlock() return err }
[ "func", "(", "r", "*", "raftLog", ")", "Close", "(", ")", "error", "{", "r", ".", "Lock", "(", ")", "\n", "if", "r", ".", "closed", "{", "r", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "}", "\n", "r", ".", "closed", "=", "true", "\n", "err", ":=", "r", ".", "conn", ".", "Close", "(", ")", "\n", "r", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}" ]
// Close implements the LogStore interface
[ "Close", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L159-L169
train
nats-io/nats-streaming-server
server/raft_log.go
FirstIndex
func (r *raftLog) FirstIndex() (uint64, error) { r.RLock() idx, err := r.getIndex(true) r.RUnlock() return idx, err }
go
func (r *raftLog) FirstIndex() (uint64, error) { r.RLock() idx, err := r.getIndex(true) r.RUnlock() return idx, err }
[ "func", "(", "r", "*", "raftLog", ")", "FirstIndex", "(", ")", "(", "uint64", ",", "error", ")", "{", "r", ".", "RLock", "(", ")", "\n", "idx", ",", "err", ":=", "r", ".", "getIndex", "(", "true", ")", "\n", "r", ".", "RUnlock", "(", ")", "\n", "return", "idx", ",", "err", "\n", "}" ]
// FirstIndex implements the LogStore interface
[ "FirstIndex", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L172-L177
train
nats-io/nats-streaming-server
server/raft_log.go
LastIndex
func (r *raftLog) LastIndex() (uint64, error) { r.RLock() idx, err := r.getIndex(false) r.RUnlock() return idx, err }
go
func (r *raftLog) LastIndex() (uint64, error) { r.RLock() idx, err := r.getIndex(false) r.RUnlock() return idx, err }
[ "func", "(", "r", "*", "raftLog", ")", "LastIndex", "(", ")", "(", "uint64", ",", "error", ")", "{", "r", ".", "RLock", "(", ")", "\n", "idx", ",", "err", ":=", "r", ".", "getIndex", "(", "false", ")", "\n", "r", ".", "RUnlock", "(", ")", "\n", "return", "idx", ",", "err", "\n", "}" ]
// LastIndex implements the LogStore interface
[ "LastIndex", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L180-L185
train
nats-io/nats-streaming-server
server/raft_log.go
GetLog
func (r *raftLog) GetLog(idx uint64, log *raft.Log) error { r.RLock() tx, err := r.conn.Begin(false) if err != nil { r.RUnlock() return err } var key [8]byte binary.BigEndian.PutUint64(key[:], idx) bucket := tx.Bucket(logsBucket) val := bucket.Get(key[:]) if val == nil { err = raft.ErrLogNotFound } else { err = r.decodeRaftLog(val, log) } tx.Rollback() r.RUnlock() return err }
go
func (r *raftLog) GetLog(idx uint64, log *raft.Log) error { r.RLock() tx, err := r.conn.Begin(false) if err != nil { r.RUnlock() return err } var key [8]byte binary.BigEndian.PutUint64(key[:], idx) bucket := tx.Bucket(logsBucket) val := bucket.Get(key[:]) if val == nil { err = raft.ErrLogNotFound } else { err = r.decodeRaftLog(val, log) } tx.Rollback() r.RUnlock() return err }
[ "func", "(", "r", "*", "raftLog", ")", "GetLog", "(", "idx", "uint64", ",", "log", "*", "raft", ".", "Log", ")", "error", "{", "r", ".", "RLock", "(", ")", "\n", "tx", ",", "err", ":=", "r", ".", "conn", ".", "Begin", "(", "false", ")", "\n", "if", "err", "!=", "nil", "{", "r", ".", "RUnlock", "(", ")", "\n", "return", "err", "\n", "}", "\n", "var", "key", "[", "8", "]", "byte", "\n", "binary", ".", "BigEndian", ".", "PutUint64", "(", "key", "[", ":", "]", ",", "idx", ")", "\n", "bucket", ":=", "tx", ".", "Bucket", "(", "logsBucket", ")", "\n", "val", ":=", "bucket", ".", "Get", "(", "key", "[", ":", "]", ")", "\n", "if", "val", "==", "nil", "{", "err", "=", "raft", ".", "ErrLogNotFound", "\n", "}", "else", "{", "err", "=", "r", ".", "decodeRaftLog", "(", "val", ",", "log", ")", "\n", "}", "\n", "tx", ".", "Rollback", "(", ")", "\n", "r", ".", "RUnlock", "(", ")", "\n", "return", "err", "\n", "}" ]
// GetLog implements the LogStore interface
[ "GetLog", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L212-L231
train
nats-io/nats-streaming-server
server/raft_log.go
StoreLog
func (r *raftLog) StoreLog(log *raft.Log) error { return r.StoreLogs([]*raft.Log{log}) }
go
func (r *raftLog) StoreLog(log *raft.Log) error { return r.StoreLogs([]*raft.Log{log}) }
[ "func", "(", "r", "*", "raftLog", ")", "StoreLog", "(", "log", "*", "raft", ".", "Log", ")", "error", "{", "return", "r", ".", "StoreLogs", "(", "[", "]", "*", "raft", ".", "Log", "{", "log", "}", ")", "\n", "}" ]
// StoreLog implements the LogStore interface
[ "StoreLog", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L234-L236
train
nats-io/nats-streaming-server
server/raft_log.go
StoreLogs
func (r *raftLog) StoreLogs(logs []*raft.Log) error { r.Lock() tx, err := r.conn.Begin(true) if err != nil { r.Unlock() return err } bucket := tx.Bucket(logsBucket) for _, log := range logs { var ( key [8]byte val []byte ) binary.BigEndian.PutUint64(key[:], log.Index) val, err = r.encodeRaftLog(log) if err != nil { break } err = bucket.Put(key[:], val) if err != nil { break } } if err != nil { tx.Rollback() } else { err = tx.Commit() } r.Unlock() return err }
go
func (r *raftLog) StoreLogs(logs []*raft.Log) error { r.Lock() tx, err := r.conn.Begin(true) if err != nil { r.Unlock() return err } bucket := tx.Bucket(logsBucket) for _, log := range logs { var ( key [8]byte val []byte ) binary.BigEndian.PutUint64(key[:], log.Index) val, err = r.encodeRaftLog(log) if err != nil { break } err = bucket.Put(key[:], val) if err != nil { break } } if err != nil { tx.Rollback() } else { err = tx.Commit() } r.Unlock() return err }
[ "func", "(", "r", "*", "raftLog", ")", "StoreLogs", "(", "logs", "[", "]", "*", "raft", ".", "Log", ")", "error", "{", "r", ".", "Lock", "(", ")", "\n", "tx", ",", "err", ":=", "r", ".", "conn", ".", "Begin", "(", "true", ")", "\n", "if", "err", "!=", "nil", "{", "r", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}", "\n", "bucket", ":=", "tx", ".", "Bucket", "(", "logsBucket", ")", "\n", "for", "_", ",", "log", ":=", "range", "logs", "{", "var", "(", "key", "[", "8", "]", "byte", "\n", "val", "[", "]", "byte", "\n", ")", "\n", "binary", ".", "BigEndian", ".", "PutUint64", "(", "key", "[", ":", "]", ",", "log", ".", "Index", ")", "\n", "val", ",", "err", "=", "r", ".", "encodeRaftLog", "(", "log", ")", "\n", "if", "err", "!=", "nil", "{", "break", "\n", "}", "\n", "err", "=", "bucket", ".", "Put", "(", "key", "[", ":", "]", ",", "val", ")", "\n", "if", "err", "!=", "nil", "{", "break", "\n", "}", "\n", "}", "\n", "if", "err", "!=", "nil", "{", "tx", ".", "Rollback", "(", ")", "\n", "}", "else", "{", "err", "=", "tx", ".", "Commit", "(", ")", "\n", "}", "\n", "r", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}" ]
// StoreLogs implements the LogStore interface
[ "StoreLogs", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L239-L269
train
nats-io/nats-streaming-server
server/raft_log.go
DeleteRange
func (r *raftLog) DeleteRange(min, max uint64) (retErr error) { r.Lock() defer r.Unlock() start := time.Now() r.log.Noticef("Deleting raft logs from %v to %v", min, max) err := r.deleteRange(min, max) dur := time.Since(start) durTxt := fmt.Sprintf("Deletion took %v", dur) if dur > 2*time.Second { r.log.Errorf(durTxt) } else { r.log.Noticef(durTxt) } return err }
go
func (r *raftLog) DeleteRange(min, max uint64) (retErr error) { r.Lock() defer r.Unlock() start := time.Now() r.log.Noticef("Deleting raft logs from %v to %v", min, max) err := r.deleteRange(min, max) dur := time.Since(start) durTxt := fmt.Sprintf("Deletion took %v", dur) if dur > 2*time.Second { r.log.Errorf(durTxt) } else { r.log.Noticef(durTxt) } return err }
[ "func", "(", "r", "*", "raftLog", ")", "DeleteRange", "(", "min", ",", "max", "uint64", ")", "(", "retErr", "error", ")", "{", "r", ".", "Lock", "(", ")", "\n", "defer", "r", ".", "Unlock", "(", ")", "\n\n", "start", ":=", "time", ".", "Now", "(", ")", "\n", "r", ".", "log", ".", "Noticef", "(", "\"", "\"", ",", "min", ",", "max", ")", "\n", "err", ":=", "r", ".", "deleteRange", "(", "min", ",", "max", ")", "\n", "dur", ":=", "time", ".", "Since", "(", "start", ")", "\n", "durTxt", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "dur", ")", "\n", "if", "dur", ">", "2", "*", "time", ".", "Second", "{", "r", ".", "log", ".", "Errorf", "(", "durTxt", ")", "\n", "}", "else", "{", "r", ".", "log", ".", "Noticef", "(", "durTxt", ")", "\n", "}", "\n", "return", "err", "\n", "}" ]
// DeleteRange implements the LogStore interface
[ "DeleteRange", "implements", "the", "LogStore", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L272-L287
train
nats-io/nats-streaming-server
server/raft_log.go
Set
func (r *raftLog) Set(k, v []byte) error { r.Lock() tx, err := r.conn.Begin(true) if err != nil { r.Unlock() return err } bucket := tx.Bucket(confBucket) err = bucket.Put(k, v) if err != nil { tx.Rollback() } else { err = tx.Commit() } r.Unlock() return err }
go
func (r *raftLog) Set(k, v []byte) error { r.Lock() tx, err := r.conn.Begin(true) if err != nil { r.Unlock() return err } bucket := tx.Bucket(confBucket) err = bucket.Put(k, v) if err != nil { tx.Rollback() } else { err = tx.Commit() } r.Unlock() return err }
[ "func", "(", "r", "*", "raftLog", ")", "Set", "(", "k", ",", "v", "[", "]", "byte", ")", "error", "{", "r", ".", "Lock", "(", ")", "\n", "tx", ",", "err", ":=", "r", ".", "conn", ".", "Begin", "(", "true", ")", "\n", "if", "err", "!=", "nil", "{", "r", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}", "\n", "bucket", ":=", "tx", ".", "Bucket", "(", "confBucket", ")", "\n", "err", "=", "bucket", ".", "Put", "(", "k", ",", "v", ")", "\n", "if", "err", "!=", "nil", "{", "tx", ".", "Rollback", "(", ")", "\n", "}", "else", "{", "err", "=", "tx", ".", "Commit", "(", ")", "\n", "}", "\n", "r", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}" ]
// Set implements the Stable interface
[ "Set", "implements", "the", "Stable", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L314-L330
train
nats-io/nats-streaming-server
server/raft_log.go
Get
func (r *raftLog) Get(k []byte) ([]byte, error) { r.RLock() tx, err := r.conn.Begin(false) if err != nil { r.RUnlock() return nil, err } var v []byte bucket := tx.Bucket(confBucket) val := bucket.Get(k) if val == nil { err = errKeyNotFound } else { // Make a copy v = append([]byte(nil), val...) } tx.Rollback() r.RUnlock() return v, err }
go
func (r *raftLog) Get(k []byte) ([]byte, error) { r.RLock() tx, err := r.conn.Begin(false) if err != nil { r.RUnlock() return nil, err } var v []byte bucket := tx.Bucket(confBucket) val := bucket.Get(k) if val == nil { err = errKeyNotFound } else { // Make a copy v = append([]byte(nil), val...) } tx.Rollback() r.RUnlock() return v, err }
[ "func", "(", "r", "*", "raftLog", ")", "Get", "(", "k", "[", "]", "byte", ")", "(", "[", "]", "byte", ",", "error", ")", "{", "r", ".", "RLock", "(", ")", "\n", "tx", ",", "err", ":=", "r", ".", "conn", ".", "Begin", "(", "false", ")", "\n", "if", "err", "!=", "nil", "{", "r", ".", "RUnlock", "(", ")", "\n", "return", "nil", ",", "err", "\n", "}", "\n", "var", "v", "[", "]", "byte", "\n", "bucket", ":=", "tx", ".", "Bucket", "(", "confBucket", ")", "\n", "val", ":=", "bucket", ".", "Get", "(", "k", ")", "\n", "if", "val", "==", "nil", "{", "err", "=", "errKeyNotFound", "\n", "}", "else", "{", "// Make a copy", "v", "=", "append", "(", "[", "]", "byte", "(", "nil", ")", ",", "val", "...", ")", "\n", "}", "\n", "tx", ".", "Rollback", "(", ")", "\n", "r", ".", "RUnlock", "(", ")", "\n", "return", "v", ",", "err", "\n", "}" ]
// Get implements the Stable interface
[ "Get", "implements", "the", "Stable", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L333-L352
train
nats-io/nats-streaming-server
server/raft_log.go
SetUint64
func (r *raftLog) SetUint64(k []byte, v uint64) error { var vbytes [8]byte binary.BigEndian.PutUint64(vbytes[:], v) err := r.Set(k, vbytes[:]) return err }
go
func (r *raftLog) SetUint64(k []byte, v uint64) error { var vbytes [8]byte binary.BigEndian.PutUint64(vbytes[:], v) err := r.Set(k, vbytes[:]) return err }
[ "func", "(", "r", "*", "raftLog", ")", "SetUint64", "(", "k", "[", "]", "byte", ",", "v", "uint64", ")", "error", "{", "var", "vbytes", "[", "8", "]", "byte", "\n", "binary", ".", "BigEndian", ".", "PutUint64", "(", "vbytes", "[", ":", "]", ",", "v", ")", "\n", "err", ":=", "r", ".", "Set", "(", "k", ",", "vbytes", "[", ":", "]", ")", "\n", "return", "err", "\n", "}" ]
// SetUint64 implements the Stable interface
[ "SetUint64", "implements", "the", "Stable", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L355-L360
train
nats-io/nats-streaming-server
server/raft_log.go
GetUint64
func (r *raftLog) GetUint64(k []byte) (uint64, error) { var v uint64 vbytes, err := r.Get(k) if err == nil { v = binary.BigEndian.Uint64(vbytes) } return v, err }
go
func (r *raftLog) GetUint64(k []byte) (uint64, error) { var v uint64 vbytes, err := r.Get(k) if err == nil { v = binary.BigEndian.Uint64(vbytes) } return v, err }
[ "func", "(", "r", "*", "raftLog", ")", "GetUint64", "(", "k", "[", "]", "byte", ")", "(", "uint64", ",", "error", ")", "{", "var", "v", "uint64", "\n", "vbytes", ",", "err", ":=", "r", ".", "Get", "(", "k", ")", "\n", "if", "err", "==", "nil", "{", "v", "=", "binary", ".", "BigEndian", ".", "Uint64", "(", "vbytes", ")", "\n", "}", "\n", "return", "v", ",", "err", "\n", "}" ]
// GetUint64 implements the Stable interface
[ "GetUint64", "implements", "the", "Stable", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/raft_log.go#L363-L370
train
nats-io/nats-streaming-server
server/snapshot.go
Restore
func (r *raftFSM) Restore(snapshot io.ReadCloser) (retErr error) { s := r.server defer snapshot.Close() r.Lock() defer r.Unlock() // This function may be invoked directly from raft.NewRaft() when // the node is initialized and if there were exisiting local snapshots, // or later, when catching up with a leader. We behave differently // depending on the situation. So we need to know if we are called // from NewRaft(). // // To do so, we first look at the number of local snapshots before // calling NewRaft(). If the number is > 0, it means that Raft will // call us within NewRaft(). Raft will restore the latest snapshot // first, and only in case of Restore() returning an error will move // to the next (earliest) one. When there are none and Restore() still // returns an error raft.NewRaft() will return an error. // // So on error we decrement the number of snapshots, on success we set // it to 0. This means that next time Restore() is invoked, we know it // is restoring from a leader, not from the local snapshots. inNewRaftCall := r.snapshotsOnInit != 0 if inNewRaftCall { defer func() { if retErr != nil { r.snapshotsOnInit-- } else { r.snapshotsOnInit = 0 } }() } else { s.log.Noticef("restoring from snapshot") defer s.log.Noticef("done restoring from snapshot") } // We need to drop current state. The server will recover from snapshot // and all newer Raft entry logs (basically the entire state is being // reconstructed from this point on). for _, c := range s.channels.getAll() { for _, sub := range c.ss.getAllSubs() { sub.RLock() clientID := sub.ClientID sub.RUnlock() if err := s.unsubscribeSub(c, clientID, "unsub", sub, false, false); err != nil { return err } } c.store.Subs.Flush() } for clientID := range s.clients.getClients() { if _, err := s.clients.unregister(clientID); err != nil { return err } } sizeBuf := make([]byte, 4) // Read the snapshot size. if _, err := io.ReadFull(snapshot, sizeBuf); err != nil { if err == io.EOF { return nil } return err } // Read the snapshot. size := util.ByteOrder.Uint32(sizeBuf) buf := make([]byte, size) if _, err := io.ReadFull(snapshot, buf); err != nil { return err } serverSnap := &spb.RaftSnapshot{} if err := serverSnap.Unmarshal(buf); err != nil { panic(err) } if err := r.restoreClientsFromSnapshot(serverSnap); err != nil { return err } return r.restoreChannelsFromSnapshot(serverSnap, inNewRaftCall) }
go
func (r *raftFSM) Restore(snapshot io.ReadCloser) (retErr error) { s := r.server defer snapshot.Close() r.Lock() defer r.Unlock() // This function may be invoked directly from raft.NewRaft() when // the node is initialized and if there were exisiting local snapshots, // or later, when catching up with a leader. We behave differently // depending on the situation. So we need to know if we are called // from NewRaft(). // // To do so, we first look at the number of local snapshots before // calling NewRaft(). If the number is > 0, it means that Raft will // call us within NewRaft(). Raft will restore the latest snapshot // first, and only in case of Restore() returning an error will move // to the next (earliest) one. When there are none and Restore() still // returns an error raft.NewRaft() will return an error. // // So on error we decrement the number of snapshots, on success we set // it to 0. This means that next time Restore() is invoked, we know it // is restoring from a leader, not from the local snapshots. inNewRaftCall := r.snapshotsOnInit != 0 if inNewRaftCall { defer func() { if retErr != nil { r.snapshotsOnInit-- } else { r.snapshotsOnInit = 0 } }() } else { s.log.Noticef("restoring from snapshot") defer s.log.Noticef("done restoring from snapshot") } // We need to drop current state. The server will recover from snapshot // and all newer Raft entry logs (basically the entire state is being // reconstructed from this point on). for _, c := range s.channels.getAll() { for _, sub := range c.ss.getAllSubs() { sub.RLock() clientID := sub.ClientID sub.RUnlock() if err := s.unsubscribeSub(c, clientID, "unsub", sub, false, false); err != nil { return err } } c.store.Subs.Flush() } for clientID := range s.clients.getClients() { if _, err := s.clients.unregister(clientID); err != nil { return err } } sizeBuf := make([]byte, 4) // Read the snapshot size. if _, err := io.ReadFull(snapshot, sizeBuf); err != nil { if err == io.EOF { return nil } return err } // Read the snapshot. size := util.ByteOrder.Uint32(sizeBuf) buf := make([]byte, size) if _, err := io.ReadFull(snapshot, buf); err != nil { return err } serverSnap := &spb.RaftSnapshot{} if err := serverSnap.Unmarshal(buf); err != nil { panic(err) } if err := r.restoreClientsFromSnapshot(serverSnap); err != nil { return err } return r.restoreChannelsFromSnapshot(serverSnap, inNewRaftCall) }
[ "func", "(", "r", "*", "raftFSM", ")", "Restore", "(", "snapshot", "io", ".", "ReadCloser", ")", "(", "retErr", "error", ")", "{", "s", ":=", "r", ".", "server", "\n", "defer", "snapshot", ".", "Close", "(", ")", "\n\n", "r", ".", "Lock", "(", ")", "\n", "defer", "r", ".", "Unlock", "(", ")", "\n\n", "// This function may be invoked directly from raft.NewRaft() when", "// the node is initialized and if there were exisiting local snapshots,", "// or later, when catching up with a leader. We behave differently", "// depending on the situation. So we need to know if we are called", "// from NewRaft().", "//", "// To do so, we first look at the number of local snapshots before", "// calling NewRaft(). If the number is > 0, it means that Raft will", "// call us within NewRaft(). Raft will restore the latest snapshot", "// first, and only in case of Restore() returning an error will move", "// to the next (earliest) one. When there are none and Restore() still", "// returns an error raft.NewRaft() will return an error.", "//", "// So on error we decrement the number of snapshots, on success we set", "// it to 0. This means that next time Restore() is invoked, we know it", "// is restoring from a leader, not from the local snapshots.", "inNewRaftCall", ":=", "r", ".", "snapshotsOnInit", "!=", "0", "\n", "if", "inNewRaftCall", "{", "defer", "func", "(", ")", "{", "if", "retErr", "!=", "nil", "{", "r", ".", "snapshotsOnInit", "--", "\n", "}", "else", "{", "r", ".", "snapshotsOnInit", "=", "0", "\n", "}", "\n", "}", "(", ")", "\n", "}", "else", "{", "s", ".", "log", ".", "Noticef", "(", "\"", "\"", ")", "\n", "defer", "s", ".", "log", ".", "Noticef", "(", "\"", "\"", ")", "\n", "}", "\n\n", "// We need to drop current state. The server will recover from snapshot", "// and all newer Raft entry logs (basically the entire state is being", "// reconstructed from this point on).", "for", "_", ",", "c", ":=", "range", "s", ".", "channels", ".", "getAll", "(", ")", "{", "for", "_", ",", "sub", ":=", "range", "c", ".", "ss", ".", "getAllSubs", "(", ")", "{", "sub", ".", "RLock", "(", ")", "\n", "clientID", ":=", "sub", ".", "ClientID", "\n", "sub", ".", "RUnlock", "(", ")", "\n", "if", "err", ":=", "s", ".", "unsubscribeSub", "(", "c", ",", "clientID", ",", "\"", "\"", ",", "sub", ",", "false", ",", "false", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "}", "\n", "c", ".", "store", ".", "Subs", ".", "Flush", "(", ")", "\n", "}", "\n", "for", "clientID", ":=", "range", "s", ".", "clients", ".", "getClients", "(", ")", "{", "if", "_", ",", "err", ":=", "s", ".", "clients", ".", "unregister", "(", "clientID", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "}", "\n\n", "sizeBuf", ":=", "make", "(", "[", "]", "byte", ",", "4", ")", "\n", "// Read the snapshot size.", "if", "_", ",", "err", ":=", "io", ".", "ReadFull", "(", "snapshot", ",", "sizeBuf", ")", ";", "err", "!=", "nil", "{", "if", "err", "==", "io", ".", "EOF", "{", "return", "nil", "\n", "}", "\n", "return", "err", "\n", "}", "\n", "// Read the snapshot.", "size", ":=", "util", ".", "ByteOrder", ".", "Uint32", "(", "sizeBuf", ")", "\n", "buf", ":=", "make", "(", "[", "]", "byte", ",", "size", ")", "\n", "if", "_", ",", "err", ":=", "io", ".", "ReadFull", "(", "snapshot", ",", "buf", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n\n", "serverSnap", ":=", "&", "spb", ".", "RaftSnapshot", "{", "}", "\n", "if", "err", ":=", "serverSnap", ".", "Unmarshal", "(", "buf", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "if", "err", ":=", "r", ".", "restoreClientsFromSnapshot", "(", "serverSnap", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "return", "r", ".", "restoreChannelsFromSnapshot", "(", "serverSnap", ",", "inNewRaftCall", ")", "\n", "}" ]
// Restore is used to restore an FSM from a snapshot. It is not called // concurrently with any other command. The FSM must discard all previous // state.
[ "Restore", "is", "used", "to", "restore", "an", "FSM", "from", "a", "snapshot", ".", "It", "is", "not", "called", "concurrently", "with", "any", "other", "command", ".", "The", "FSM", "must", "discard", "all", "previous", "state", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/server/snapshot.go#L225-L305
train
nats-io/nats-streaming-server
stores/raftstore.go
NewRaftStore
func NewRaftStore(log logger.Logger, s Store, limits *StoreLimits) *RaftStore { return &RaftStore{Store: s, log: log} }
go
func NewRaftStore(log logger.Logger, s Store, limits *StoreLimits) *RaftStore { return &RaftStore{Store: s, log: log} }
[ "func", "NewRaftStore", "(", "log", "logger", ".", "Logger", ",", "s", "Store", ",", "limits", "*", "StoreLimits", ")", "*", "RaftStore", "{", "return", "&", "RaftStore", "{", "Store", ":", "s", ",", "log", ":", "log", "}", "\n", "}" ]
// NewRaftStore returns an instarce of a RaftStore
[ "NewRaftStore", "returns", "an", "instarce", "of", "a", "RaftStore" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/raftstore.go#L40-L42
train
nats-io/nats-streaming-server
stores/common.go
setLimits
func (gs *genericStore) setLimits(limits *StoreLimits) error { // Make a copy. gs.limits = limits.Clone() // Build will validate and apply inheritance if no error. if err := gs.limits.Build(); err != nil { return err } // We don't need the PerChannel map and the sublist. So replace // the map with the sublist instead. gs.sublist = util.NewSublist() for key, val := range gs.limits.PerChannel { // val is already a copy of the original limits.PerChannel[key], // so don't need to make a copy again, we own this. gs.sublist.Insert(key, val) } // Get rid of the map now. gs.limits.PerChannel = nil return nil }
go
func (gs *genericStore) setLimits(limits *StoreLimits) error { // Make a copy. gs.limits = limits.Clone() // Build will validate and apply inheritance if no error. if err := gs.limits.Build(); err != nil { return err } // We don't need the PerChannel map and the sublist. So replace // the map with the sublist instead. gs.sublist = util.NewSublist() for key, val := range gs.limits.PerChannel { // val is already a copy of the original limits.PerChannel[key], // so don't need to make a copy again, we own this. gs.sublist.Insert(key, val) } // Get rid of the map now. gs.limits.PerChannel = nil return nil }
[ "func", "(", "gs", "*", "genericStore", ")", "setLimits", "(", "limits", "*", "StoreLimits", ")", "error", "{", "// Make a copy.", "gs", ".", "limits", "=", "limits", ".", "Clone", "(", ")", "\n", "// Build will validate and apply inheritance if no error.", "if", "err", ":=", "gs", ".", "limits", ".", "Build", "(", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "// We don't need the PerChannel map and the sublist. So replace", "// the map with the sublist instead.", "gs", ".", "sublist", "=", "util", ".", "NewSublist", "(", ")", "\n", "for", "key", ",", "val", ":=", "range", "gs", ".", "limits", ".", "PerChannel", "{", "// val is already a copy of the original limits.PerChannel[key],", "// so don't need to make a copy again, we own this.", "gs", ".", "sublist", ".", "Insert", "(", "key", ",", "val", ")", "\n", "}", "\n", "// Get rid of the map now.", "gs", ".", "limits", ".", "PerChannel", "=", "nil", "\n", "return", "nil", "\n", "}" ]
// setLimits makes a copy of the given StoreLimits, // validates the limits and if ok, applies the inheritance.
[ "setLimits", "makes", "a", "copy", "of", "the", "given", "StoreLimits", "validates", "the", "limits", "and", "if", "ok", "applies", "the", "inheritance", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L115-L133
train
nats-io/nats-streaming-server
stores/common.go
getChannelLimits
func (gs *genericStore) getChannelLimits(channel string) *ChannelLimits { r := gs.sublist.Match(channel) if len(r) == 0 { // If there is no match, that means we need to use the global limits. return &gs.limits.ChannelLimits } // If there is a match, use the limits from the last element because // we know that the returned array is ordered from widest to narrowest, // and the only literal that there is would be the channel we are // looking up. return r[len(r)-1].(*ChannelLimits) }
go
func (gs *genericStore) getChannelLimits(channel string) *ChannelLimits { r := gs.sublist.Match(channel) if len(r) == 0 { // If there is no match, that means we need to use the global limits. return &gs.limits.ChannelLimits } // If there is a match, use the limits from the last element because // we know that the returned array is ordered from widest to narrowest, // and the only literal that there is would be the channel we are // looking up. return r[len(r)-1].(*ChannelLimits) }
[ "func", "(", "gs", "*", "genericStore", ")", "getChannelLimits", "(", "channel", "string", ")", "*", "ChannelLimits", "{", "r", ":=", "gs", ".", "sublist", ".", "Match", "(", "channel", ")", "\n", "if", "len", "(", "r", ")", "==", "0", "{", "// If there is no match, that means we need to use the global limits.", "return", "&", "gs", ".", "limits", ".", "ChannelLimits", "\n", "}", "\n", "// If there is a match, use the limits from the last element because", "// we know that the returned array is ordered from widest to narrowest,", "// and the only literal that there is would be the channel we are", "// looking up.", "return", "r", "[", "len", "(", "r", ")", "-", "1", "]", ".", "(", "*", "ChannelLimits", ")", "\n", "}" ]
// Returns the appropriate limits for this channel based on inheritance. // The channel is assumed to be a literal, and the store lock held on entry.
[ "Returns", "the", "appropriate", "limits", "for", "this", "channel", "based", "on", "inheritance", ".", "The", "channel", "is", "assumed", "to", "be", "a", "literal", "and", "the", "store", "lock", "held", "on", "entry", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L137-L148
train
nats-io/nats-streaming-server
stores/common.go
GetChannelLimits
func (gs *genericStore) GetChannelLimits(channel string) *ChannelLimits { gs.RLock() defer gs.RUnlock() c := gs.channels[channel] if c == nil { return nil } // Return a copy cl := *gs.getChannelLimits(channel) return &cl }
go
func (gs *genericStore) GetChannelLimits(channel string) *ChannelLimits { gs.RLock() defer gs.RUnlock() c := gs.channels[channel] if c == nil { return nil } // Return a copy cl := *gs.getChannelLimits(channel) return &cl }
[ "func", "(", "gs", "*", "genericStore", ")", "GetChannelLimits", "(", "channel", "string", ")", "*", "ChannelLimits", "{", "gs", ".", "RLock", "(", ")", "\n", "defer", "gs", ".", "RUnlock", "(", ")", "\n", "c", ":=", "gs", ".", "channels", "[", "channel", "]", "\n", "if", "c", "==", "nil", "{", "return", "nil", "\n", "}", "\n", "// Return a copy", "cl", ":=", "*", "gs", ".", "getChannelLimits", "(", "channel", ")", "\n", "return", "&", "cl", "\n", "}" ]
// GetChannelLimits implements the Store interface
[ "GetChannelLimits", "implements", "the", "Store", "interface" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L151-L161
train
nats-io/nats-streaming-server
stores/common.go
SetLimits
func (gs *genericStore) SetLimits(limits *StoreLimits) error { gs.Lock() err := gs.setLimits(limits) gs.Unlock() return err }
go
func (gs *genericStore) SetLimits(limits *StoreLimits) error { gs.Lock() err := gs.setLimits(limits) gs.Unlock() return err }
[ "func", "(", "gs", "*", "genericStore", ")", "SetLimits", "(", "limits", "*", "StoreLimits", ")", "error", "{", "gs", ".", "Lock", "(", ")", "\n", "err", ":=", "gs", ".", "setLimits", "(", "limits", ")", "\n", "gs", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}" ]
// SetLimits sets limits for this store
[ "SetLimits", "sets", "limits", "for", "this", "store" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L164-L169
train
nats-io/nats-streaming-server
stores/common.go
canAddChannel
func (gs *genericStore) canAddChannel(name string) error { if gs.channels[name] != nil { return ErrAlreadyExists } if gs.limits.MaxChannels > 0 && len(gs.channels) >= gs.limits.MaxChannels { return ErrTooManyChannels } return nil }
go
func (gs *genericStore) canAddChannel(name string) error { if gs.channels[name] != nil { return ErrAlreadyExists } if gs.limits.MaxChannels > 0 && len(gs.channels) >= gs.limits.MaxChannels { return ErrTooManyChannels } return nil }
[ "func", "(", "gs", "*", "genericStore", ")", "canAddChannel", "(", "name", "string", ")", "error", "{", "if", "gs", ".", "channels", "[", "name", "]", "!=", "nil", "{", "return", "ErrAlreadyExists", "\n", "}", "\n", "if", "gs", ".", "limits", ".", "MaxChannels", ">", "0", "&&", "len", "(", "gs", ".", "channels", ")", ">=", "gs", ".", "limits", ".", "MaxChannels", "{", "return", "ErrTooManyChannels", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// canAddChannel returns true if the current number of channels is below the limit. // If a channel named `channelName` alreadt exists, an error is returned. // Store lock is assumed to be locked.
[ "canAddChannel", "returns", "true", "if", "the", "current", "number", "of", "channels", "is", "below", "the", "limit", ".", "If", "a", "channel", "named", "channelName", "alreadt", "exists", "an", "error", "is", "returned", ".", "Store", "lock", "is", "assumed", "to", "be", "locked", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L203-L211
train
nats-io/nats-streaming-server
stores/common.go
Close
func (gs *genericStore) Close() error { gs.Lock() defer gs.Unlock() if gs.closed { return nil } gs.closed = true return gs.close() }
go
func (gs *genericStore) Close() error { gs.Lock() defer gs.Unlock() if gs.closed { return nil } gs.closed = true return gs.close() }
[ "func", "(", "gs", "*", "genericStore", ")", "Close", "(", ")", "error", "{", "gs", ".", "Lock", "(", ")", "\n", "defer", "gs", ".", "Unlock", "(", ")", "\n", "if", "gs", ".", "closed", "{", "return", "nil", "\n", "}", "\n", "gs", ".", "closed", "=", "true", "\n", "return", "gs", ".", "close", "(", ")", "\n", "}" ]
// Close closes all stores
[ "Close", "closes", "all", "stores" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L224-L232
train
nats-io/nats-streaming-server
stores/common.go
close
func (gs *genericStore) close() error { var err error var lerr error for _, cs := range gs.channels { lerr = cs.Subs.Close() if lerr != nil && err == nil { err = lerr } lerr = cs.Msgs.Close() if lerr != nil && err == nil { err = lerr } } return err }
go
func (gs *genericStore) close() error { var err error var lerr error for _, cs := range gs.channels { lerr = cs.Subs.Close() if lerr != nil && err == nil { err = lerr } lerr = cs.Msgs.Close() if lerr != nil && err == nil { err = lerr } } return err }
[ "func", "(", "gs", "*", "genericStore", ")", "close", "(", ")", "error", "{", "var", "err", "error", "\n", "var", "lerr", "error", "\n\n", "for", "_", ",", "cs", ":=", "range", "gs", ".", "channels", "{", "lerr", "=", "cs", ".", "Subs", ".", "Close", "(", ")", "\n", "if", "lerr", "!=", "nil", "&&", "err", "==", "nil", "{", "err", "=", "lerr", "\n", "}", "\n", "lerr", "=", "cs", ".", "Msgs", ".", "Close", "(", ")", "\n", "if", "lerr", "!=", "nil", "&&", "err", "==", "nil", "{", "err", "=", "lerr", "\n", "}", "\n", "}", "\n", "return", "err", "\n", "}" ]
// close closes all stores. Store lock is assumed held on entry
[ "close", "closes", "all", "stores", ".", "Store", "lock", "is", "assumed", "held", "on", "entry" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L235-L250
train
nats-io/nats-streaming-server
stores/common.go
State
func (gms *genericMsgStore) State() (numMessages int, byteSize uint64, err error) { gms.RLock() c, b := gms.totalCount, gms.totalBytes gms.RUnlock() return c, b, nil }
go
func (gms *genericMsgStore) State() (numMessages int, byteSize uint64, err error) { gms.RLock() c, b := gms.totalCount, gms.totalBytes gms.RUnlock() return c, b, nil }
[ "func", "(", "gms", "*", "genericMsgStore", ")", "State", "(", ")", "(", "numMessages", "int", ",", "byteSize", "uint64", ",", "err", "error", ")", "{", "gms", ".", "RLock", "(", ")", "\n", "c", ",", "b", ":=", "gms", ".", "totalCount", ",", "gms", ".", "totalBytes", "\n", "gms", ".", "RUnlock", "(", ")", "\n", "return", "c", ",", "b", ",", "nil", "\n", "}" ]
// State returns some statistics related to this store
[ "State", "returns", "some", "statistics", "related", "to", "this", "store" ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L264-L269
train
nats-io/nats-streaming-server
stores/common.go
FirstSequence
func (gms *genericMsgStore) FirstSequence() (uint64, error) { gms.RLock() first := gms.first gms.RUnlock() return first, nil }
go
func (gms *genericMsgStore) FirstSequence() (uint64, error) { gms.RLock() first := gms.first gms.RUnlock() return first, nil }
[ "func", "(", "gms", "*", "genericMsgStore", ")", "FirstSequence", "(", ")", "(", "uint64", ",", "error", ")", "{", "gms", ".", "RLock", "(", ")", "\n", "first", ":=", "gms", ".", "first", "\n", "gms", ".", "RUnlock", "(", ")", "\n", "return", "first", ",", "nil", "\n", "}" ]
// FirstSequence returns sequence for first message stored.
[ "FirstSequence", "returns", "sequence", "for", "first", "message", "stored", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L278-L283
train
nats-io/nats-streaming-server
stores/common.go
LastSequence
func (gms *genericMsgStore) LastSequence() (uint64, error) { gms.RLock() last := gms.last gms.RUnlock() return last, nil }
go
func (gms *genericMsgStore) LastSequence() (uint64, error) { gms.RLock() last := gms.last gms.RUnlock() return last, nil }
[ "func", "(", "gms", "*", "genericMsgStore", ")", "LastSequence", "(", ")", "(", "uint64", ",", "error", ")", "{", "gms", ".", "RLock", "(", ")", "\n", "last", ":=", "gms", ".", "last", "\n", "gms", ".", "RUnlock", "(", ")", "\n", "return", "last", ",", "nil", "\n", "}" ]
// LastSequence returns sequence for last message stored.
[ "LastSequence", "returns", "sequence", "for", "last", "message", "stored", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L286-L291
train
nats-io/nats-streaming-server
stores/common.go
FirstAndLastSequence
func (gms *genericMsgStore) FirstAndLastSequence() (uint64, uint64, error) { gms.RLock() first, last := gms.first, gms.last gms.RUnlock() return first, last, nil }
go
func (gms *genericMsgStore) FirstAndLastSequence() (uint64, uint64, error) { gms.RLock() first, last := gms.first, gms.last gms.RUnlock() return first, last, nil }
[ "func", "(", "gms", "*", "genericMsgStore", ")", "FirstAndLastSequence", "(", ")", "(", "uint64", ",", "uint64", ",", "error", ")", "{", "gms", ".", "RLock", "(", ")", "\n", "first", ",", "last", ":=", "gms", ".", "first", ",", "gms", ".", "last", "\n", "gms", ".", "RUnlock", "(", ")", "\n", "return", "first", ",", "last", ",", "nil", "\n", "}" ]
// FirstAndLastSequence returns sequences for the first and last messages stored.
[ "FirstAndLastSequence", "returns", "sequences", "for", "the", "first", "and", "last", "messages", "stored", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L294-L299
train
nats-io/nats-streaming-server
stores/common.go
CreateSub
func (gss *genericSubStore) CreateSub(sub *spb.SubState) error { gss.Lock() err := gss.createSub(sub) gss.Unlock() return err }
go
func (gss *genericSubStore) CreateSub(sub *spb.SubState) error { gss.Lock() err := gss.createSub(sub) gss.Unlock() return err }
[ "func", "(", "gss", "*", "genericSubStore", ")", "CreateSub", "(", "sub", "*", "spb", ".", "SubState", ")", "error", "{", "gss", ".", "Lock", "(", ")", "\n", "err", ":=", "gss", ".", "createSub", "(", "sub", ")", "\n", "gss", ".", "Unlock", "(", ")", "\n", "return", "err", "\n", "}" ]
// CreateSub records a new subscription represented by SubState. On success, // it records the subscription's ID in SubState.ID. This ID is to be used // by the other SubStore methods.
[ "CreateSub", "records", "a", "new", "subscription", "represented", "by", "SubState", ".", "On", "success", "it", "records", "the", "subscription", "s", "ID", "in", "SubState", ".", "ID", ".", "This", "ID", "is", "to", "be", "used", "by", "the", "other", "SubStore", "methods", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L354-L359
train
nats-io/nats-streaming-server
stores/common.go
createSub
func (gss *genericSubStore) createSub(sub *spb.SubState) error { if gss.limits.MaxSubscriptions > 0 && len(gss.subs) >= gss.limits.MaxSubscriptions { return ErrTooManySubs } // Bump the max value before assigning it to the new subscription. gss.maxSubID++ // This new subscription has the max value. sub.ID = gss.maxSubID // Store anything. Some implementations may replace with specific // object. gss.subs[sub.ID] = emptySub return nil }
go
func (gss *genericSubStore) createSub(sub *spb.SubState) error { if gss.limits.MaxSubscriptions > 0 && len(gss.subs) >= gss.limits.MaxSubscriptions { return ErrTooManySubs } // Bump the max value before assigning it to the new subscription. gss.maxSubID++ // This new subscription has the max value. sub.ID = gss.maxSubID // Store anything. Some implementations may replace with specific // object. gss.subs[sub.ID] = emptySub return nil }
[ "func", "(", "gss", "*", "genericSubStore", ")", "createSub", "(", "sub", "*", "spb", ".", "SubState", ")", "error", "{", "if", "gss", ".", "limits", ".", "MaxSubscriptions", ">", "0", "&&", "len", "(", "gss", ".", "subs", ")", ">=", "gss", ".", "limits", ".", "MaxSubscriptions", "{", "return", "ErrTooManySubs", "\n", "}", "\n\n", "// Bump the max value before assigning it to the new subscription.", "gss", ".", "maxSubID", "++", "\n\n", "// This new subscription has the max value.", "sub", ".", "ID", "=", "gss", ".", "maxSubID", "\n", "// Store anything. Some implementations may replace with specific", "// object.", "gss", ".", "subs", "[", "sub", ".", "ID", "]", "=", "emptySub", "\n\n", "return", "nil", "\n", "}" ]
// createSub checks that the number of subscriptions is below the max // and if so, assigns a new subscription ID and keep track of it in a map. // Lock is assumed to be held on entry.
[ "createSub", "checks", "that", "the", "number", "of", "subscriptions", "is", "below", "the", "max", "and", "if", "so", "assigns", "a", "new", "subscription", "ID", "and", "keep", "track", "of", "it", "in", "a", "map", ".", "Lock", "is", "assumed", "to", "be", "held", "on", "entry", "." ]
57c6c84265c0012a1efef365703c221329804d4c
https://github.com/nats-io/nats-streaming-server/blob/57c6c84265c0012a1efef365703c221329804d4c/stores/common.go#L369-L384
train