add example client program

This commit is contained in:
Steven Polley 2023-02-27 23:33:00 -07:00
parent 5d5e28ad94
commit f44423dd38

37
client/clientProgram.go Normal file
View File

@ -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)
}