Skip to content

Commit

Permalink
Change json.Marshall for codec.Encode. Show redis connection info in log
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Luis Lucas committed Nov 29, 2018
1 parent b196787 commit c535c2b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tests/gossip/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand All @@ -25,6 +26,7 @@ import (

"github.com/bbva/qed/gossip/member"
"github.com/go-redis/redis"
"github.com/hashicorp/go-msgpack/codec"
)

type stats struct {
Expand Down Expand Up @@ -64,8 +66,9 @@ func NewRedisClient() *RedisCli {
})

pong, err := c.Ping().Result()
fmt.Println(pong, err)
// Output: PONG <nil>
if err == nil {
fmt.Printf("%s: Redis successfully connected in %s \n", pong, c.Options().Addr)
}
return &RedisCli{rcli: c}
}

Expand Down Expand Up @@ -126,16 +129,23 @@ func handler(w http.ResponseWriter, r *http.Request) {

func PublishHandler(w http.ResponseWriter, r *http.Request, client *RedisCli) {
if r.Method == "POST" {
// Decode batch to get signed snapshots and batch version.
var b BatchSnapshots
err := json.NewDecoder(r.Body).Decode(&b)
if err != nil {
fmt.Println("Error unmarshalling: ", err)
}

// Encode each signed snapshot for sending it to DB.
var buf bytes.Buffer
encoder := codec.NewEncoder(&buf, &codec.MsgpackHandle{})

for i, s := range b.Snapshots {
key := strconv.FormatUint(s.Snapshot.Version, 10)
encSnap, _ := json.Marshal(s)
client.QueueCommands(key, encSnap)

_ = encoder.Encode(s)

client.QueueCommands(key, buf.Bytes())
if i%len(b.Snapshots) == 0 {
go client.Execute()
}
Expand Down

0 comments on commit c535c2b

Please sign in to comment.