diff --git a/internal/database/query.go b/internal/database/query.go index 02b8bdf..b128abd 100644 --- a/internal/database/query.go +++ b/internal/database/query.go @@ -119,7 +119,11 @@ func sendResults(results resulter, req request.Request) { } for i := startIndex; i < startIndex+maxResults; i++ { - req.ResponseChannel <- results.Result(i) + select { + case req.ResponseChannel <- results.Result(i): + case <-req.Done: + return + } } } diff --git a/internal/request/request.go b/internal/request/request.go index 6f68f4f..55f8878 100644 --- a/internal/request/request.go +++ b/internal/request/request.go @@ -30,6 +30,9 @@ type Request struct { // ResponseChannel is the channel // on which the database will send back the results ResponseChannel chan string `json:"-"` + // Done is used to signal to the database + // that no more results are needed + Done chan struct{} `json:"-"` } type Settings struct { @@ -101,11 +104,13 @@ func serve(c net.Conn, requestReceiver chan<- Request) { err := json.NewDecoder(c).Decode(&request) if err != nil { + // TODO: send error back log.Println("failed to decode request:", err) return } request.ResponseChannel = make(chan string) + request.Done = make(chan struct{}) requestReceiver <- request for response := range request.ResponseChannel { @@ -116,6 +121,7 @@ func serve(c net.Conn, requestReceiver chan<- Request) { } if err != nil { log.Println("failed to write to unix domain socket:", err) + request.Done <- struct{}{} break } diff --git a/pkg/client/client.go b/pkg/client/client.go index e50cbd6..2045043 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -61,12 +61,12 @@ func SearchRequest(searchQuery string, options ...Option) (<-chan string, error) defer c.Close() reader := bufio.NewReader(c) for { - bytes, err := reader.ReadBytes('\n') + line, err := reader.ReadString('\n') if err != nil { // TODO: handle this error return } - responseChan <- string(bytes) + responseChan <- line } }()