-
Notifications
You must be signed in to change notification settings - Fork 16
Add FCH #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new FCH client for the NDN-FCH service, enabling HTTP queries to retrieve router information.
- Introduces core types (Request, Response, Router) with helper functions to apply defaults and build query URLs.
- Implements the Query function to perform HTTP requests and parse responses into routers.
Files not reviewed (1)
- go.mod: Language not supported
Comments suppressed due to low confidence (1)
std/ndn/fch/fch.go:47
- The function 'max' is referenced here but is not defined. Consider defining a helper function or using appropriate logic to ensure req.Count is at least 1.
req.Count = max(1, req.Count)
} | ||
hReq.Header.Set("Accept", "application/json, text/plain, */*") | ||
|
||
hRes, e := http.DefaultClient.Do(hReq) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HTTP response body is not closed after reading, which could lead to resource leaks. Consider adding 'defer hRes.Body.Close()' after checking for a nil error.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is accurate. By document:
If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close. If the Body is not both read to EOF and closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.
io.ReadAll
does not automatically close it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested with the following code and it works.
res, err := fch.Query(context.Background(), fch.Request{})
if err != nil {
println(err)
} else {
retJson, _ := json.Marshal(res)
println(string(retJson))
}
} | ||
hReq.Header.Set("Accept", "application/json, text/plain, */*") | ||
|
||
hRes, e := http.DefaultClient.Do(hReq) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is accurate. By document:
If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close. If the Body is not both read to EOF and closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.
io.ReadAll
does not automatically close it.
"github.com/gorilla/schema" | ||
) | ||
|
||
var encoder = schema.NewEncoder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I don't like using a global variable.
req.applyDefaults() | ||
u, e := req.toURL() | ||
if e != nil { | ||
return res, e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to write explicitly the values when using named result parameters.
routers := bytes.Split(body, []byte{','}) | ||
for _, router := range routers { | ||
if len(router) == 0 { | ||
return res, errors.New("empty response") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do not check if routers
array is empty, but to check individual element instead?
return res, errors.New("empty response") | ||
} | ||
|
||
connect := string(router) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Connect sounds too much like a function. Maybe rename to uri
.
No description provided.