mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.2 KiB
74 lines
1.2 KiB
// Copyright 2022 ROC. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package service
|
|
|
|
import "sync"
|
|
|
|
var (
|
|
httpServers = newServerPool[*httpServer]()
|
|
grpcServers = newServerPool[*grpcServer]()
|
|
)
|
|
|
|
const (
|
|
_statusServerUnknow uint8 = iota
|
|
_statusServerInitilized
|
|
_statusServerStarted
|
|
_statusServerStoped
|
|
)
|
|
|
|
type server interface {
|
|
status() uint8
|
|
setStatus(uint8)
|
|
start() error
|
|
stop() error
|
|
}
|
|
|
|
type serverPool[T server] struct {
|
|
servers map[string]T
|
|
}
|
|
|
|
type baseServer struct {
|
|
sync.RWMutex
|
|
serverStatus uint8
|
|
}
|
|
|
|
func (p *serverPool[T]) from(addr string, newServer func() T) T {
|
|
s, exist := p.servers[addr]
|
|
if exist {
|
|
return s
|
|
}
|
|
s = newServer()
|
|
s.setStatus(_statusServerInitilized)
|
|
p.servers[addr] = s
|
|
return s
|
|
}
|
|
|
|
func (s *baseServer) setStatus(status uint8) {
|
|
s.RLock()
|
|
defer s.RUnlock()
|
|
|
|
s.serverStatus = status
|
|
}
|
|
|
|
func (s *baseServer) status() uint8 {
|
|
s.RLock()
|
|
defer s.RUnlock()
|
|
|
|
return s.serverStatus
|
|
}
|
|
|
|
func newServerPool[T server]() *serverPool[T] {
|
|
return &serverPool[T]{
|
|
servers: make(map[string]T),
|
|
}
|
|
}
|
|
|
|
func newBaseServe() *baseServer {
|
|
return &baseServer{
|
|
RWMutex: sync.RWMutex{},
|
|
serverStatus: _statusServerUnknow,
|
|
}
|
|
}
|