2016-01-25 06:41:39 +01:00
|
|
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2016-03-03 02:18:55 +01:00
|
|
|
"github.com/chihaya/chihaya"
|
2016-01-25 06:41:39 +01:00
|
|
|
"github.com/chihaya/chihaya/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pool represents a running pool of servers.
|
|
|
|
type Pool struct {
|
|
|
|
servers []Server
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2016-03-03 02:18:55 +01:00
|
|
|
// StartPool creates a new pool of servers specified by the provided
|
|
|
|
// configuration and runs them.
|
|
|
|
func StartPool(cfgs []chihaya.ServerConfig, tkr *tracker.Tracker) (*Pool, error) {
|
2016-03-02 22:18:34 +01:00
|
|
|
var toReturn Pool
|
2016-01-25 06:41:39 +01:00
|
|
|
|
|
|
|
for _, cfg := range cfgs {
|
|
|
|
srv, err := New(&cfg, tkr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-02 22:18:34 +01:00
|
|
|
toReturn.wg.Add(1)
|
2016-01-25 06:41:39 +01:00
|
|
|
go func(srv Server) {
|
2016-03-02 22:18:34 +01:00
|
|
|
defer toReturn.wg.Done()
|
2016-01-25 06:41:39 +01:00
|
|
|
srv.Start()
|
|
|
|
}(srv)
|
|
|
|
|
2016-03-02 22:18:34 +01:00
|
|
|
toReturn.servers = append(toReturn.servers, srv)
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 22:18:34 +01:00
|
|
|
return &toReturn, nil
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop safely shuts down a pool of servers.
|
|
|
|
func (p *Pool) Stop() {
|
|
|
|
for _, srv := range p.servers {
|
|
|
|
srv.Stop()
|
|
|
|
}
|
|
|
|
p.wg.Wait()
|
|
|
|
}
|