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