
The big change is that the GeoJSON package has been completely rewritten to fix a few of geometry calculation bugs, increase performance, and to better follow the GeoJSON spec RFC 7946. GeoJSON updates - A LineString now requires at least two points. - All json members, even foreign, now persist with the object. - The bbox member persists too but is no longer used for geometry calculations. This is change in behavior. Previously Tile38 would treat the bbox as the object's physical rectangle. - Corrections to geometry intersects and within calculations. Faster spatial queries - The performance of Point-in-polygon and object intersect operations are greatly improved for complex polygons and line strings. It went from O(n) to roughly O(log n). - The same for all collection types with many children, including FeatureCollection, GeometryCollection, MultiPoint, MultiLineString, and MultiPolygon. Codebase changes - The pkg directory has been renamed to internal - The GeoJSON internal package has been moved to a seperate repo at https://github.com/tidwall/geojson. It's now vendored. Please look out for higher memory usage for datasets using complex shapes. A complex shape is one that has 64 or more points. For these shapes it's expected that there will be increase of least 54 bytes per point.
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const dialTimeout = time.Second * 3
|
|
const pingTimeout = time.Second
|
|
|
|
// Pool represents a pool of tile38 connections.
|
|
type Pool struct {
|
|
mu sync.Mutex
|
|
conns []*Conn
|
|
addr string
|
|
closed bool
|
|
}
|
|
|
|
// DialPool creates a new pool with 5 initial connections to the specified tile38 server.
|
|
func DialPool(addr string) (*Pool, error) {
|
|
pool := &Pool{
|
|
addr: addr,
|
|
}
|
|
// create some connections. 5 is a good start
|
|
var tconns []*Conn
|
|
for i := 0; i < 5; i++ {
|
|
conn, err := pool.Get()
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("unable to fill pool: %s", err)
|
|
}
|
|
tconns = append(tconns, conn)
|
|
}
|
|
pool.conns = tconns
|
|
return pool, nil
|
|
}
|
|
|
|
// Close releases the resources used by the pool.
|
|
func (pool *Pool) Close() error {
|
|
pool.mu.Lock()
|
|
defer pool.mu.Unlock()
|
|
if pool.closed {
|
|
return errors.New("pool closed")
|
|
}
|
|
pool.closed = true
|
|
for _, conn := range pool.conns {
|
|
conn.pool = nil
|
|
conn.Close()
|
|
}
|
|
pool.conns = nil
|
|
return nil
|
|
}
|
|
|
|
// Get borrows a connection. When the connection closes, the application returns it to the pool.
|
|
func (pool *Pool) Get() (*Conn, error) {
|
|
pool.mu.Lock()
|
|
defer pool.mu.Unlock()
|
|
for len(pool.conns) != 0 {
|
|
i := rand.Int() % len(pool.conns)
|
|
conn := pool.conns[i]
|
|
pool.conns = append(pool.conns[:i], pool.conns[i+1:]...)
|
|
// Ping to test on borrow.
|
|
conn.SetDeadline(time.Now().Add(pingTimeout))
|
|
if _, err := conn.Do("PING"); err != nil {
|
|
conn.pool = nil
|
|
conn.Close()
|
|
continue
|
|
}
|
|
conn.SetDeadline(time.Time{})
|
|
return conn, nil
|
|
}
|
|
conn, err := DialTimeout(pool.addr, dialTimeout)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
conn.pool = pool
|
|
return conn, nil
|
|
}
|
|
|
|
func (pool *Pool) put(conn *Conn) error {
|
|
pool.mu.Lock()
|
|
defer pool.mu.Unlock()
|
|
if pool.closed {
|
|
return errors.New("pool closed")
|
|
}
|
|
conn.SetDeadline(time.Time{})
|
|
conn.SetReadDeadline(time.Time{})
|
|
conn.SetWriteDeadline(time.Time{})
|
|
pool.conns = append(pool.conns, conn)
|
|
return nil
|
|
}
|