-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching to use my own version of certstream-go to catch panic cause…
… by jsonq lib
- Loading branch information
santrancisco
committed
Sep 10, 2018
1 parent
56a2a27
commit bf224ec
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package certstream | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/jmoiron/jsonq" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewQueryCatchPanic(v interface{}) *jsonq.JsonQuery { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Println("[ERROR] Recovered from ", r) | ||
} | ||
}() | ||
jq := jsonq.NewQuery(v) | ||
return jq | ||
} | ||
|
||
func CertStreamEventStream(skipHeartbeats bool) (chan jsonq.JsonQuery, chan error) { | ||
outputStream := make(chan jsonq.JsonQuery) | ||
errStream := make(chan error) | ||
|
||
go func() { | ||
MAINLOOP: | ||
for { | ||
c, _, err := websocket.DefaultDialer.Dial("wss://certstream.calidog.io", nil) | ||
|
||
if err != nil { | ||
errStream <- errors.Wrap(err, "Error connecting to certstream! Sleeping a few seconds and reconnecting... ") | ||
time.Sleep(5 * time.Second) | ||
continue | ||
} | ||
|
||
defer c.Close() | ||
defer close(outputStream) | ||
|
||
for { | ||
var v interface{} | ||
err = c.ReadJSON(&v) | ||
if err != nil { | ||
errStream <- errors.Wrap(err, "Error decoding json frame!") | ||
} | ||
var jq *jsonq.JsonQuery | ||
jq = NewQueryCatchPanic(v) | ||
if jq == nil { | ||
continue MAINLOOP | ||
} | ||
|
||
res, err := jq.String("message_type") | ||
if err != nil { | ||
errStream <- errors.Wrap(err, "Error creating jq object!") | ||
} | ||
|
||
if skipHeartbeats && res == "heartbeat" { | ||
continue | ||
} | ||
|
||
outputStream <- *jq | ||
} | ||
} | ||
}() | ||
|
||
return outputStream, errStream | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters