mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 09:25:41 +08:00
30 lines
646 B
Go
30 lines
646 B
Go
package dns
|
|
|
|
import "sync/atomic"
|
|
|
|
// UpStream is a dns upstream.
|
|
type UpStream struct {
|
|
index uint32
|
|
servers []string
|
|
}
|
|
|
|
// NewUpStream returns a new UpStream.
|
|
func NewUpStream(servers []string) *UpStream {
|
|
return &UpStream{servers: servers}
|
|
}
|
|
|
|
// Server returns a dns server.
|
|
func (u *UpStream) Server() string {
|
|
return u.servers[atomic.LoadUint32(&u.index)%uint32(len(u.servers))]
|
|
}
|
|
|
|
// Switch switches to the next dns server.
|
|
func (u *UpStream) Switch() string {
|
|
return u.servers[atomic.AddUint32(&u.index, 1)%uint32(len(u.servers))]
|
|
}
|
|
|
|
// Len returns the number of dns servers.
|
|
func (u *UpStream) Len() int {
|
|
return len(u.servers)
|
|
}
|