@@ -22,6 +22,8 @@ package grammes
2222
2323import  (
2424	"encoding/json" 
25+ 	"errors" 
26+ 	"time" 
2527
2628	"github.com/northwesternmutual/grammes/gremconnect" 
2729)
@@ -71,20 +73,32 @@ func (c *Client) retrieveResponse(id string) ([][]byte, error) {
7173		dataPart     []byte 
7274	)
7375
74- 	if  n  :=  <- notifier .(chan  int ); n  ==  1  {
75- 		if  dataI , ok  :=  c .results .Load (id ); ok  {
76- 			for  _ , d  :=  range  dataI .([]interface {}) {
77- 				if  err , ok  =  d .(error ); ok  {
78- 					break 
79- 				}
80- 				if  dataPart , err  =  jsonMarshalData (d ); err  !=  nil  {
81- 					break 
76+ 	timeout  :=  make (chan  bool , 1 )
77+ 
78+ 	time .AfterFunc (c .requestTimeout , func () {
79+ 		timeout  <-  true 
80+ 	})
81+ 
82+ 	for  n  :=  1 ;n  ==  1 ; {
83+ 		select  {
84+ 		case  n  =  <- notifier .(chan  int ):
85+ 
86+ 			if  dataI , ok  :=  c .results .Load (id ); ok  {
87+ 				for  _ , d  :=  range  dataI .([]interface {}) {
88+ 					if  err , ok  =  d .(error ); ok  {
89+ 						break 
90+ 					}
91+ 					if  dataPart , err  =  jsonMarshalData (d ); err  !=  nil  {
92+ 						break 
93+ 					}
94+ 					data  =  append (data , dataPart )
8295				}
83- 				data  =  append (data , dataPart )
96+ 				close (notifier .(chan  int ))
97+ 				c .resultMessenger .Delete (id )
98+ 				c .deleteResponse (id )
8499			}
85- 			close (notifier .(chan  int ))
86- 			c .resultMessenger .Delete (id )
87- 			c .deleteResponse (id )
100+ 		case  <- timeout :
101+ 			return  nil , errors .New ("request failed with timeout" )
88102		}
89103	}
90104
0 commit comments