From f44423dd38aac97a203748d43bc273d46f158ed9 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Mon, 27 Feb 2023 23:33:00 -0700 Subject: [PATCH] add example client program --- client/clientProgram.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 client/clientProgram.go diff --git a/client/clientProgram.go b/client/clientProgram.go new file mode 100644 index 0000000..a87a574 --- /dev/null +++ b/client/clientProgram.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/binary" + "log" + "math/bits" + "strconv" +) + +// This is an example client program and is a placeholder for a real life situation +// In thie example, the program has a message it needs to send, and it's given a connection +// from the pool to use. At the end of the program / function it releases the connection back tot he pool +func clientProgram(message string) { + + conn, err := requestLease() + if err != nil { + log.Printf("could not get connection from pool") + return + } + + log.Printf("attempted to send message '%s'", message) + integerMessage, err := strconv.Atoi(message) + if err != nil { + log.Printf("failed to conver message '%s' to an integer: %v", message, err) + return + } + + buf := make([]byte, 8) + binary.BigEndian.PutUint64(buf, uint64(integerMessage)) + _, err = conn.Write(buf[bits.LeadingZeros64(uint64(integerMessage))>>3:]) + if err != nil { + log.Printf("failed to write to socket: %v", err) + return + } + + releaseConnection(conn) +}