Setting a value in structure fails - http

I set the value in the structure, but it is not set. The methods are called consequently, not in parallel. How can that be? This is golang, forgot to say.
If I change the code to set value in the "start" method (instead of "init" method), it works; but setting value in "init" method fails. Looks very strange to me.
package main
import (
"log"
"net/http"
"time"
)
type tServer struct {
ipAddress string
port string
server http.Server
}
var server tServer
func main() {
server.ipAddress = "0.0.0.0"
server.port = "12345"
server.init()
server.start()
time.Sleep(time.Second * 5)
}
func (srv tServer) init() {
srv.server.Addr = srv.ipAddress + ":" + srv.port
log.Println("srv.server.Addr=", srv.server.Addr) ////////////////////
}
func (srv tServer) start() {
log.Println("srv.server.Addr=", srv.server.Addr) ////////////////////
go srv.startServerRoutine()
}
func (srv tServer) startServerRoutine() {
log.Println("Server started at", srv.server.Addr) //
err := srv.server.ListenAndServe()
if err != nil {
log.Println("Server Error:", err) //
return
}
}
Here is the console:
2017/04/18 19:43:07 srv.server.Addr= 0.0.0.0:12345
2017/04/18 19:43:07 srv.server.Addr=
2017/04/18 19:43:07 Server started at
2017/04/18 19:43:07 Server Error: listen tcp :80: bind: permission denied

This is due to prototype of methods:
func (srv tServer) init()
// ^^^ copies values
so:
server.init() // updates its own copy of server,
// copy gets disposed after init() returns
server.start() // uses its own copy of server
You need to pass srv by pointer to methods:
func (srv *tServer) init()
in that case both init() and start() calls will work on the same copy of tServer structure and will share values in its fields.

If you look at the docs for http.Server you will see that a blank value is possible, and means ":http":
type Server struct {
Addr string // TCP address to listen on, ":http" if empty
You set srv.server.Addr directly in init(), but this is not the proper way to use the http.Server type.
Perhaps you meant to do this:
func (srv tServer) startServerRoutine() {
log.Println("Server started at", srv.ipAddress + ":" + srv.port) //
err := srv.server.ListenAndServe(srv.ipAddress + ":" + srv.port)
if err != nil {
log.Println("Server Error:", err) //
return
}
}

Related

Not able to get the keys of a Map even though gin's Context struct has Header as Map

I'm not able to get all the keys from Gin's Context.Header (Golang's gin-gonic http/rest framework) field, even though Header is defined as a Map "type Header map[string][]string" (Header is from net\http\Header.go, request is from net\hhtp\request.go, Context is from Gin-gonic's package), and surprisingly it's strange that even at the compile/build time, Visual Studio code doesn't let me call/use "MapKeys()" method on this Header which is of map type (Given Golang is statically typed language and it knows its data type at compile time already).
I need to copy all the HTTP headers into the Logger, so that when I log any message, I can put the corresponding request Headers.
And also I need to pass all the HTTP Headers from HTTP to gRPC calls for end to end call traceability need.
func (l *Logger) InfoCtx(ctx *gin.Context, md metadata.MD) *zerolog.Event {
headerName := "X-Request-Id" // Read all the headers from the ENV file
// mapping := make(map[string]string)
// mapping[headerName] = ctx.Request.Header[headerName][0]
event := l.Logger.Info()
// ctx.Request.Header ==> Even though this is a "map" type,
// which is known at the compilation time itself,
// it doesn't let me use any map functions.
if ctx != nil && len(ctx.Request.Header[headerName]) > 0 {
event = event.Str(headerName, ctx.Request.Header[headerName][0])
} else if md != nil {
// some other gRPC metadata context handling (not relevant for this question)
}
return event
}
Could you please help?
Header Object
Request object uses Header field
Shows Header is of map type
I may be misunderstanding your issue but I'm able to enumerate the map of request headers from Gin's context:
go.mod:
module github.com/OWNER/stackoverflow/69315290
go 1.16
require github.com/gin-gonic/gin v1.7.4
And main.go:
package main
import (
"log"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
for k, v := range c.Request.Header {
log.Printf("%s: %v", k, v)
}
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run()
}
And:
curl --header "dog: freddie" localhost:8080/ping
Yields:
{"message":"pong"}
And:
2021/09/25 10:41:05 User-Agent: [curl/7.68.0]
2021/09/25 10:41:05 Accept: [*/*]
2021/09/25 10:41:05 Dog: [freddie]
[GIN] 2021/09/25 - 10:41:05 | 200 | 408.631µs | 127.0.0.1 | GET "/ping"
The other approach that worked for me meanwhile was,
I created a struct to have "HttpHeadersMap map[string][]string" in it
type CommonContext struct {
HttpHeadersMap map[string][]string
RequestContext context.Context
GrpcMDHeadersMap map[string][]string
}
Assigned Gin's "ctx.Request.Header" to "HttpHeadersMap map[string][]string"
func GetCommonCtx(ctx *gin.Context, md metadata.MD) CommonContext {
var commonContext CommonContext
if ctx != nil {
// event = event.Str(headerName, ctx.Request.Header[headerName][0])
commonContext = CommonContext{ // don't return address, use valye type
HttpHeadersMap: ctx.Request.Header,
RequestContext: ctx.Request.Context(),
}
}
...
}
then inside "gRPC Interceptor (just showing for example use case)", I could use it "HttpHeadersMap" regular way as "headersMapVal.MapKeys()" to iterate over the Map keys.
func clientInterceptor(
ctx context.Context,
method string,
req interface{},
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
start := time.Now()
commonCtx := commonContext.GetCommonCtx(nil, metadata.MD{})
if callOpt, ok := opts[0].(CustomDataCallOption); ok {
headersMapVal := reflect.ValueOf(callOpt).FieldByName("HeadersMap")
newMap := make(map[string]string)
// allKeysMap := make(map[string]string)
for _, key := range headersMapVal.MapKeys() {
// fmt.Printf("headersMapVal.MapKeys(), e %v", e)
// c_key := e.Convert(headersMapValueIndirectStr.Type().Key())
keyValue := headersMapVal.MapIndex(key)
...
...
}

Golang Invalid Receiver Type in Method Func

I'm trying to make a simple package to send SSH commands to a server.
I have the following code:
type Connection *ssh.Client
func Connect(addr, user, password string) (conn Connection, err error) {
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
conn, err = ssh.Dial("tcp", addr, sshConfig)
return
}
func (conn Connection) SendCommand() ([]byte, error) {
session, err := (*ssh.Client)(conn).NewSession()
// ...
}
My problem is on the two lines func (conn Connection) SendCommand() ([]byte, error) and session, err := (*ssh.Client)(conn).NewSession().
I can't figure out how to use the methods available for *ssh.Client from my overlaying Connection type.
I understand that I need to do some conversion, and using ssh.Client(*conn).NewSession() would work, but it copies the values of the *ssh.Client which doesn't seem to be the right method.
What should do to access the methods available for a *ssh.Client when working with my custom type Connection *ssh.Client type?
You can't declare a new type with a pointer TypeSpec. Also declaring a new type is used specifically to remove the entire method set, so you won't have any of the original methods from the *ssh.Client.
What you want is to use composition by embedding the *ssh.Client in your own struct type:
type Connection struct {
*ssh.Client
}
func Connect(addr, user, password string) (*Connection, error) {
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
conn, err = ssh.Dial("tcp", addr, sshConfig)
if err != nil {
return nil, err
}
return &Connection{conn}, nil
}
func (conn *Connection) SendCommand() ([]byte, error) {
session, err := conn.NewSession()
// ...
}
This is the best I can come up with:
type Connection ssh.Client
func (conn *Connection) SendCommand() ([]byte, error) {
(*ssh.Client)(conn).NewSession()
Note that I've changed the type to not be a pointer type (but then I've made a pointer receiver for SendCommand). I'm not sure there's any way to create a function with a pointer type as a receiver.
Another option is to use type aliasing to achieve the desired behavior. I was trying to do something "clever" for readability:
type foo struct {
i int
}
type foo_ptr = *foo
type foo_ptr_slice = []foo_ptr
type foo_ptr_map = map[string]foo_ptr
type foo_ptr_slice_map = map[string]foo_ptr_slice
func (r foo_ptr) dump() {
fmt.Printf("%d\n", r.i)
}
func main() {
// need a map of slice of pointers
var m foo_ptr_map
m = make(foo_ptr_map, 0)
m["test"] = &foo{i: 1}
var m2 foo_ptr_slice_map
m2 = make(foo_ptr_slice_map, 0)
m2["test"] = make(foo_ptr_slice, 0, 10)
m2["test"] = append(m2["test"], &foo{i: 2})
fmt.Printf("%d\n", m["test"].i)
fmt.Printf("%d\n", m2["test"][0].i)
m["test"].dump()
}
I acknowledge that type aliasing is used for large-scale refactoring but this seems like a very good use for readability sake.

too many open files, when using net/http

I am writing REST service using go-json-rest, which inturn using net/http.
My server code is simply, get the request and pass it to a channel
Here is my server code
package main
import (
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
"strconv"
"time"
)
const workerCount = 4
var evChannel = make(chan Event)
var workers = make([]*LogWorker, workerCount)
const maxLogFileSize = 100 // In MB
const maxLogFileBackups = 30
const maxLogFileAge = 5
const logFileName = "/home/sam/tmp/go_logs/event_"
func main() {
// Initialize workers
// Four workers is being created
for i := 0; i < workerCount; i++ {
var fileName = logFileName + strconv.Itoa(i)
workers[i] = NewLogWorker(fileName, maxLogFileSize, maxLogFileBackups, maxLogFileAge)
go workers[i].Work(evChannel)
}
// Initialize REST API
api := rest.NewApi()
//api.Use(rest.DefaultDevStack...)
api.Use(rest.DefaultCommonStack...)
router, err := rest.MakeRouter(
rest.Post("/events", StoreEvents),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":4545", api.MakeHandler()))
}
func StoreEvents(w rest.ResponseWriter, r *rest.Request) {
event := Event{}
err := r.DecodeJsonPayload(&event)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO : Add validation if needed
// Add code to parse the request and add further information to event
// log.Println()
select {
case evChannel <- event:
case <- time.After(5 * time.Second):
// throw away the message, so sad
}
// evChannel <- event
//log.Println(Csv(event))
w.WriteHeader(http.StatusOK)
}
When I execute it continuously using jmeter I am occasionally getting the below error
http: Accept error: accept tcp [::]:4545: too many open files; retrying in 10ms
Does net/http open files for every request?
Posting elithrar comment as answer
Sockets, yes. You may need to increase your fd limit (via ulimit or sysctl).

Can't dereference pointers properly and get actual values out of an array of memory addresses

I started picking Go in the past couple of days, relying mostly on the language specification and package documentation, however I have problem deciphering the correct usage of net.LookupNS.
Since it's a pointer type, returning an array of memory addresses of NS server values, I want to access the actual values / dereference the array.
The Program:
package main
import "fmt"
import "net"
import "os"
var host string
func args() {
if len(os.Args) != 2 {
fmt.Println("You need to enter a host!")
} else {
host = os.Args[1]
}
if host == "" {
os.Exit(0)
}
}
func nslookup() []*net.NS {
nserv, err := net.LookupNS(host)
if err != nil {
fmt.Println("Error occured during NS lookup", err)
}
return *&nserv
}
func main() {
args()
fmt.Println("Nameserver information:", host)
fmt.Println(" NS records:", nslookup())
}
Given e.g. google.com, it displays the following:
Nameserver information: google.com
NS records: [0xc2100376f0 0xc210037700 0xc210037710 0xc210037720]
Instead of the memory address locations, I would like to see the dereferenced values, e.g:
NS records: ["ns1.google.com", "ns2.google.com", "ns3.google.com", "ns4.google.com"]
Now obviously, I would prefer them as an array/slice of strings, but the problem is that the only way I can get an actual nameserver out is as follows:
func nslookup() *net.NS {
// The rest of the function
return *&nserv[0] // This returns the first nameserver
The above returns the following:
Nameserver information: google.com
NS records: &{ns1.google.com.}
While this at least returns the actual value instead of a memory address, it requires indexing, which isn't very flexible and it's not formatted in a very user-friendly format.
Also, direct conversion of the []*net.NS struct to string is not possible.
The Problem:
How do I get an array of nameservers, instead of memory addresses out, preferably as an array/slice of strings?
Ok few problems :
Why are you returning *&nserv? Go is NOT C, please stop everything you're doing and read Effective Go.
Your nslookup function returns a slice of *net.NS, that's a slice of pointers, so fmt.Println is printing the right thing, if you want more details you could use fmt.Printf with %#vor %#q modifier to see how the data actually looks.
Example:
package main
import "fmt"
import "net"
import "os"
var host string
func nslookupString(nserv []*net.NS) (hosts []string) {
hosts = make([]string, len(nserv))
for i, host := range nserv {
hosts[i] = host.Host
}
return
}
func nslookupNS(host string) []*net.NS {
nserv, err := net.LookupNS(host)
if err != nil {
fmt.Println("Error occured during NS lookup", err)
}
return nserv
}
func init() { //initilizing global arguments is usually done in init()
if len(os.Args) == 2 {
host = os.Args[1]
}
}
func main() {
if host == "" {
fmt.Println("You need to enter a host!")
os.Exit(1)
}
fmt.Println("Nameserver information:", host)
ns := nslookupNS(host)
fmt.Printf(" NS records String: %#q\n", nslookupString(ns))
fmt.Printf(" NS records net.NS: %q\n", ns)
for _, h := range ns {
fmt.Printf("%#v\n", h)
}
}

What is causing my HTTP server to fail with "exit status -1073741819"?

As an exercise I created a small HTTP server that generates random game mechanics, similar to this one. I wrote it on a Windows 7 (32-bit) system and it works flawlessly. However, when I run it on my home machine, Windows 7 (64-bit), it always fails with the same message: exit status -1073741819. I haven't managed to find anything on the web which references that status code, so I don't know how important it is.
Here's code for the server, with redundancy abridged:
package main
import (
"fmt"
"math/rand"
"time"
"net/http"
"html/template"
)
// Info about a game mechanic
type MechanicInfo struct { Name, Desc string }
// Print a mechanic as a string
func (m MechanicInfo) String() string {
return fmt.Sprintf("%s: %s", m.Name, m.Desc)
}
// A possible game mechanic
var (
UnkillableObjects = &MechanicInfo{"Avoiding Unkillable Objects",
"There are objects that the player cannot touch. These are different from normal enemies because they cannot be destroyed or moved."}
//...
Race = &MechanicInfo{"Race",
"The player must reach a place before the opponent does. Like \"Timed\" except the enemy as a \"timer\" can be slowed down by the player's actions, or there may be multiple enemies being raced against."}
)
// Slice containing all game mechanics
var GameMechanics []*MechanicInfo
// Pseudorandom number generator
var prng *rand.Rand
// Get a random mechanic
func RandMechanic() *MechanicInfo {
i := prng.Intn(len(GameMechanics))
return GameMechanics[i]
}
// Initialize the package
func init() {
prng = rand.New(rand.NewSource(time.Now().Unix()))
GameMechanics = make([]*MechanicInfo, 34)
GameMechanics[0] = UnkillableObjects
//...
GameMechanics[33] = Race
}
// serving
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func randMechHandler(w http.ResponseWriter, req *http.Request) {
mechanics := [3]*MechanicInfo{RandMechanic(), RandMechanic(), RandMechanic()}
if err := index.Execute(w, mechanics); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", randMechHandler)
if err := http.ListenAndServe(":80", nil); err != nil {
panic(err)
}
}
In addition, the unabridged code, the _base.html template, and the index.html template.
What could be causing this issue? Is there a process for debugging a cryptic exit status like this?
When I ran it, I got the following two errors:
template: content:6: nil pointer evaluating *main.MechanicInfo.Name
http: multiple response.WriteHeader calls
The former was in the web browser, the latter in the console window where I launched your server.
The nil pointer problem is because your abridged program leaves GameMechanics[1:32] set to nil.
The second error is interesting. The only place in your program that any methods on your http.ResponseWriter get called is inside of index.Execute, which is not your code -- meaning maybe there is something wrong happening in html/template. I'm testing this with Go 1.0.2.
I put _base.html at the top of index.html and then changed index to this:
var index = template.Must(template.ParseFiles("templates/index.html"))
and the http.WriteHeaders warning went away.
Not really an answer, but a direction you could explore.
As a bonus, here's the more "Go way" of writing your program. Note that I simplified the use of the PRNG (you don't need to instantiate unless you want several going in parallel) and simplified the structure initializer:
package main
import (
"fmt"
"html/template"
"math/rand"
"net/http"
)
// Info about a game mechanic
type MechanicInfo struct{ Name, Desc string }
// Print a mechanic as a string
func (m MechanicInfo) String() string {
return fmt.Sprintf("%s: %s", m.Name, m.Desc)
}
// The game mechanics
var GameMechanics = [...]*MechanicInfo{
{"Avoiding Unkillable Objects",
"There are objects that the player cannot touch. These are different from normal enemies because they cannot be destroyed or moved."},
{"Race",
"The player must reach a place before the opponent does. Like \"Timed\" except the enemy as a \"timer\" can be slowed down by the player's actions, or there may be multiple enemies being raced against."},
}
// Get a random mechanic
func RandMechanic() *MechanicInfo {
i := rand.Intn(len(GameMechanics))
return GameMechanics[i]
}
var index = template.Must(template.ParseFiles("templates/index.html"))
func randMechHandler(w http.ResponseWriter, req *http.Request) {
mechanics := [3]*MechanicInfo{RandMechanic(), RandMechanic(), RandMechanic()}
if err := index.Execute(w, mechanics); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", randMechHandler)
if err := http.ListenAndServe(":80", nil); err != nil {
panic(err)
}
}

Resources