Fix memory leak

This commit is contained in:
Chteufleur 2016-07-18 23:08:21 +02:00
parent 731183c637
commit 5ae82dfff6
4 changed files with 32 additions and 34 deletions

View File

@ -74,11 +74,13 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
chanAnswer := make(chan string) chanAnswer := make(chan string)
ChanRequest <- jid client := new(xmpp.Client)
ChanRequest <- method client.JID = jid
ChanRequest <- domain client.Method = method
ChanRequest <- transaction client.Domain = domain
ChanRequest <- chanAnswer client.Transaction = transaction
client.ChanReply = chanAnswer
client.QueryClient()
select { select {
case answer := <-chanAnswer: case answer := <-chanAnswer:
@ -97,7 +99,16 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
} }
case <-time.After(time.Duration(timeout) * time.Second): case <-time.After(time.Duration(timeout) * time.Second):
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
delete(xmpp.WaitMessageAnswers, transaction) }
switch client.TypeSend {
case xmpp.TYPE_SEND_IQ:
log.Printf("%sDelete IQ", LogDebug)
delete(xmpp.WaitIqMessages, client.IdMap)
case xmpp.TYPE_SEND_MESSAGE:
log.Printf("%sDelete Message", LogDebug)
delete(xmpp.WaitMessageAnswers, client.IdMap)
} }
} }

23
main.go
View File

@ -15,7 +15,7 @@ import (
) )
const ( const (
Version = "v0.3" Version = "v0.3.1"
configurationFilePath = "httpAuth.cfg" configurationFilePath = "httpAuth.cfg"
default_xmpp_server_address = "127.0.0.1" default_xmpp_server_address = "127.0.0.1"
@ -69,24 +69,6 @@ func init() {
xmpp.Debug = mapConfig["xmpp_debug"] == "true" xmpp.Debug = mapConfig["xmpp_debug"] == "true"
} }
func request() {
for {
client := new(xmpp.Client)
client.JID = getChanString(http.ChanRequest)
client.Method = getChanString(http.ChanRequest)
client.Domain = getChanString(http.ChanRequest)
client.Transaction = getChanString(http.ChanRequest)
chanResult := <-http.ChanRequest
if v, ok := chanResult.(chan string); ok {
client.ChanReply = v
}
go client.QueryClient()
}
}
func getChanString(c chan interface{}) string { func getChanString(c chan interface{}) string {
ret := "" ret := ""
i := <-c i := <-c
@ -100,7 +82,6 @@ func main() {
go http.Run() go http.Run()
go xmpp.Run() go xmpp.Run()
go request()
sigchan := make(chan os.Signal, 1) sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt) signal.Notify(sigchan, os.Interrupt)
@ -108,8 +89,6 @@ func main() {
signal.Notify(sigchan, os.Kill) signal.Notify(sigchan, os.Kill)
<-sigchan <-sigchan
// TODO close all ressources
log.Println("Exit main()") log.Println("Exit main()")
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }

View File

@ -14,6 +14,9 @@ const (
REPLY_UNREACHABLE = "reply_unreachable" REPLY_UNREACHABLE = "reply_unreachable"
REPLY_DENY = "reply_deny" REPLY_DENY = "reply_deny"
REPLY_OK = "reply_ok" REPLY_OK = "reply_ok"
TYPE_SEND_MESSAGE = "type_send_message"
TYPE_SEND_IQ = "type_send_iq"
) )
type Client struct { type Client struct {
@ -22,6 +25,9 @@ type Client struct {
Domain string Domain string
Transaction string Transaction string
TypeSend string
IdMap string
ChanReply chan string ChanReply chan string
} }
@ -30,7 +36,7 @@ func (client *Client) QueryClient() {
isAutoGeneratedTranctionID := false isAutoGeneratedTranctionID := false
if client.Transaction == "" { if client.Transaction == "" {
// Random transaction ID generation // Random transaction ID generation
client.Transaction = xmpp.SessionID() client.Transaction = TransactionID()
isAutoGeneratedTranctionID = true isAutoGeneratedTranctionID = true
} }
clientJID, _ := xmpp.ParseJID(client.JID) clientJID, _ := xmpp.ParseJID(client.JID)
@ -49,6 +55,9 @@ func (client *Client) askViaIQ() {
m.PayloadEncode(confirm) m.PayloadEncode(confirm)
WaitIqMessages[stanzaIDstr] = client WaitIqMessages[stanzaIDstr] = client
comp.Out <- m comp.Out <- m
client.TypeSend = TYPE_SEND_IQ
client.IdMap = stanzaIDstr
} }
func (client *Client) askViaMessage(isAutoGeneratedTranctionID bool) { func (client *Client) askViaMessage(isAutoGeneratedTranctionID bool) {
@ -67,9 +76,12 @@ func (client *Client) askViaMessage(isAutoGeneratedTranctionID bool) {
log.Printf("%sSend message %v", LogInfo, m) log.Printf("%sSend message %v", LogInfo, m)
WaitMessageAnswers[client.Transaction] = client WaitMessageAnswers[client.Transaction] = client
comp.Out <- m comp.Out <- m
client.TypeSend = TYPE_SEND_MESSAGE
client.IdMap = client.Transaction
} }
func SessionID() string { func TransactionID() string {
var bytes = make([]byte, 8) var bytes = make([]byte, 8)
if _, err := rand.Read(bytes); err != nil { if _, err := rand.Read(bytes); err != nil {
panic(err) panic(err)

View File

@ -55,7 +55,6 @@ func mainXMPP() {
confirm := v.Confir confirm := v.Confir
if confirm != nil { if confirm != nil {
client := WaitMessageAnswers[confirm.Id] client := WaitMessageAnswers[confirm.Id]
delete(WaitMessageAnswers, confirm.Id)
processConfirm(v, client) processConfirm(v, client)
} else { } else {
// If body is the confirmation id, it will be considerated as accepted. // If body is the confirmation id, it will be considerated as accepted.
@ -63,7 +62,6 @@ func mainXMPP() {
client := WaitMessageAnswers[v.Body] client := WaitMessageAnswers[v.Body]
jidFrom, _ := xmpp.ParseJID(v.From) jidFrom, _ := xmpp.ParseJID(v.From)
if client != nil && client.JID == jidFrom.Bare() { if client != nil && client.JID == jidFrom.Bare() {
delete(WaitMessageAnswers, v.Body)
processConfirm(v, client) processConfirm(v, client)
} }
} }
@ -88,13 +86,11 @@ func mainXMPP() {
confirm := &xmpp.Confirm{} confirm := &xmpp.Confirm{}
v.PayloadDecode(confirm) v.PayloadDecode(confirm)
client := WaitIqMessages[v.Id] client := WaitIqMessages[v.Id]
delete(WaitIqMessages, v.Id)
processConfirm(v, client) processConfirm(v, client)
default: default:
// Handle reply iq that doesn't contain HTTP-Auth namespace // Handle reply iq that doesn't contain HTTP-Auth namespace
client := WaitIqMessages[v.Id] client := WaitIqMessages[v.Id]
delete(WaitIqMessages, v.Id)
processConfirm(v, client) processConfirm(v, client)
if client == nil { if client == nil {