Skip to content

Commit 84a6b56

Browse files
committed
Add more code explanations.
1 parent 00fb8a0 commit 84a6b56

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

backtest/backtest.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ type Backtest struct {
5959
zmqConn *ZmqConn
6060
}
6161

62+
// NewBacktest create new Backtest object with selected start,
63+
// end dates and create new ZMQ connection using chosen socket URL
6264
func NewBacktest(start, end, socketUrl string) (*Backtest, error) {
65+
66+
// Create new ZMQ connection
6367
zmqConn, err := NewZmq(socketUrl)
6468
if err != nil {
6569
return nil, err
@@ -73,7 +77,11 @@ func NewBacktest(start, end, socketUrl string) (*Backtest, error) {
7377
}, nil
7478
}
7579

80+
// CallErocMethod parse client request data and use it to create new EROC request and send data using ZMQ;
81+
// Returns EROC response as HTTP response.
7682
func (b *Backtest) CallErocMethod(req *http.Request) *http.Response {
83+
84+
// Parse request data as JSON to erocRequestData structure
7785
erocRequestData := ErocRequestData{}
7886

7987
if req.Body != nil {
@@ -111,6 +119,7 @@ func (b *Backtest) CallErocMethod(req *http.Request) *http.Response {
111119
return b.errorHandler(req, err, DefaultErrorMessage)
112120
}
113121

122+
// Set runtime events
114123
b.runtimeEvents = erocResponse.Events
115124

116125
erocJSONResponse, err := json.Marshal(BacktestResponse{
@@ -139,7 +148,9 @@ func (b *Backtest) CallErocMethod(req *http.Request) *http.Response {
139148

140149
}
141150

151+
// errorHandler returns HTTP Bad Gateway error if something unexpected happened inside CallErocMethod function
142152
func (b *Backtest) errorHandler(req *http.Request, err error, message string) *http.Response {
153+
143154
// Log source error
144155
if err != nil {
145156
log.Println(err)
@@ -171,14 +182,17 @@ func (b *Backtest) errorHandler(req *http.Request, err error, message string) *h
171182
return res
172183
}
173184

185+
// SetCurrentBarInfo set currentBarInfo datetime and resolution
174186
func (b *Backtest) SetCurrentBarInfo(info *BarInfo) {
175187
b.currentBarInfo = info
176188
}
177189

190+
// GetRuntimeEvents returns current Backtest events data
178191
func (b *Backtest) GetRuntimeEvents() map[string]interface{} {
179192
return b.runtimeEvents
180193
}
181194

195+
// Close ZMQ connection
182196
func (b *Backtest) Close() {
183197
b.zmqConn.Close()
184198
}

backtest/zmq.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ type ZmqConn struct {
99
req *goczmq.Sock
1010
}
1111

12+
// NewZmq create new Req socket and connect it to the router
1213
func NewZmq(socketUrl string) (*ZmqConn, error) {
13-
14-
// Create a new Req socket and connect it to the router.
1514
req, err := goczmq.NewReq(socketUrl)
1615
if err != nil {
1716
return nil, err
@@ -20,12 +19,14 @@ func NewZmq(socketUrl string) (*ZmqConn, error) {
2019
return &ZmqConn{req}, nil
2120
}
2221

22+
// SendMsg sends a byte array via the socket
2323
func (z *ZmqConn) SendMsg(msg []byte) error {
2424
err := z.req.SendFrame(msg, goczmq.FlagNone)
2525

2626
return err
2727
}
2828

29+
// SendJSON convert data to json and sends a byte array via the socket
2930
func (z *ZmqConn) SendJSON(src interface{}) error {
3031
srcJSON, err := json.Marshal(src)
3132
if err != nil {
@@ -39,11 +40,13 @@ func (z *ZmqConn) SendJSON(src interface{}) error {
3940
return nil
4041
}
4142

43+
// ReceiveMsg receives a full message from the socket and returns it as an array of byte arrays
4244
func (z *ZmqConn) ReceiveMsg() ([][]byte, error) {
4345
msg, err := z.req.RecvMessage()
4446
return msg, err
4547
}
4648

49+
// ReceiveJSON receives a full message from the socket and parse JSON-encoded data into selected struct
4750
func (z *ZmqConn) ReceiveJSON(dst interface{}) error {
4851
msg, err := z.req.RecvMessage()
4952
if err != nil {

net/http/http.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ type Response _http.Response
3030
type Header _http.Header
3131
type Client _http.Client
3232

33+
// NewDefaultClient returns new HTTP client pointer with default timeout
3334
func NewDefaultClient() *Client {
3435
return &Client{Timeout: defaultTimeout}
3536
}
3637

3738
var DefaultClient = NewDefaultClient()
3839
var httpDefaultClient = _http.DefaultClient
3940

41+
// newRequestWithContentType wraps NewRequestWithContext using context.Background and set content type to headers
4042
func newRequestWithContentType(method, url string, contentType string, body io.Reader) (*_http.Request, error) {
4143
req, err := NewRequest(method, url, body)
4244
if err != nil {
@@ -49,6 +51,8 @@ func newRequestWithContentType(method, url string, contentType string, body io.R
4951
return req, err
5052
}
5153

54+
// Do send an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth)
55+
// as configured on the client.
5256
func (c *Client) Do(req *_http.Request) (*_http.Response, error) {
5357

5458
// If url include protocol, then use default http client
@@ -59,6 +63,9 @@ func (c *Client) Do(req *_http.Request) (*_http.Response, error) {
5963

6064
}
6165

66+
// processRequest proxy request to external endpoints
67+
// or proxy to Tradologics API if URL doesn't include protocol,
68+
// or proxy to Backtest/ZMQ client if backtest mode is turned on
6269
func (c *Client) processRequest(method, url, contentType string, body io.Reader, header _http.Header) (*_http.Response, error) {
6370

6471
if includeProtocol(url) {
@@ -72,7 +79,6 @@ func (c *Client) processRequest(method, url, contentType string, body io.Reader,
7279
}
7380

7481
if IsBacktest {
75-
// TODO error handler
7682
req, err := newRequestWithContentType(method, url, contentType, body)
7783
if err != nil {
7884
return nil, err
@@ -107,42 +113,52 @@ func (c *Client) processRequest(method, url, contentType string, body io.Reader,
107113
return r, nil
108114
}
109115

116+
// Head issues a HEAD to the specified URL using default client
110117
func Head(url string) (resp *_http.Response, err error) {
111118
return DefaultClient.Head(url)
112119
}
113120

121+
// Head issues a HEAD to the specified URL
114122
func (c *Client) Head(url string) (resp *_http.Response, err error) {
115123
return c.processRequest("HEAD", url, "", nil, nil)
116124
}
117125

126+
// Get issues a GET to the specified URL using default client
118127
func Get(url string) (resp *_http.Response, err error) {
119128
return DefaultClient.Get(url)
120129
}
121130

131+
// Get issues a GET to the specified URL
122132
func (c *Client) Get(url string) (resp *_http.Response, err error) {
123133
return c.processRequest("GET", url, "", nil, nil)
124134
}
125135

136+
// Post issues a POST to the specified URL using default client
126137
func Post(url, contentType string, body io.Reader) (resp *_http.Response, err error) {
127138
return DefaultClient.Post(url, contentType, body)
128139
}
129140

141+
// Post issues a POST to the specified URL
130142
func (c *Client) Post(url, contentType string, body io.Reader) (resp *_http.Response, err error) {
131143
return c.processRequest("POST", url, contentType, body, nil)
132144
}
133145

146+
// PostForm issues a POST to the specified URL using default client
134147
func PostForm(url string, data url.Values) (resp *_http.Response, err error) {
135148
return DefaultClient.PostForm(url, data)
136149
}
137150

151+
// PostForm issues a POST to the specified URL using default client
138152
func (c *Client) PostForm(url string, data url.Values) (resp *_http.Response, err error) {
139153
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
140154
}
141155

156+
// SetToken set Authorization token
142157
func SetToken(token string) {
143158
Token = token
144159
}
145160

161+
// SetBacktestMode turn on backtest mode
146162
func SetBacktestMode(start, end string) (err error) {
147163
Backtest, err = backtest.NewBacktest(start, end, socketUrl)
148164
if err != nil {
@@ -154,6 +170,7 @@ func SetBacktestMode(start, end string) (err error) {
154170
return nil
155171
}
156172

173+
// SetCurrentBarInfo set current Backtest currentBarInfo datetime and resolution
157174
func SetCurrentBarInfo(info *backtest.BarInfo) error {
158175
if Backtest != nil {
159176
Backtest.SetCurrentBarInfo(info)
@@ -163,6 +180,7 @@ func SetCurrentBarInfo(info *backtest.BarInfo) error {
163180
return errors.New("please set backtest mode first")
164181
}
165182

183+
// GetRuntimeEvents returns current Backtest runtime events
166184
func GetRuntimeEvents() (map[string]interface{}, error) {
167185
if Backtest != nil {
168186
return Backtest.GetRuntimeEvents(), nil

net/http/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package http
22

33
import "strings"
44

5+
// includeProtocol returns trues if string include protocol
56
func includeProtocol(url string) bool {
67
if strings.Contains(url, "://") {
78
return true

server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
var strategyHandler func(w http.ResponseWriter, r *http.Request)
1010

11+
// postMethodOnlyHandler validate request method and execute only 'POST'
1112
func postMethodOnlyHandler(w http.ResponseWriter, r *http.Request) {
1213
if r.Method == http.MethodPost {
1314
strategyHandler(w, r)
@@ -20,6 +21,7 @@ func postMethodOnlyHandler(w http.ResponseWriter, r *http.Request) {
2021
}
2122
}
2223

24+
// router returns http server mux with selected handler and URL path
2325
func router(strategy func(w http.ResponseWriter, r *http.Request), endpoint string) http.Handler {
2426
strategyHandler = strategy
2527

@@ -29,6 +31,7 @@ func router(strategy func(w http.ResponseWriter, r *http.Request), endpoint stri
2931
return router
3032
}
3133

34+
// Start create new server with selected host and port, and use strategy as request handler
3235
func Start(strategy func(w http.ResponseWriter, r *http.Request), endpoint, host string, port int) {
3336
err := http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), router(strategy, endpoint))
3437
if err != nil {

0 commit comments

Comments
 (0)