mattbaird/elastigo

Issue with reusing connection in another go procedure..

Opened this issue · 1 comments

Getting this error when trying to reuse connection from another go procedure:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x12c8bf]

goroutine 50 [running]:
github.com/mattbaird/elastigo/lib.(*Conn).NewRequest(0x0, 0x3dacb0, 0x3, 0xc8201c9050, 0x25, 0x0, 0x0, 0xc820055180, 0x0, 0x0)
    /Users/dyatlov/go/src/github.com/mattbaird/elastigo/lib/connection.go:138 +0x9ef
github.com/mattbaird/elastigo/lib.(*Conn).DoCommand(0x0, 0x3dae50, 0x3, 0xc82015b170, 0x25, 0x0, 0x2c0840, 0xc8200fe5a0, 0x0, 0x0, ...)
    /Users/dyatlov/go/src/github.com/mattbaird/elastigo/lib/baserequest.go:33 +0x18b
github.com/mattbaird/elastigo/lib.(*Conn).IndexWithParameters(0x0, 0x7fff5fbffa70, 0x8, 0x3dd740, 0x7, 0xc8202265c0, 0x12, 0x0, 0x0, 0x0, ...)
    /Users/dyatlov/go/src/github.com/mattbaird/elastigo/lib/coreindex.go:56 +0x28a
github.com/mattbaird/elastigo/lib.(*Conn).Index(0x0, 0x7fff5fbffa70, 0x8, 0x3dd740, 0x7, 0xc8202265c0, 0x12, 0x0, 0x2c0840, 0xc8200fe5a0, ...)
    /Users/dyatlov/go/src/github.com/mattbaird/elastigo/lib/coreindex.go:37 +0x147

But if I create another connection in go procedure itself (in the same way as I do in main() ) then all goes fine.

Some pieces of my code:

// ESHost Elasticsearch host
var ESHost = flag.String("eshost", "localhost", "Elasticsearch Server Host Address")

// ESPort Elasticsearch port
var ESPort = flag.String("esport", "9200", "Elasticsearch Server Port Address")

// ESIndex Elasticsearch index name
var ESIndex = flag.String("esindex", "websites", "Elasticsearch Index Name")

// ESCon Elasticsearch connection
var ESCon *elastigo.Conn

// WG for waiting for all threads to complete
var WG sync.WaitGroup

func main() {
    flag.Parse()

    ESCon := elastigo.NewConn()
    ESCon.Domain = *ESHost
    ESCon.Port = *ESPort

    _, err := ESCon.Count(*ESIndex, "product", nil, nil) // it all is fine here

    if err != nil {
        checkErr(err)
    }

       // then I create a pull of background workers..
       // and try to reuse ESCon in there ( ESCon.Index(..) ), but it fails with the error above
}

elastigo.Conn is not thread safe, so you'll have to create a new instance in each worker.

See this open PR #165