-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lonord
committed
Jun 18, 2018
1 parent
b7d9022
commit 77c4d98
Showing
5 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package app | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/lonord/sse" | ||
) | ||
|
||
const readInterval = time.Second * 3 | ||
|
||
type netSpeedService struct { | ||
sse *sse.Service | ||
action *MainAction | ||
reader *WrappedNetSpeedReader | ||
lck sync.Mutex | ||
} | ||
|
||
func newNetSpeedService(action *MainAction) *netSpeedService { | ||
return &netSpeedService{ | ||
action: action, | ||
} | ||
} | ||
|
||
func (s *netSpeedService) handleClient(clientID interface{}, w http.ResponseWriter) error { | ||
c, err := s.sse.HandleClient(clientID, w) | ||
if err != nil { | ||
return err | ||
} | ||
go s.runNetSpeedReader() | ||
<-c | ||
return nil | ||
} | ||
|
||
func (s *netSpeedService) runNetSpeedReader() { | ||
s.lck.Lock() | ||
if s.reader == nil { | ||
rd, err := s.action.CreateNetSpeedReader() | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
s.reader = rd | ||
} | ||
s.lck.Unlock() | ||
timer := time.NewTimer(readInterval) | ||
for { | ||
select { | ||
case <-timer.C: | ||
if s.sse.GetClientCount() == 0 { | ||
s.lck.Lock() | ||
s.reader = nil | ||
s.lck.Unlock() | ||
return | ||
} | ||
result, _ := s.reader.Read() | ||
s.sse.Broadcast(sse.Event{ | ||
Data: result, | ||
}) | ||
timer.Reset(readInterval) | ||
} | ||
} | ||
} else { | ||
s.lck.Unlock() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters