-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_message.go
56 lines (50 loc) · 1.5 KB
/
redis_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package container_monitor
import (
"encoding/json"
"log"
)
const (
START_COMMAND = "start_test" // Stress test started.
STOP_COMMAND = "stop_test" // Stress test stopped.
STRESS_TEST_CHANNEL = "stress_test_client" // Redis channel name.
)
// Redis pub/sub message
type redisMessage struct {
Command string
TestID string
}
// Returns new instance of Redis pub/sub message.
//
// params: command string Kind of command.
// test_id string Stress test ID.
func newRedisMessage(command string, test_id string) *redisMessage {
return &redisMessage{
Command: command,
TestID: test_id,
}
}
// Encodes Redis pub/sub message instance to JSON string.
//
// param: message *redisMessage Instance of Redis pub/sub message.
// return JSON string or error instance.
func marshalRedisMessage(message *redisMessage) (string, error) {
message_string, err := json.Marshal(message)
if err != nil {
log.Printf("Can not marshal redis message %s", err.Error())
return err.Error(), err
}
return string(message_string), nil
}
// Decodes Redis pub/sub message JSON string to instance of *redisMessage
//
// param: message_string string JSON message string.
// return: Instance of *redisMessage or error instance.
func unmarshalRedisMessage(message_string string) (*redisMessage, error) {
mess := &redisMessage{}
err := json.Unmarshal([]byte(message_string), mess)
if err != nil {
log.Printf("Can not unmarshal redis message %s", err.Error())
return nil, err
}
return mess, nil
}