refactor: converted int64 to int32 where appropriate.

refactor: reduced usage of fmt in favor of logs and string concatication.

refactor: minor re-ordering of the data structures to reduce storage space required.
This commit is contained in:
James Wells 2023-03-21 07:34:10 -07:00
parent 8640d42132
commit 1bcc682b7c
Signed by: jwells
GPG key ID: 73196D10B8E65666
9 changed files with 53 additions and 48 deletions

View file

@ -1,5 +1,5 @@
FROM nikatjef/golang:1.20 as builder
ARG SEMVER=${SEMVER:-0.0.7}
ARG SEMVER=${SEMVER:-0.0.8}
WORKDIR /gagent
COPY . .

View file

@ -56,7 +56,7 @@ var environment struct {
* This is the application version number. It can be overridden at build time
* using the -ldflags "-X main.semVER=0.0.1" option.
*/
var semVER = "0.0.6"
var semVER = "0.0.8"
/*
* This is the application configuration. It is populated from the configuration
@ -290,7 +290,7 @@ func init() {
if config.MonitorPort != 0 {
go func() {
log.Printf("[INFO] Starting Prometheus metrics exporter on port %d\n", config.MonitorPort)
log.Fatal(http.ListenAndServe(string(config.ListenAddr)+strconv.FormatInt(config.MonitorPort, 10), nil))
log.Fatal(http.ListenAndServe(string(config.ListenAddr)+strconv.Itoa(config.MonitorPort), nil))
}()
}
}

View file

@ -8,11 +8,11 @@ type GagentConfig struct {
Mode string `hcl:"mode,attr"`
UUID string `hcl:"uuid,optional"`
ListenAddr string `hcl:"listenaddr,optional"`
MonitorPort int64 `hcl:"monitorport,optional"`
ClientPort int64 `hcl:"clientport,optional"`
RouterPort int64 `hcl:"routerport,optional"`
WorkerPort int64 `hcl:"workerport,optional"`
ChainDBPath string `hcl:"chaindbpath,optional"`
MonitorPort int `hcl:"monitorport,optional"`
ClientPort int `hcl:"clientport,optional"`
RouterPort int `hcl:"routerport,optional"`
WorkerPort int `hcl:"workerport,optional"`
Clients []*ClientDetails `hcl:"client,block"`
Routers []*RouterDetails `hcl:"router,block"`
Workers []*WorkerDetails `hcl:"worker,block"`
@ -62,27 +62,27 @@ type RouterDetails struct {
*/
RouterAddr string `hcl:"address,attr"`
/*
* G'Agent client will use this port to communicate with the routers.
*/
ClientPort int64 `hcl:"clientport,optional"`
/*
* G'Agent router will use this port to communicate with other routers.
*/
RouterPort int64 `hcl:"routerport,optional"`
/*
* G'Agent worker will use this port to communicate with the routers.
*/
WorkerPort int64 `hcl:"workerport,optional"`
/*
* These tags will be passed to the router upon connection. The router
* will then use these tags to help determine which worker / client to
* send the client's requests and results to.
*/
RouterTags []string `hcl:"tags,optional"`
/*
* G'Agent client will use this port to communicate with the routers.
*/
ClientPort int `hcl:"clientport,optional"`
/*
* G'Agent router will use this port to communicate with other routers.
*/
RouterPort int `hcl:"routerport,optional"`
/*
* G'Agent worker will use this port to communicate with the routers.
*/
WorkerPort int `hcl:"workerport,optional"`
}
/*
@ -111,8 +111,8 @@ type WorkerDetails struct {
type AgentDetails struct {
Client string `hcl:"client"`
Status int64 `hcl:"status"`
Shasum string `hcl:"shasum"`
Status int `hcl:"status"`
Hints []string
ScriptCode []byte
Answer []byte

View file

@ -4,6 +4,7 @@ import (
fmt "fmt"
log "log"
http "net/http"
strconv "strconv"
sync "sync"
gcdb "github.com/dragonheim/gagent/internal/chaindb"
@ -44,7 +45,7 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
db.Init()
workerListener := fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.WorkerPort)
workerListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.WorkerPort)
_ = workerSock.Bind(workerListener)
workers := make([]string, 0)
@ -112,7 +113,7 @@ func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) {
clientSock, _ := zmq.NewSocket(zmq.ROUTER)
defer clientSock.Close()
clientListener := fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.ClientPort)
clientListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.ClientPort)
log.Printf("[DEBUG] Binding to: %s", clientListener)
_ = clientSock.Bind(clientListener)

View file

@ -24,9 +24,9 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
rootBody.SetAttributeValue("mode", cty.StringVal(config.Mode))
rootBody.SetAttributeValue("uuid", cty.StringVal(config.UUID))
rootBody.SetAttributeValue("listenaddr", cty.StringVal("0.0.0.0"))
rootBody.SetAttributeValue("clientport", cty.NumberIntVal(config.ClientPort))
rootBody.SetAttributeValue("routerport", cty.NumberIntVal(config.RouterPort))
rootBody.SetAttributeValue("workerport", cty.NumberIntVal(config.WorkerPort))
rootBody.SetAttributeValue("clientport", cty.NumberIntVal(int64(config.ClientPort)))
rootBody.SetAttributeValue("routerport", cty.NumberIntVal(int64(config.RouterPort)))
rootBody.SetAttributeValue("workerport", cty.NumberIntVal(int64(config.WorkerPort)))
rootBody.AppendNewline()
clientBlock1 := rootBody.AppendNewBlock("client", []string{config.Name})
@ -47,9 +47,9 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
routerBody1 := routerBlock1.Body()
routerBody1.SetAttributeValue("routerid", cty.StringVal(config.UUID))
routerBody1.SetAttributeValue("address", cty.StringVal("127.0.0.1"))
routerBody1.SetAttributeValue("clientport", cty.NumberIntVal(config.ClientPort))
routerBody1.SetAttributeValue("routerport", cty.NumberIntVal(config.RouterPort))
routerBody1.SetAttributeValue("workerport", cty.NumberIntVal(config.WorkerPort))
routerBody1.SetAttributeValue("clientport", cty.NumberIntVal(int64(config.ClientPort)))
routerBody1.SetAttributeValue("routerport", cty.NumberIntVal(int64(config.RouterPort)))
routerBody1.SetAttributeValue("workerport", cty.NumberIntVal(int64(config.WorkerPort)))
rootBody.AppendNewline()
workerBlock1 := rootBody.AppendNewBlock("worker", []string{config.Name})

View file

@ -8,7 +8,7 @@ import (
gstructs "github.com/dragonheim/gagent/internal/gstructs"
/*
* picol "github.com/dragonheim/gagent/src/picol"
* picol "github.com/dragonheim/gagent/pkg/picol"
*/
prometheus "github.com/prometheus/client_golang/prometheus"
@ -43,13 +43,13 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
/*
* Generate connect string for this router.
*/
connectString := "tcp://" + config.Routers[key].RouterAddr + ":" + strconv.FormatInt(rport, 10)
connectString := "tcp://" + config.Routers[key].RouterAddr + ":" + strconv.Itoa(rport)
wg.Add(1)
go getAgent(wg, config.UUID, connectString)
}
/*
* workerListener := "tcp://" + config.ListenAddr + ":" + strconv.FormatInt(config.WorkerPort, 10)
* workerListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.WorkerPort)
*/
}

View file

@ -1,6 +1,7 @@
package picol
import (
"errors"
"fmt"
"strconv"
"strings"
@ -92,8 +93,8 @@ func CommandMath(i *Interpreter, argv []string, pd interface{}) (string, error)
if a != b {
c = 1
}
default: // FIXME I hate warnings
c = 0
default:
return "0", errors.New("invalid operator " + argv[0])
}
return fmt.Sprintf("%d", c), nil
}

View file

@ -5,6 +5,7 @@ import (
"unicode/utf8"
)
// Define parser token types
const (
ptESC = iota
ptSTR
@ -15,6 +16,7 @@ const (
ptEOF
)
// parserStruct represents the parser state
type parserStruct struct {
text string
p, start, end, ln int
@ -22,26 +24,31 @@ type parserStruct struct {
Type int
}
// initParser initializes a new parserStruct instance
func initParser(text string) *parserStruct {
return &parserStruct{text, 0, 0, 0, len(text), 0, ptEOL}
return &parserStruct{text: text, ln: len(text), Type: ptEOL}
}
// next advances the parser position by one rune
func (p *parserStruct) next() {
_, w := utf8.DecodeRuneInString(p.text[p.p:])
p.p += w
p.ln -= w
}
// current returns the current rune at the parser position
func (p *parserStruct) current() rune {
r, _ := utf8.DecodeRuneInString(p.text[p.p:])
return r
}
// token returns the current token text between start and end positions
func (p *parserStruct) token() (t string) {
defer recover()
return p.text[p.start:p.end]
}
// parseSep parses whitespace separators
func (p *parserStruct) parseSep() string {
p.start = p.p
for ; p.p < len(p.text); p.next() {
@ -54,6 +61,7 @@ func (p *parserStruct) parseSep() string {
return p.token()
}
// parseEol parses end of line and comments
func (p *parserStruct) parseEol() string {
p.start = p.p
@ -70,6 +78,7 @@ func (p *parserStruct) parseEol() string {
return p.token()
}
// parseCommand parses a command within brackets
func (p *parserStruct) parseCommand() string {
level, blevel := 1, 0
p.next() // skip
@ -103,6 +112,7 @@ Loop:
return p.token()
}
// parseVar parses a variable reference
func (p *parserStruct) parseVar() string {
p.next() // skip the $
p.start = p.p
@ -132,6 +142,7 @@ func (p *parserStruct) parseVar() string {
return p.token()
}
// parseBrace parses a brace-enclosed string
func (p *parserStruct) parseBrace() string {
level := 1
p.next() // skip
@ -160,6 +171,7 @@ Loop:
return p.token()
}
// parseString parses a string with or without quotes
func (p *parserStruct) parseString() string {
newword := p.Type == ptSEP || p.Type == ptEOL || p.Type == ptSTR
@ -203,6 +215,7 @@ Loop:
return p.token()
}
// parseComment skips over comment text
func (p *parserStruct) parseComment() string {
for p.ln != 0 && p.current() != '\n' {
p.next()
@ -210,6 +223,7 @@ func (p *parserStruct) parseComment() string {
return p.token()
}
// GetToken returns the next token from the parser
func (p *parserStruct) GetToken() string {
for {
if p.ln == 0 {
@ -246,5 +260,4 @@ func (p *parserStruct) GetToken() string {
return p.parseString()
}
}
/* return p.token() /* unreached */
}

View file

@ -12,20 +12,10 @@ import (
var fname = flag.String("f", "", "file name")
// CommandPuts is a simple version of the TCL puts function.
func CommandPuts(i *picol.Interpreter, argv []string, pd interface{}) (string, error) {
if len(argv) != 2 {
return "", fmt.Errorf("wrong number of args for %s %s", argv[0], argv)
}
fmt.Println(argv[1])
return "", nil
}
func main() {
flag.Parse()
interp := picol.NewInterpreter()
interp.RegisterCoreCommands()
interp.RegisterCommand("puts", CommandPuts, nil)
buf, err := ioutil.ReadFile(*fname)
if err == nil {